diff options
Diffstat (limited to '')
-rw-r--r-- | bc_funcs/lib.bc (renamed from bc_funcs/trig.bc) | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/bc_funcs/trig.bc b/bc_funcs/lib.bc index 9a4fcb3..d413763 100644 --- a/bc_funcs/trig.bc +++ b/bc_funcs/lib.bc @@ -96,3 +96,34 @@ define atan(x) { ibase = b return ((m * a + r) / n) } + +define abs(x) { + if (x < 0) + x *= -1 + return x +} + +define fact(x) { + auto r, s + + if (x < 0) { + print "Error: Negative factorial\n" + return 0 + } + + /* x % 1 requires scale set to 0 */ + s = scale + scale = 0 + if (x % 1 != 0) { + print "Error: Non-integer factorial\n" + scale = s + return 0 + } + + r = 1 + for (i = 2; i <= x; i++) + r *= i + + scale = s + return r +} |