Skip to content

Commit 163a8e9

Browse files
committed
std.fmt.parse_float: Fix exponent calculation
This was incorrectly translated as a u64. binary_exponent is an unadjusted value so can be negative. In becomes unconditionally positive when adding the bias.
1 parent 1c73c08 commit 163a8e9

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

std/fmt/parse_float.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn convertRepr(comptime T: type, n: FloatRepr) T {
103103
s.d1 = @truncate(u32, n.mantissa >> 32);
104104
s.d2 = 0;
105105

106-
var binary_exponent: u64 = 92;
106+
var binary_exponent: i32 = 92;
107107
var exp = n.exponent;
108108

109109
while (exp > 0) : (exp -= 1) {
@@ -161,7 +161,7 @@ fn convertRepr(comptime T: type, n: FloatRepr) T {
161161
} else if (binary_exponent < 1) {
162162
break :blk if (n.negative) f64_minus_zero else f64_plus_zero;
163163
} else if (s.d2 != 0) {
164-
const binexs2 = u64(binary_exponent) << 52;
164+
const binexs2 = @intCast(u64, binary_exponent) << 52;
165165
const rr = (u64(s.d2 & ~mask28) << 24) | ((u64(s.d1) + 128) >> 8) | binexs2;
166166
break :blk if (n.negative) rr | (1 << 63) else rr;
167167
} else {
@@ -415,6 +415,7 @@ test "fmt.parseFloat" {
415415
if (T != f16) {
416416
expect(approxEq(T, try parseFloat(T, "123142.1"), 123142.1, epsilon));
417417
expect(approxEq(T, try parseFloat(T, "-123142.1124"), T(-123142.1124), epsilon));
418+
expect(approxEq(T, try parseFloat(T, "0.7062146892655368"), T(0.7062146892655368), epsilon));
418419
}
419420
}
420421
}

0 commit comments

Comments
 (0)