commit 8079a00d631c45ee149d9236f73af4dd4e835422
parent b4ddb2a067c5baa02d4a633fa2d4d6725e145a3b
Author: Joe Finney <me@spxtr.net>
Date: Mon, 8 Jan 2024 19:04:23 +0000
strconv: Fix bug with large inputs to stof32.
Signed-off-by: Joe Finney <me@spxtr.net>
Diffstat:
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/strconv/stof.ha b/strconv/stof.ha
@@ -509,7 +509,7 @@ const f32pow10: [_]f32 = [
1.0e0, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5, 1.0e6, 1.0e7, 1.0e8, 1.0e9, 1.0e10
];
-fn stof32exact(mant: u32, exp: i32, neg: bool) (f32 | void) = {
+fn stof32exact(mant: u64, exp: i32, neg: bool) (f32 | void) = {
if (mant >> math::F32_MANTISSA_BITS != 0) return;
let n = mant: i32: f32; // XXX: ARCH
if (neg) {
@@ -592,7 +592,7 @@ export fn stof32(s: str) (f32 | invalid | overflow) = {
if (p is fast_parsed_float) {
const p = p: fast_parsed_float;
if (!p.truncated) {
- let n = stof32exact(p.mantissa: u32, p.exponent,
+ let n = stof32exact(p.mantissa, p.exponent,
p.negative);
if (n is f32) {
return n: f32;
@@ -666,5 +666,7 @@ export fn stof32(s: str) (f32 | invalid | overflow) = {
assert(math::isnan(stof32("NaN"): f32));
assert(math::isnan(stof32("nan"): f32));
assert(math::isnan(stof32("naN"): f32));
+ assert(stof32("9.19100241453305036800e+20")
+ == 9.19100241453305036800e+20);
};