aboutsummaryrefslogtreecommitdiff
path: root/bc_funcs
diff options
context:
space:
mode:
authorMango0x45 <thomasvoss@live.com>2021-01-06 16:37:05 +0000
committerMango0x45 <thomasvoss@live.com>2021-01-06 16:37:05 +0000
commitaf0868e738a5dd8f24b123f170ad2e2d7a8c2140 (patch)
treef236bf233ec58aba79651b2d45f6be35507c5e9e /bc_funcs
parent1b6264627c67fbbd0d99902aa18ce045cff24319 (diff)
downloadsteve-bot-af0868e738a5dd8f24b123f170ad2e2d7a8c2140.tar
steve-bot-af0868e738a5dd8f24b123f170ad2e2d7a8c2140.tar.gz
steve-bot-af0868e738a5dd8f24b123f170ad2e2d7a8c2140.tar.bz2
steve-bot-af0868e738a5dd8f24b123f170ad2e2d7a8c2140.tar.lz
steve-bot-af0868e738a5dd8f24b123f170ad2e2d7a8c2140.tar.xz
steve-bot-af0868e738a5dd8f24b123f170ad2e2d7a8c2140.tar.zst
steve-bot-af0868e738a5dd8f24b123f170ad2e2d7a8c2140.zip
Fix factorial
Diffstat (limited to 'bc_funcs')
-rw-r--r--bc_funcs/std.bc32
1 files changed, 19 insertions, 13 deletions
diff --git a/bc_funcs/std.bc b/bc_funcs/std.bc
index 6bc58b1..e90231e 100644
--- a/bc_funcs/std.bc
+++ b/bc_funcs/std.bc
@@ -5,20 +5,26 @@ define abs(x) {
}
define fact(x) {
- auto r
+ auto r, s
- if (x < 0) {
- print "Error: Negative factorial\n"
- return 0
- }
+ if (x < 0) {
+ print "Error: Negative factorial\n"
+ return 0
+ }
- if (x % 1 != 0) {
- print "Error: Non-integer 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
- return r
+ r = 1
+ for (i = 2; i <= x; i++)
+ r *= i
+
+ scale = s
+ return r
}