Skip to content

Commit 017e68b

Browse files
committed
fix comptime float formatting
Closes ziglang#19379 Closes ziglang#18046
1 parent e90583f commit 017e68b

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

lib/std/fmt.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,8 @@ test comptimePrint {
18391839
try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
18401840
try std.testing.expectEqualStrings("30", comptimePrint("{d}", .{30.0}));
18411841
try std.testing.expectEqualStrings("30.0", comptimePrint("{d:3.1}", .{30.0}));
1842+
try std.testing.expectEqualStrings("0.05", comptimePrint("{d}", .{0.05}));
1843+
try std.testing.expectEqualStrings("5e-2", comptimePrint("{e}", .{0.05}));
18421844
}
18431845

18441846
test "parse u64 digit too big" {

lib/std/fmt/format_float.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ pub fn formatDecimal(buf: []u8, f_: FloatDecimal128, precision: ?usize) FormatEr
270270

271271
// fixed bound: leading_digit(1) + point(1)
272272
const req_bytes = if (f.exponent >= 0)
273-
2 + @abs(f.exponent) + olength + (precision orelse 0)
273+
@as(usize, 2) + @abs(f.exponent) + olength + (precision orelse 0)
274274
else
275-
2 + @max(@abs(f.exponent) + olength, precision orelse 0);
275+
@as(usize, 2) + @max(@abs(f.exponent) + olength, precision orelse 0);
276276
if (buf.len < req_bytes) {
277277
return error.BufferTooSmall;
278278
}

0 commit comments

Comments
 (0)