l-math.lua /size: 2679 b    last modification: 2023-12-21 09:44
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    if xmath then
18
19        math.round = xmath.round
20
21    else
22
23        local floor = math.floor
24
25        function math.round(x)
26            return x < 0 and -floor(-x + 0.5) or floor(x + 0.5)
27        end
28
29    end
30
31end
32
33if not math.div then
34
35    local floor = math.floor
36
37    function math.div(n,m) return floor(n/m) end
38
39end
40
41if not math.mod then
42
43    function math.mod(n,m) return n % m end
44
45end
46
47if not math.sind then
48
49    local sin, cos, tan = math.sin, math.cos, math.tan
50
51    local pipi = 2*math.pi/360
52
53    function math.sind(d) return sin(d*pipi) end
54    function math.cosd(d) return cos(d*pipi) end
55    function math.tand(d) return tan(d*pipi) end
56
57end
58
59if not math.odd then
60
61    function math.odd (n) return n % 2 ~= 0 end
62    function math.even(n) return n % 2 == 0 end
63
64end
65
66if not math.cosh then
67
68    local exp = math.exp
69
70    function math.cosh(x)
71        local xx = exp(x)
72        return (xx+1/xx)/2
73    end
74    function math.sinh(x)
75        local xx = exp(x)
76        return (xx-1/xx)/2
77    end
78    function math.tanh(x)
79        local xx = exp(x)
80        return (xx-1/xx)/(xx+1/xx)
81    end
82
83end
84
85if not math.pow then
86
87    function math.pow(x,y)
88        return x^y
89    end
90
91end
92
93if not math.atan2 then
94
95    math.atan2 = math.atan
96
97end
98
99if not math.ldexp then
100
101    function math.ldexp(x,e)
102        return x * 2.0^e
103    end
104
105end
106
107-- if not math.frexp then
108--
109--     -- not a oneliner so use a math library instead
110--
111--     function math.frexp(x,e)
112--         -- returns m and e such that x = m2e, e is an integer and the absolute
113--         -- value of m is in the range [0.5, 1) (or zero when x is zero)
114--     end
115--
116-- end
117
118if not math.log10 then
119
120    local log = math.log
121
122    function math.log10(x)
123        return log(x,10)
124    end
125
126end
127
128if not math.type then
129
130    function math.type()
131        return "float"
132    end
133
134end
135
136if not math.tointeger then
137
138    math.mininteger = -0x4FFFFFFFFFFF
139    math.maxinteger =  0x4FFFFFFFFFFF
140
141    local floor = math.floor
142
143    function math.tointeger(n)
144        local f = floor(n)
145        return f == n and f or nil
146    end
147
148end
149
150if not math.ult then
151
152    local floor = math.floor
153
154    function math.ult(m,n)
155        -- not ok but i'm not motivated to look into it now
156        return floor(m) < floor(n) -- unsigned comparison needed
157    end
158
159end
160