aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--func.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/func.go b/func.go
index fee20da..42763c0 100644
--- a/func.go
+++ b/func.go
@@ -16,6 +16,8 @@ func parseFunction(fn string) func(Calculator) {
return deg
case "rad":
return rad
+ case "fac":
+ return fac
}
return nil
}
@@ -69,18 +71,36 @@ func invTrig(fn string) func(Calculator) {
}
}
+// Convert radians to degrees.
func deg(c Calculator) {
if len(c.stack) > 0 {
c.stack[len(c.stack)-1] = degrees(c.stack[len(c.stack)-1])
}
}
+// Convert degrees to radians.
func rad(c Calculator) {
if len(c.stack) > 0 {
c.stack[len(c.stack)-1] = radians(c.stack[len(c.stack)-1])
}
}
+// Factorial (!).
+func fac(c Calculator) {
+ if len(c.stack) > 0 {
+ a := &c.stack[len(c.stack)-1] // will replace with a!
+ if float64(int(*a)) != *a { // undefined on non-ints
+ return
+ } else if int(*a) == 0 { // 0! = 1
+ *a = 1.0
+ } else { // a! = a*(a-1)!
+ for i := int(*a) - 1; i > 1; i-- {
+ *a *= float64(i)
+ }
+ }
+ }
+}
+
// radians converts degrees to radians.
func radians(deg float64) float64 {
return deg * math.Pi / 180.0