Skip to content
Snippets Groups Projects
Commit cc20271c authored by Matthias Schiffer's avatar Matthias Schiffer
Browse files

gluon-status-page: avoid complex math

This code is usually running on an embedded CPU without FPU. In
addtition to its inefficience, the algorithm is also much harder to
understand.

Replace the logarithm formula with a simple loop.

(cherry picked from commit f2e0f7e3)
parent b03e1e28
No related branches found
No related tags found
No related merge requests found
...@@ -66,12 +66,17 @@ ...@@ -66,12 +66,17 @@
local function formatBits(bits) local function formatBits(bits)
local units = {[0]='', 'k', 'M', 'G'} local units = {[0]='', 'k', 'M', 'G'}
local unit = 0
for i = 1, #units do
if math.abs(bits) < 1000 then
break
end
unit = i
bits = bits / 1000
end
local pow = math.floor(math.log(math.max(math.abs(bits), 1)) / math.log(1000)) return string.format('%g %sbit', bits, units[unit])
local known_pow = math.min(pow, #units)
local significand = bits/(1000^known_pow)
return string.format('%g %sbit', significand, units[known_pow])
end end
local function statistics(key, format) local function statistics(key, format)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment