l-math.lua /size: 2555 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['l-math'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9if not math.ceiling then
10
11    math.ceiling = math.ceil
12
13end
14
15if not math.round then
16
17    local floor = math.floor
18
19    function math.round(x) return floor(x + 0.5) end
20
21end
22
23if not math.div then
24
25    local floor = math.floor
26
27    function math.div(n,m) return floor(n/m) end
28
29end
30
31if not math.mod then
32
33    function math.mod(n,m) return n % m end
34
35end
36
37if not math.sind then
38
39    local sin, cos, tan = math.sin, math.cos, math.tan
40
41    local pipi = 2*math.pi/360
42
43    function math.sind(d) return sin(d*pipi) end
44    function math.cosd(d) return cos(d*pipi) end
45    function math.tand(d) return tan(d*pipi) end
46
47end
48
49if not math.odd then
50
51    function math.odd (n) return n % 2 ~= 0 end
52    function math.even(n) return n % 2 == 0 end
53
54end
55
56if not math.cosh then
57
58    local exp = math.exp
59
60    function math.cosh(x)
61        local xx = exp(x)
62        return (xx+1/xx)/2
63    end
64    function math.sinh(x)
65        local xx = exp(x)
66        return (xx-1/xx)/2
67    end
68    function math.tanh(x)
69        local xx = exp(x)
70        return (xx-1/xx)/(xx+1/xx)
71    end
72
73end
74
75if not math.pow then
76
77    function math.pow(x,y)
78        return x^y
79    end
80
81end
82
83if not math.atan2 then
84
85    math.atan2 = math.atan
86
87end
88
89if not math.ldexp then
90
91    function math.ldexp(x,e)
92        return x * 2.0^e
93    end
94
95end
96
97-- if not math.frexp then
98--
99--     -- not a oneliner so use a math library instead
100--
101--     function math.frexp(x,e)
102--         -- returns m and e such that x = m2e, e is an integer and the absolute
103--         -- value of m is in the range [0.5, 1) (or zero when x is zero)
104--     end
105--
106-- end
107
108if not math.log10 then
109
110    local log = math.log
111
112    function math.log10(x)
113        return log(x,10)
114    end
115
116end
117
118if not math.type then
119
120    function math.type()
121        return "float"
122    end
123
124end
125
126if not math.tointeger then
127
128    math.mininteger = -0x4FFFFFFFFFFF
129    math.maxinteger =  0x4FFFFFFFFFFF
130
131    local floor = math.floor
132
133    function math.tointeger(n)
134        local f = floor(n)
135        return f == n and f or nil
136    end
137
138end
139
140if not math.ult then
141
142    local floor = math.floor
143
144    function math.tointeger(m,n)
145        -- not ok but i'm not motivated to look into it now
146        return floor(m) < floor(n) -- unsigned comparison needed
147    end
148
149end
150