l-math.lua /size: 2555 b    last modification: 2020-07-01 14:35
1
if
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 9
if
not
math
.
ceiling
then
10 11
math
.
ceiling
=
math
.
ceil
12 13
end
14 15
if
not
math
.
round
then
16 17
local
floor
=
math
.
floor
18 19
function
math
.
round
(
x
)
return
floor
(
x
+
0
.
5
)
end
20 21
end
22 23
if
not
math
.
div
then
24 25
local
floor
=
math
.
floor
26 27
function
math
.
div
(
n
,
m
)
return
floor
(
n
/
m
)
end
28 29
end
30 31
if
not
math
.
mod
then
32 33
function
math
.
mod
(
n
,
m
)
return
n
%
m
end
34 35
end
36 37
if
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 47
end
48 49
if
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 54
end
55 56
if
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 73
end
74 75
if
not
math
.
pow
then
76 77
function
math
.
pow
(
x
,
y
)
78
return
x
^
y
79
end
80 81
end
82 83
if
not
math
.
atan2
then
84 85
math
.
atan2
=
math
.
atan
86 87
end
88 89
if
not
math
.
ldexp
then
90 91
function
math
.
ldexp
(
x
,
e
)
92
return
x
*
2
.
0
^
e
93
end
94 95
end
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 108
if
not
math
.
log10
then
109 110
local
log
=
math
.
log
111 112
function
math
.
log10
(
x
)
113
return
log
(
x
,
10
)
114
end
115 116
end
117 118
if
not
math
.
type
then
119 120
function
math
.
type
(
)
121
return
"
float
"
122
end
123 124
end
125 126
if
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 138
end
139 140
if
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 149
end
150