diff options
author | Reimer Behrends <behrends@gmail.com> | 2015-08-14 14:32:30 +0200 |
---|---|---|
committer | Reimer Behrends <behrends@gmail.com> | 2015-08-14 14:32:30 +0200 |
commit | 22789a827571009b836fa20801961bedeea9f7ae (patch) | |
tree | afe47b058a57f67560a13f91099b4491e28b52e5 /lib | |
parent | 134c44f3be373a94fd5ee144d98cd296ee967107 (diff) | |
download | Nim-22789a827571009b836fa20801961bedeea9f7ae.tar.gz |
Fix exponentiation operation to avoid overflow.
The exponentation implementation unnecessarily multiplied the result with itself at the end if the exponent was an even number. This led to overflow if result*result > high(int).
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/math.nim | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 821ab738b..8d95ea9c0 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -427,10 +427,12 @@ proc `^`*[T](x, y: T): T = var (x, y) = (x, y) result = 1 - while y != 0: + while true: if (y and 1) != 0: result *= x y = y shr 1 + if y == 0: + break x *= x proc gcd*[T](x, y: T): T = |