summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/lualibs/lualibs-math.lua
blob: 6105bc3c2d7900ab4bddcd97a96004dabf9ad58f (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
if not modules then modules = { } end modules ['l-math'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

if not math.ceiling then

    math.ceiling = math.ceil

end

if not math.round then

    if xmath then

        math.round = xmath.round

    else

        local floor = math.floor

        function math.round(x)
            return x < 0 and -floor(-x + 0.5) or floor(x + 0.5)
        end

    end

end

if not math.div then

    local floor = math.floor

    function math.div(n,m) return floor(n/m) end

end

if not math.mod then

    function math.mod(n,m) return n % m end

end

if not math.sind then

    local sin, cos, tan = math.sin, math.cos, math.tan

    local pipi = 2*math.pi/360

    function math.sind(d) return sin(d*pipi) end
    function math.cosd(d) return cos(d*pipi) end
    function math.tand(d) return tan(d*pipi) end

end

if not math.odd then

    function math.odd (n) return n % 2 ~= 0 end
    function math.even(n) return n % 2 == 0 end

end

if not math.cosh then

    local exp = math.exp

    function math.cosh(x)
        local xx = exp(x)
        return (xx+1/xx)/2
    end
    function math.sinh(x)
        local xx = exp(x)
        return (xx-1/xx)/2
    end
    function math.tanh(x)
        local xx = exp(x)
        return (xx-1/xx)/(xx+1/xx)
    end

end

if not math.pow then

    function math.pow(x,y)
        return x^y
    end

end

if not math.atan2 then

    math.atan2 = math.atan

end

if not math.ldexp then

    function math.ldexp(x,e)
        return x * 2.0^e
    end

end

-- if not math.frexp then
--
--     -- not a oneliner so use a math library instead
--
--     function math.frexp(x,e)
--         -- returns m and e such that x = m2e, e is an integer and the absolute
--         -- value of m is in the range [0.5, 1) (or zero when x is zero)
--     end
--
-- end

if not math.log10 then

    local log = math.log

    function math.log10(x)
        return log(x,10)
    end

end

if not math.type then

    function math.type()
        return "float"
    end

end

if not math.tointeger then

    math.mininteger = -0x4FFFFFFFFFFF
    math.maxinteger =  0x4FFFFFFFFFFF

    local floor = math.floor

    function math.tointeger(n)
        local f = floor(n)
        return f == n and f or nil
    end

end

if not math.ult then

    local floor = math.floor

    function math.ult(m,n)
        -- not ok but i'm not motivated to look into it now
        return floor(m) < floor(n) -- unsigned comparison needed
    end

end