authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-06-12 15:50:44+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-12 13:38:12-04:00
log1bc92b1fde6e812de4a45ab3b2131d16151114d7
tree87c1da4124fe1fe324a726a60e3c290048a3d1ed
parent57f1ed5325eeb3c3ca62af62a104f840881248f0

Fix formatting of floating point values with the B and Bi specifiers


1 files changed, 5 insertions(+), 2 deletions(-)

lib/std/fmt.zig+5-2
......@@ -868,11 +868,14 @@ pub fn formatBytes(
868868 return out_stream.writeAll("0B");
869869 }
870870
871 const is_float = comptime std.meta.trait.is(.Float)(@TypeOf(value));
871872 const mags_si = " kMGTPEZY";
872873 const mags_iec = " KMGTPEZY";
874
875 const log2 = if (is_float) @floatToInt(usize, math.log2(value)) else math.log2(value);
873876 const magnitude = switch (radix) {
874 1000 => math.min(math.log2(value) / comptime math.log2(1000), mags_si.len - 1),
875 1024 => math.min(math.log2(value) / 10, mags_iec.len - 1),
877 1000 => math.min(log2 / comptime math.log2(1000), mags_si.len - 1),
878 1024 => math.min(log2 / 10, mags_iec.len - 1),
876879 else => unreachable,
877880 };
878881 const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude));