mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-26 17:24:02 +00:00
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).
This commit is contained in:
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user