aboutsummaryrefslogtreecommitdiff
path: root/examples/complex.lua
blob: 62d6d0e8f027f2515ba4dbeef685664b0d691d33 (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
local P = {}
if _REQUIREDNAME == nil then
  complex = P
else
  _G[_REQUIREDNAME] = P
end

-- imports
-- local sqrt = math.sqrt

-- no more external access after this point
setfenv(1, P)

function new (r, i) return {r=r, i=i} end

i = new(0, 1)

function add(c1, c2)
  return new(c1.r + c2.r, c1.i + c2.i)
end

function sub(c1, c2)
  return new(c1.r - c2.r, c1.i - c2.i)
end

function mul(c1, c2)
  return new(c1.r * c2.r - c1.i * c2.i, c1.r * c2.i + c1.i * c2.r)
end

return P