From 1bc92b1fde6e812de4a45ab3b2131d16151114d7 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Fri, 12 Jun 2020 15:50:44 +0300 Subject: [PATCH] Fix formatting of floating point values with the B and Bi specifiers --- lib/std/fmt.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 671f25a943..23066a6963 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -868,11 +868,14 @@ pub fn formatBytes( return out_stream.writeAll("0B"); } + const is_float = comptime std.meta.trait.is(.Float)(@TypeOf(value)); const mags_si = " kMGTPEZY"; const mags_iec = " KMGTPEZY"; + + const log2 = if (is_float) @floatToInt(usize, math.log2(value)) else math.log2(value); const magnitude = switch (radix) { - 1000 => math.min(math.log2(value) / comptime math.log2(1000), mags_si.len - 1), - 1024 => math.min(math.log2(value) / 10, mags_iec.len - 1), + 1000 => math.min(log2 / comptime math.log2(1000), mags_si.len - 1), + 1024 => math.min(log2 / 10, mags_iec.len - 1), else => unreachable, }; const new_value = lossyCast(f64, value) / math.pow(f64, lossyCast(f64, radix), lossyCast(f64, magnitude));