-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
79 lines (65 loc) · 1.53 KB
/
main.lua
File metadata and controls
79 lines (65 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
74
75
76
77
78
79
function get_eng_prefix(x,lst_sel)
-- This function associates the correct prefix to use
-- for the engineering notation of numbers, in
-- conjunction with the LaTeX package 'siunitx'.
-- INPUT:
-- - x, number to be processed
-- - lst_sel, Long/Short/Tex prefix SELector
--
-- bg @2016/07/05
-- define backslash for TeX output
local backslash = '\\'
-- ensure the functions are defined
function log10(x)
return math.log(x,10)
end
floor = math.floor
-- define prefixes
local prex = {'y','z','a','f','p',
'n','u','m','','k',
'M','G','T','P','E',
'Z','Y'}
local prefix = {'yocto',
'zepto',
'atto',
'femto',
'pico',
'nano',
'micro',
'milli',
'',
'kilo',
'mega',
'giga',
'tera',
'peta',
'exa',
'zetta',
'yotta'}
-- get integer part of the exponent multiple of 3, s.t.
-- x >= 10^(3t)
local t = floor(1/3*log10(x))
-- set maximum prefix
if t > 8 then
t = 8
end
-- set minimum prefix
if t < -8 then
t = -8
end
-- compute new number, x = y . 10^(3t)
local y = x/10^(3*t)
-- associate t to correct position
local pos = t + 9
if lst_sel == 'short' then
return y, prex[pos]
elseif lst_sel == 'tex' then
if t == 0 then
return y,''
else
return y, backslash .. prefix[pos]
end
else
return y, prefix[pos]
end
end