From 163a8e98bc04ea955ee54d5905436ffac34c93a2 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Mon, 20 May 2019 17:28:17 +1200 Subject: [PATCH] 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. --- std/fmt/parse_float.zig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/std/fmt/parse_float.zig b/std/fmt/parse_float.zig index c13977dc4254bc1128f1b11c0e59a60ce174da72..51f3f2f0cb0014deaa4d6223e8e04a6ea8ceb8c0 100644 --- a/std/fmt/parse_float.zig +++ b/std/fmt/parse_float.zig @@ -103,7 +103,7 @@ fn convertRepr(comptime T: type, n: FloatRepr) T { s.d1 = @truncate(u32, n.mantissa >> 32); s.d2 = 0; - var binary_exponent: u64 = 92; + var binary_exponent: i32 = 92; var exp = n.exponent; while (exp > 0) : (exp -= 1) { @@ -161,7 +161,7 @@ fn convertRepr(comptime T: type, n: FloatRepr) T { } else if (binary_exponent < 1) { break :blk if (n.negative) f64_minus_zero else f64_plus_zero; } else if (s.d2 != 0) { - const binexs2 = u64(binary_exponent) << 52; + const binexs2 = @intCast(u64, binary_exponent) << 52; const rr = (u64(s.d2 & ~mask28) << 24) | ((u64(s.d1) + 128) >> 8) | binexs2; break :blk if (n.negative) rr | (1 << 63) else rr; } else { @@ -415,6 +415,7 @@ test "fmt.parseFloat" { if (T != f16) { expect(approxEq(T, try parseFloat(T, "123142.1"), 123142.1, epsilon)); expect(approxEq(T, try parseFloat(T, "-123142.1124"), T(-123142.1124), epsilon)); + expect(approxEq(T, try parseFloat(T, "0.7062146892655368"), T(0.7062146892655368), epsilon)); } } } -- 2.54.0