From b9ec5c2436a4f765948a80aa881494a24996f990 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 29 Jul 2023 13:26:46 -0230 Subject: inverse trig functions --- func.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'func.go') diff --git a/func.go b/func.go index 445cb41..8fe3f0d 100644 --- a/func.go +++ b/func.go @@ -10,6 +10,8 @@ func parseFunction(fn string) func(Calculator) { switch fn { case "sin", "cos", "tan": return trig(fn) + case "asin", "acos", "atan": + return invTrig(fn) } return nil } @@ -39,6 +41,30 @@ func trig(fn string) func(Calculator) { } } +// invTrig returns a closure that performs the inverse trig function specified +// by fn. Panics if fn is not one of "asin", "acos" or "atan". +func invTrig(fn string) func(Calculator) { + return func(c Calculator) { + if len(c.stack) <= 0 { + return + } + v := &c.stack[len(c.stack)-1] + switch fn { + case "asin": + *v = math.Asin(*v) + case "acos": + *v = math.Acos(*v) + case "atan": + *v = math.Atan(*v) + default: + panic(fmt.Sprintf("invalid inverse trig function: '%s'", fn)) + } + if c.anglem == modeDeg { + *v = degrees(*v) + } + } +} + // radians converts degrees to radians. func radians(deg float64) float64 { return deg * math.Pi / 180.0 -- cgit v1.2.3