aboutsummaryrefslogtreecommitdiff
path: root/compat53.lua
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-01-16 17:07:02 +0100
committerPhilipp Janda <siffiejoe@gmx.net>2015-01-16 17:07:02 +0100
commit6124b7b5e687eff120507dccfe98b72b7ddb702d (patch)
tree8f84aecb8afcc4116bda5fff0c44cb3c7fa9daee /compat53.lua
parent1fd73399caa9a1ea24bd626e607980ac94debd64 (diff)
downloadlua-compat-5.3-6124b7b5e687eff120507dccfe98b72b7ddb702d.tar.gz
lua-compat-5.3-6124b7b5e687eff120507dccfe98b72b7ddb702d.tar.bz2
lua-compat-5.3-6124b7b5e687eff120507dccfe98b72b7ddb702d.zip
add new fields to math library
Diffstat (limited to 'compat53.lua')
-rw-r--r--compat53.lua77
1 files changed, 73 insertions, 4 deletions
diff --git a/compat53.lua b/compat53.lua
index 98e1a3d..0346cb7 100644
--- a/compat53.lua
+++ b/compat53.lua
@@ -3,10 +3,13 @@ local lua_version = _VERSION:sub(-3)
3if lua_version ~= "5.3" then 3if lua_version ~= "5.3" then
4 4
5 -- load utf8 library 5 -- load utf8 library
6 utf8 = require("compat53.utf8") 6 local ok, utf8lib = pcall(require, "compat53.utf8")
7 package.loaded["utf8"] = utf8 7 if ok then
8 if lua_version == "5.1" then 8 utf8 = utf8lib
9 utf8.charpattern = "[%z\1-\127\194-\244][\128-\191]*" 9 package.loaded["utf8"] = utf8lib
10 if lua_version == "5.1" then
11 utf8lib.charpattern = "[%z\1-\127\194-\244][\128-\191]*"
12 end
10 end 13 end
11 14
12 -- use Roberto's struct module for string packing/unpacking for now 15 -- use Roberto's struct module for string packing/unpacking for now
@@ -20,5 +23,71 @@ if lua_version ~= "5.3" then
20 string.unpack = struct.unpack 23 string.unpack = struct.unpack
21 end 24 end
22 25
26
27 -- update math library
28 do
29 local maxint, minint = 1, 0
30
31 while maxint+1 > maxint and 2*maxint > maxint do
32 maxint = maxint * 2
33 end
34 if 2*maxint <= maxint then
35 maxint = 2*maxint-1
36 minint = -maxint-1
37 else
38 maxint = maxint
39 minint = -maxint
40 end
41 math.maxinteger = maxint
42 math.mininteger = minint
43
44 local _type = type
45 function math.tointeger(n)
46 if _type(n) == "number" and n <= maxint and n >= minint and n % 1 == 0 then
47 return n
48 end
49 return nil
50 end
51
52 function math.type(n)
53 if _type(n) == "number" then
54 if n <= maxint and n >= minint and n % 1 == 0 then
55 return "integer"
56 else
57 return "float"
58 end
59 else
60 return nil
61 end
62 end
63
64 local _error = error
65 local function checkinteger(x, i, f)
66 local t = _type(x)
67 if t ~= "number" then
68 _error("bad argument #"..i.." to '"..f..
69 "' (number expected, got "..t..")", 0)
70 elseif x > maxint or x < minint or x % 1 ~= 0 then
71 _error("bad argument #"..i.." to '"..f..
72 "' (number has no integer representation)", 0)
73 else
74 return x
75 end
76 end
77
78 function math.ult(m, n)
79 m = checkinteger(m, "1", "math.ult")
80 n = checkinteger(n, "2", "math.ult")
81 if m >= 0 and n < 0 then
82 return true
83 elseif m < 0 and n >= 0 then
84 return false
85 else
86 return m < n
87 end
88 end
89 end
90
23end 91end
24 92
93-- vi: set expandtab softtabstop=3 shiftwidth=3 :