authorgravatar for 46078689+emar-kar@users.noreply.github.comLeo Emar-Kar <46078689+emar-kar@users.noreply.github.com> 2024-03-16 23:38:46+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-03-16 23:38:46+00:00
logf88a971e4ff211b78695609b4482fb886f30a1af
tree213230353738c5bb0471dcb955e3b57852d245d3
parent242ab81112c05fa815523551a6f612c5a12c52b2
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.fmt: fix incorrect rounding on 0 precision of a decimal


1 files changed, 9 insertions(+), 1 deletions(-)

lib/std/fmt/ryu128.zig+9-1
...@@ -2,6 +2,7 @@...@@ -2,6 +2,7 @@
2//! https://dl.acm.org/doi/pdf/10.1145/33605952//! https://dl.acm.org/doi/pdf/10.1145/3360595
33
4const std = @import("std");4const std = @import("std");
5const expectFmt = std.testing.expectFmt;
56
6const special_exponent = 0x7fffffff;7const special_exponent = 0x7fffffff;
78
...@@ -131,7 +132,7 @@ fn round(f: FloatDecimal128, mode: RoundMode, precision: usize) FloatDecimal128...@@ -131,7 +132,7 @@ fn round(f: FloatDecimal128, mode: RoundMode, precision: usize) FloatDecimal128
131132
132 switch (mode) {133 switch (mode) {
133 .decimal => {134 .decimal => {
134 if (f.exponent >= 0) {135 if (f.exponent > 0) {
135 round_digit = (olength - 1) + precision + @as(usize, @intCast(f.exponent));136 round_digit = (olength - 1) + precision + @as(usize, @intCast(f.exponent));
136 } else {137 } else {
137 const min_exp_required = @as(usize, @intCast(-f.exponent));138 const min_exp_required = @as(usize, @intCast(-f.exponent));
...@@ -1129,3 +1130,10 @@ test "format f128" {...@@ -1129,3 +1130,10 @@ test "format f128" {
1129 try check(f128, 9.409340012568248e18, "9.409340012568248e18");1130 try check(f128, 9.409340012568248e18, "9.409340012568248e18");
1130 try check(f128, 1.2345678, "1.2345678e0");1131 try check(f128, 1.2345678, "1.2345678e0");
1131}1132}
1133
1134test "format float to decimal with zero precision" {
1135 try expectFmt("5", "{d:.0}", .{5});
1136 try expectFmt("6", "{d:.0}", .{6});
1137 try expectFmt("7", "{d:.0}", .{7});
1138 try expectFmt("8", "{d:.0}", .{8});
1139}