Skip to content

Commit

Permalink
fix comptime float formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tiehuis committed Mar 23, 2024
1 parent e90583f commit 017e68b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/std/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1839,6 +1839,8 @@ test comptimePrint {
try std.testing.expectEqualSlices(u8, "100", comptimePrint("{}", .{100}));
try std.testing.expectEqualStrings("30", comptimePrint("{d}", .{30.0}));
try std.testing.expectEqualStrings("30.0", comptimePrint("{d:3.1}", .{30.0}));
try std.testing.expectEqualStrings("0.05", comptimePrint("{d}", .{0.05}));
try std.testing.expectEqualStrings("5e-2", comptimePrint("{e}", .{0.05}));
}

test "parse u64 digit too big" {
Expand Down
4 changes: 2 additions & 2 deletions lib/std/fmt/format_float.zig
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ pub fn formatDecimal(buf: []u8, f_: FloatDecimal128, precision: ?usize) FormatEr

// fixed bound: leading_digit(1) + point(1)
const req_bytes = if (f.exponent >= 0)
2 + @abs(f.exponent) + olength + (precision orelse 0)
@as(usize, 2) + @abs(f.exponent) + olength + (precision orelse 0)
else
2 + @max(@abs(f.exponent) + olength, precision orelse 0);
@as(usize, 2) + @max(@abs(f.exponent) + olength, precision orelse 0);
if (buf.len < req_bytes) {
return error.BufferTooSmall;
}
Expand Down

0 comments on commit 017e68b

Please sign in to comment.