From 22789a827571009b836fa20801961bedeea9f7ae Mon Sep 17 00:00:00 2001 From: Reimer Behrends Date: Fri, 14 Aug 2015 14:32:30 +0200 Subject: [PATCH] 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). --- lib/pure/math.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 =