Software /
code /
prosody
Annotate
util/human/units.lua @ 10889:25e0ec11b4e4
util.human.units: Put math functions into locals
Primarily because the next commit will deal with math.log behaving
differently on Lua 5.1 and that's eaiser with locals.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 03 Jun 2020 20:16:00 +0200 |
parent | 10888:42a0d9089de9 |
child | 10890:a451f80d1cea |
rev | line source |
---|---|
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
1 local math_abs = math.abs; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
2 local math_ceil = math.ceil; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
3 local math_floor = math.floor; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
4 local math_log = math.log; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
5 local math_max = math.max; |
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
6 local math_min = math.min; |
10888
42a0d9089de9
util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10886
diff
changeset
|
7 local unpack = table.unpack or unpack; --luacheck: ignore 113 |
42a0d9089de9
util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10886
diff
changeset
|
8 |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local large = { |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 "k", 1000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 "M", 1000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 "G", 1000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 "T", 1000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 "P", 1000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 "E", 1000000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 "Z", 1000000000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 "Y", 1000000000000000000000000, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 } |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local small = { |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 "m", 0.001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 "μ", 0.000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 "n", 0.000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 "p", 0.000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 "f", 0.000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 "a", 0.000000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 "z", 0.000000000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 "y", 0.000000000000000000000001, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 } |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 local binary = { |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 "Ki", 2^10, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 "Mi", 2^20, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 "Gi", 2^30, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 "Ti", 2^40, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 "Pi", 2^50, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 "Ei", 2^60, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 "Zi", 2^70, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 "Yi", 2^80, |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 } |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 -- n: number, the number to format |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 -- unit: string, the base unit |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 -- b: optional enum 'b', thousands base |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 local function format(n, unit, b) --> string |
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
45 local round = math_floor; |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 local prefixes = large; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 local logbase = 1000; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 local fmt = "%.3g %s%s"; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 if n == 0 then |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 return fmt:format(n, "", unit); |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 if b == 'b' then |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 prefixes = binary; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 logbase = 1024; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 elseif n < 1 then |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 prefixes = small; |
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
57 round = math_ceil; |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 end |
10889
25e0ec11b4e4
util.human.units: Put math functions into locals
Kim Alvefur <zash@zash.se>
parents:
10888
diff
changeset
|
59 local m = math_max(0, math_min(8, round(math_abs(math_log(math_abs(n), logbase))))); |
10888
42a0d9089de9
util.human.units: Handle location of unpack() in Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
10886
diff
changeset
|
60 local prefix, multiplier = unpack(prefixes, m * 2-1, m*2); |
10886
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 return fmt:format(n / (multiplier or 1), prefix or "", unit); |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 end |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 return { |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 format = format; |
994c4a333199
util.human.units: A library for formatting numbers with SI units
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 }; |