-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathex09.lua
35 lines (32 loc) · 1.1 KB
/
ex09.lua
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
-- Example 9. Define, load, and use a Lua module.
-- File "trig.lua".
local M = {}
-- Radians mode.
M.rad = {
sin = math.sin,
cos = math.cos,
tan = math.tan,
}
-- Degrees mode.
M.deg = {
sin = function(x) return math.sin(math.rad(x)) end,
cos = function(x) return math.cos(math.rad(x)) end,
tan = function(x) return math.tan(math.rad(x)) end
}
--8<----------------------------------------------------------------------------
if false then
--8<----------------------------------------------------------------------------
return M
--8<----------------------------------------------------------------------------
end
package.loaded['trig'] = M
--8<----------------------------------------------------------------------------
-- Program code.
local trig = require("trig").deg
trig.sin(30) -- results in 0.5
trig = require("trig").rad
trig.sin(math.pi / 6) -- also results in 0.5
--8<----------------------------------------------------------------------------
print(require('trig').deg.sin(30))
print(require('trig').rad.sin(math.pi / 6))
--8<----------------------------------------------------------------------------