diff options
author | Dmitry Atamanov <data-man@users.noreply.github.com> | 2018-05-17 20:55:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-17 20:55:44 +0300 |
commit | 072cd073ba40d05ace580292735b5b385544d3e7 (patch) | |
tree | c51cbf69ab6e5be98b0753499bd7a9e6d1dbf573 | |
parent | 6c0185d4b7a9b38fa246c47c6d9baf3633e880d0 (diff) | |
parent | 173c92d374323f6fbbecae2ac344e6062dcfa351 (diff) | |
download | Nim-072cd073ba40d05ace580292735b5b385544d3e7.tar.gz |
Merge pull request #7835 from data-man/fix_fac
Fixes factorial's bug
-rw-r--r-- | lib/pure/math.nim | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 5f3240e00..9e590debc 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -41,7 +41,7 @@ proc fac*(n: int): int = createFactTable[13]() else: createFactTable[21]() - assert(n > 0, $n & " must not be negative.") + assert(n >= 0, $n & " must not be negative.") assert(n < factTable.len, $n & " is too large to look up in the table") factTable[n] @@ -560,3 +560,14 @@ when isMainModule: assert sgn(Inf) == 1 assert sgn(NaN) == 0 + block: # fac() tests + try: + discard fac(-1) + except AssertionError: + discard + + doAssert fac(0) == 1 + doAssert fac(1) == 1 + doAssert fac(2) == 2 + doAssert fac(3) == 6 + doAssert fac(4) == 24 |