From a4a5003b7d1284fd751f686b54a55c98fe9c9651 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 18 Sep 2015 02:22:39 +0200 Subject: [PATCH] fixes #3312 --- lib/pure/math.nim | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index c1d5c9439..06a018d9f 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -21,6 +21,20 @@ include "system/inclrtl" {.push debugger:off .} # the user does not want to trace a part # of the standard library! +proc binom*(n, k: int): int {.noSideEffect.} = + ## Computes the binomial coefficient + if k <= 0: return 1 + if 2*k > n: return binom(n, n-k) + result = n + for i in countup(2, k): + result = (result * (n + 1 - i)) div i + +proc fac*(n: int): int {.noSideEffect.} = + ## Computes the faculty/factorial function. + result = 1 + for i in countup(2, n): + result = result * i + {.push checks:off, line_dir:off, stack_trace:off.} when defined(Posix) and not defined(haiku): @@ -72,21 +86,6 @@ proc classify*(x: float): FloatClass = return fcNormal # XXX: fcSubnormal is not detected! - -proc binom*(n, k: int): int {.noSideEffect.} = - ## Computes the binomial coefficient - if k <= 0: return 1 - if 2*k > n: return binom(n, n-k) - result = n - for i in countup(2, k): - result = (result * (n + 1 - i)) div i - -proc fac*(n: int): int {.noSideEffect.} = - ## Computes the faculty/factorial function. - result = 1 - for i in countup(2, n): - result = result * i - proc isPowerOfTwo*(x: int): bool {.noSideEffect.} = ## Returns true, if `x` is a power of two, false otherwise. ## Zero and negative numbers are not a power of two.