aboutsummaryrefslogtreecommitdiffstats
path: root/func.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-07-29 13:26:46 -0230
committerSam Anthony <sam@samanthony.xyz>2023-07-29 13:26:46 -0230
commitb9ec5c2436a4f765948a80aa881494a24996f990 (patch)
tree7f76b333d6389445955f60675370303405c634f9 /func.go
parent9a86d5718dcf0d0242a1caf4629ba683972d6dc2 (diff)
downloadpfc-b9ec5c2436a4f765948a80aa881494a24996f990.zip
inverse trig functions
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