22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
return dsq / (dist^2)
-- [0,1) == less then
-- 1 == equal
-- >1 == greater than
end
-- produce an SI expression for a quantity
fn.si = function(unit, val, full, uncommonScales)
if val == 0 then return '0 ' .. unit end
local scales = {
{30, 'Q', 'quetta',true, 'q', 'quecto',true};
{27, 'R', 'ronna', true, 'r', 'ronto', true};
{24, 'Y', 'yotta', true, 'y', 'yocto', true};
{21, 'Z', 'zetta', true, 'z', 'zepto', true};
{18, 'E', 'exa', true, 'a', 'atto', true};
................................................................................
for i, s in ipairs(scales) do
local amt, smaj, pmaj, cmaj,
smin, pmin, cmin = lib.tbl.unpack(s)
if math.abs(val) > 1 then
if uncommonScales or cmaj then
local denom = 10^amt
if math.abs(val) >= (10^(amt)) then
return string.format("%s %s%s",
val / denom, (full and pmaj or smaj), unit)
end
end
elseif math.abs(val) < 1 then
if uncommonScales or cmin then
local denom = 10^-amt
if math.abs(val) <= (10^-(amt-1)) then
return string.format("%s %s%s",
val / denom, (full and pmin or smin), unit)
end
end
end
end
return string.format("%s %s", val, unit)
end
|
|
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
return dsq / (dist^2)
-- [0,1) == less then
-- 1 == equal
-- >1 == greater than
end
-- produce an SI expression for a quantity
fn.si = function(unit, val, full, uncommonScales, prec)
if val == 0 then return '0 ' .. unit end
local scales = {
{30, 'Q', 'quetta',true, 'q', 'quecto',true};
{27, 'R', 'ronna', true, 'r', 'ronto', true};
{24, 'Y', 'yotta', true, 'y', 'yocto', true};
{21, 'Z', 'zetta', true, 'z', 'zepto', true};
{18, 'E', 'exa', true, 'a', 'atto', true};
................................................................................
for i, s in ipairs(scales) do
local amt, smaj, pmaj, cmaj,
smin, pmin, cmin = lib.tbl.unpack(s)
if math.abs(val) > 1 then
if uncommonScales or cmaj then
local denom = 10^amt
local vd = val/denom
if prec then vd = lib.math.trim(vd, prec) end
if math.abs(val) >= (10^(amt)) then
return string.format("%s %s%s",
vd, (full and pmaj or smaj), unit)
end
end
elseif math.abs(val) < 1 then
if uncommonScales or cmin then
local denom = 10^-amt
local vd = val/denom
if prec then vd = lib.math.trim(vd, prec) end
if math.abs(val) <= (10^-(amt-1)) then
return string.format("%s %s%s",
vd, (full and pmin or smin), unit)
end
end
end
end
return string.format("%s %s", val, unit)
end
|