aboutsummaryrefslogtreecommitdiff
path: root/bc_funcs
diff options
context:
space:
mode:
Diffstat (limited to 'bc_funcs')
-rw-r--r--bc_funcs/std.bc19
1 files changed, 19 insertions, 0 deletions
diff --git a/bc_funcs/std.bc b/bc_funcs/std.bc
index a369505..6bc58b1 100644
--- a/bc_funcs/std.bc
+++ b/bc_funcs/std.bc
@@ -3,3 +3,22 @@ define abs(x) {
x *= -1
return x
}
+
+define fact(x) {
+ auto r
+
+ if (x < 0) {
+ print "Error: Negative factorial\n"
+ return 0
+ }
+
+ if (x % 1 != 0) {
+ print "Error: Non-integer factorial\n"
+ return 0
+ }
+
+ r = 1
+ for (i = 2; i <= x; i++)
+ r *= i
+ return r
+}