-
Notifications
You must be signed in to change notification settings - Fork 76
strconv: fix ParseFloat exponent and overflow bookkeeping #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ func ParseFloat(b []byte) (float64, int) { | |
| c := b[i] | ||
| if '0' <= c && c <= '9' { | ||
| if trunk == -1 { | ||
| if math.MaxUint64/10 < n { | ||
| if math.MaxUint64/10 < n || math.MaxUint64-uint64(c-'0') < n*10 { | ||
| trunk = i | ||
| } else { | ||
| n *= 10 | ||
|
|
@@ -54,7 +54,12 @@ func ParseFloat(b []byte) (float64, int) { | |
| if trunk == -1 { | ||
| trunk = i | ||
| } | ||
| mantExp = int64(trunk - dot - 1) | ||
| if trunk < dot { | ||
| // truncated before the dot, so no dot lies between trunk and dot | ||
| mantExp = int64(trunk - dot) | ||
| } else { | ||
| mantExp = int64(trunk - dot - 1) | ||
| } | ||
| } else if trunk != -1 { | ||
| mantExp = int64(trunk - i) | ||
| } | ||
|
|
@@ -77,18 +82,36 @@ func ParseFloat(b []byte) (float64, int) { | |
| } else if 0 < exp && exp <= 15+22 { // int * 10^k | ||
| // If exponent is big but number of digits is not, | ||
| // can move a few zeros into the integer part. | ||
| if 22 < exp { | ||
| f *= float64pow10[exp-22] | ||
| exp = 22 | ||
| // Scale a copy, the fall-through below rescales f from scratch. | ||
| g, gexp := f, exp | ||
| if 22 < gexp { | ||
| g *= float64pow10[gexp-22] | ||
| gexp = 22 | ||
| } | ||
| if -1e15 <= f && f <= 1e15 { | ||
| return f * float64pow10[exp], i | ||
| if -1e15 <= g && g <= 1e15 { | ||
| return g * float64pow10[gexp], i | ||
| } | ||
| } else if -22 <= exp && exp < 0 { // int / 10^k | ||
| return f / float64pow10[-exp], i | ||
| } | ||
| f *= math.Pow10(int(-mantExp)) | ||
| return f * math.Pow10(int(expExp)), i | ||
| if f == 0.0 { | ||
| // 0 * math.Pow10(out of range) is NaN | ||
| return f, i | ||
| } | ||
| h := f * math.Pow10(int(-mantExp)) | ||
| h *= math.Pow10(int(expExp)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can both Pow10's be merged?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They could be merged mathematically, but the two-step is deliberate: for inputs like 1e-289 one of the factors stays inside math.Pow10's [-323,308] domain while the combined exponent does not. I measured the one-step variant — it turns 11,118 currently bit-exact results 1 ulp wrong — so the fix keeps the two-step and only re-scales when the result degenerates to 0/Inf. |
||
| if h == 0.0 || math.IsInf(h, 0) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't we check for 0.0 above with f==0.0?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both checks are needed: f == 0.0 catches a zero mantissa, but h == 0.0 also fires when f != 0 and the 10^-mantExp factor underflows (e.g. 5e-324 → 5 * 10^0 * 10^-324 = 0), which is exactly the case the re-scale rescues. |
||
| // either factor alone can leave math.Pow10's [-323,308] domain | ||
| if exp < -308 { | ||
| f *= math.Pow10(-308) | ||
| exp += 308 | ||
| } else if 308 < exp { | ||
| f *= math.Pow10(308) | ||
| exp -= 308 | ||
| } | ||
| h = f * math.Pow10(int(exp)) | ||
| } | ||
| return h, i | ||
| } | ||
|
|
||
| const log2 = 0.3010299956639812 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's fix this clause and do this like float, besides it moves more results closer according to your measurements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — the dead
23 < exp && exp < 0branch is now the real negative fast path, mirroring ParseFloat:-22 <= exp && exp < 0divides by the exact table power instead of multiplying by math.Pow10's inexact reciprocal. Measured over 300k structured inputs: 12,664 results closer to the correctly-rounded value vs 448 farther (e.g. 0.3 now parses to exactly 3/10). Regression test added; d6b77c8.