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

This commit is contained in:
Alexandros Naskos
2020-06-12 15:50:44 +03:00
committed by Andrew Kelley
parent 57f1ed5325
commit 1bc92b1fde

View File

@@ -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));