From 1b6264627c67fbbd0d99902aa18ce045cff24319 Mon Sep 17 00:00:00 2001 From: Mango0x45 Date: Tue, 5 Jan 2021 11:16:29 +0100 Subject: Add factorial function --- bc_funcs/std.bc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'bc_funcs') 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 +} -- cgit v1.2.3