summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorReimer Behrends <behrends@gmail.com>2015-08-14 14:32:30 +0200
committerReimer Behrends <behrends@gmail.com>2015-08-14 14:32:30 +0200
commit22789a827571009b836fa20801961bedeea9f7ae (patch)
treeafe47b058a57f67560a13f91099b4491e28b52e5 /lib
parent134c44f3be373a94fd5ee144d98cd296ee967107 (diff)
downloadNim-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.nim4
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 =