Fix for isssue in parseBiggestFloat #7060 (#7061)

This commit is contained in:
cooldome
2018-01-11 00:57:20 +00:00
committed by Andreas Rumpf
parent 2c905f5e81
commit 2aebb8ed7e
2 changed files with 10 additions and 3 deletions

View File

@@ -386,8 +386,7 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
kdigits, fdigits = 0
exponent: int
integer: uint64
fraction: uint64
frac_exponent= 0
frac_exponent = 0
exp_sign = 1
first_digit = -1
has_sign = false
@@ -480,7 +479,8 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
# if integer is representable in 53 bits: fast path
# max fast path integer is 1<<53 - 1 or 8999999999999999 (16 digits)
if kdigits + fdigits <= 16 and first_digit <= 8:
let digits = kdigits + fdigits
if digits <= 15 or (digits <= 16 and first_digit <= 8):
# max float power of ten with set bits above the 53th bit is 10^22
if abs_exponent <= 22:
if exp_negative:
@@ -504,6 +504,7 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat,
result = i - start
i = start
# re-parse without error checking, any error should be handled by the code above.
if s[i] == '.': i.inc
while s[i] in {'0'..'9','+','-'}:
if ti < maxlen:
t[ti] = s[i]; inc(ti)

View File

@@ -48,5 +48,11 @@ doAssert "2.71828182845904523536028747".parseFloat ==
2.71828182845904523536028747
doAssert 0.00097656250000000021684043449710088680149056017398834228515625 ==
"0.00097656250000000021684043449710088680149056017398834228515625".parseFloat
doAssert 0.00998333 == ".00998333".parseFloat
doAssert 0.00128333 == ".00128333".parseFloat
doAssert 999999999999999.0 == "999999999999999.0".parseFloat
doAssert 9999999999999999.0 == "9999999999999999.0".parseFloat
doAssert 0.999999999999999 == ".999999999999999".parseFloat
doAssert 0.9999999999999999 == ".9999999999999999".parseFloat
echo("passed all tests.")