| 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 |
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 |
|
| 31 |
end |
| 32 |
|
| 33 |
if not math.div then |
| 34 |
|
| 35 |
local floor = math.floor |
| 36 |
|
| 37 |
function math.div(n,m) return floor(n/m) end |
| 38 |
|
| 39 |
end |
| 40 |
|
| 41 |
if not math.mod then |
| 42 |
|
| 43 |
function math.mod(n,m) return n % m end |
| 44 |
|
| 45 |
end |
| 46 |
|
| 47 |
if 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 |
|
| 57 |
end |
| 58 |
|
| 59 |
if 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 |
|
| 64 |
end |
| 65 |
|
| 66 |
if 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 |
|
| 83 |
end |
| 84 |
|
| 85 |
if not math.pow then |
| 86 |
|
| 87 |
function math.pow(x,y) |
| 88 |
return x^y |
| 89 |
end |
| 90 |
|
| 91 |
end |
| 92 |
|
| 93 |
if not math.atan2 then |
| 94 |
|
| 95 |
math.atan2 = math.atan |
| 96 |
|
| 97 |
end |
| 98 |
|
| 99 |
if not math.ldexp then |
| 100 |
|
| 101 |
function math.ldexp(x,e) |
| 102 |
return x * 2.0^e |
| 103 |
end |
| 104 |
|
| 105 |
end |
| 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 |
|
| 118 |
if not math.log10 then |
| 119 |
|
| 120 |
local log = math.log |
| 121 |
|
| 122 |
function math.log10(x) |
| 123 |
return log(x,10) |
| 124 |
end |
| 125 |
|
| 126 |
end |
| 127 |
|
| 128 |
if not math.type then |
| 129 |
|
| 130 |
function math.type() |
| 131 |
return "float" |
| 132 |
end |
| 133 |
|
| 134 |
end |
| 135 |
|
| 136 |
if 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 |
|
| 148 |
end |
| 149 |
|
| 150 |
if 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 |
|
| 159 |
end |