forked from lua-stdlib/lua-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.lua
More file actions
88 lines (61 loc) · 1.95 KB
/
math.lua
File metadata and controls
88 lines (61 loc) · 1.95 KB
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
--[[--
Additions to the core math module.
The module table returned by `std.math` also contains all of the entries from
the core math table. An hygienic way to import this module, then, is simply
to override the core `math` locally:
local math = require "std.math"
@corelibrary std.math
]]
local std = require "std.base"
local M
local _floor = math.floor
local function floor (n, p)
if p and p ~= 0 then
local e = 10 ^ p
return _floor (n * e) / e
else
return _floor (n)
end
end
local function monkey_patch (namespace)
namespace = namespace or _G
namespace.math = std.base.copy (namespace.math or {}, M)
return M
end
local function round (n, p)
local e = 10 ^ (p or 0)
return _floor (n * e + 0.5) / e
end
--[[ ================= ]]--
--[[ Public Interface. ]]--
--[[ ================= ]]--
local function X (decl, fn)
return require "std.debug".argscheck ("std.math." .. decl, fn)
end
M = {
--- Core Functions
-- @section corefuncs
--- Extend `math.floor` to take the number of decimal places.
-- @function floor
-- @number n number
-- @int[opt=0] p number of decimal places to truncate to
-- @treturn number `n` truncated to `p` decimal places
-- @usage tenths = floor (magnitude, 1)
floor = X ("floor (number, ?int)", floor),
--- Round a number to a given number of decimal places
-- @function round
-- @number n number
-- @int[opt=0] p number of decimal places to round to
-- @treturn number `n` rounded to `p` decimal places
-- @usage roughly = round (exactly, 2)
round = X ("round (number, ?int)", round),
--- Module Functions
-- @section modulefuncs
--- Overwrite core `math` methods with `std` enhanced versions.
-- @function monkey_patch
-- @tparam[opt=_G] table namespace where to install global functions
-- @treturn table the module table
-- @usage require "std.math".monkey_patch ()
monkey_patch = X ("monkey_patch (?table)", monkey_patch),
}
return std.base.merge (M, math)