aboutsummaryrefslogtreecommitdiffstats
path: root/func.go
diff options
context:
space:
mode:
Diffstat (limited to 'func.go')
-rw-r--r--func.go26
1 files changed, 26 insertions, 0 deletions
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