From 25b2ddf2fd172daf83cbac4b894b18ade9c409bb Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Sat, 23 Jul 2022 23:51:54 -0400 Subject: [PATCH] ENH simplify formatting math --- src/format.lua | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/format.lua b/src/format.lua index 9e91967..9834ef7 100644 --- a/src/format.lua +++ b/src/format.lua @@ -2,6 +2,7 @@ local M = {} local __tostring = tostring local __math_floor = math.floor +local __math_log = math.log local __math_ceil = math.ceil local __string_format = string.format @@ -23,21 +24,18 @@ M.round_to_string = function(x, places) end end +-- ASSUME domain is (0, inf) M.precision_round_to_string = function(x, sig_fig) - sig_fig = sig_fig or 4 - if x < 10 then return M.round_to_string(x, sig_fig - 1) - elseif x < 100 then return M.round_to_string(x, sig_fig - 2) - elseif x < 1000 then return M.round_to_string(x, sig_fig - 3) - else return M.round_to_string(x, sig_fig - 4) - end + local adj = x == 0 and 1 or __math_floor(__math_log(x, 10)) + 1 + return M.round_to_string(x, (sig_fig or 4) - adj) end +local PREFIXES = {'', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi'} + +-- ASSUME domain is (0, inf) M.convert_data_val = function(x) - if x < 1024 then return '', x - elseif x < 1048576 then return 'Ki', x / 1024 - elseif x < 1073741824 then return 'Mi', x / 1048576 - else return 'Gi', x / 1073741824 - end + local z = x == 0 and 0 or __math_floor(__math_log(x, 2) / 10) + return PREFIXES[z + 1], x / 2 ^ (10 * z) end return M