Repository navigation
Expand file tree
/
Copy pathcmp_typed.lua
More file actions
42 lines (33 loc) · 1.49 KB
/
Copy pathcmp_typed.lua
File metadata and controls
42 lines (33 loc) · 1.49 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
-- Numeric comparison specialization (LUA2WASM_OPT_INT): same-type int/float
-- compares lower to iXX/fXX ops. Must match reference Lua with flag on or off.
-- all six operators, integer, as values
local a, b = 3, 7
print(a < b, a <= b, a > b, a >= b, a == b, a ~= b) -- T T F F F T
print(b < a, b <= a, a == 3, a ~= 3) -- F F T F
-- in conditions (if / while / repeat)
local function sgn(x)
if x < 0 then return -1 elseif x > 0 then return 1 else return 0 end
end
print(sgn(-9), sgn(0), sgn(9)) -- -1 0 1
local i, acc = 0, 0
while i < 5 do acc = acc + i; i = i + 1 end
print(acc) -- 10
local j = 0
repeat j = j + 1 until j >= 3
print(j) -- 3
-- float comparisons
local x, y = 2.5, 2.5
print(x == y, x < 3.0, x > 3.0, x ~= 1.5) -- T T F T
local fc = 0
for k = 1, 100 do if (k * 0.5) <= 10.0 then fc = fc + 1 end end
print(fc) -- 20
-- NaN: every comparison with NaN is false except ~=
local nan = 0/0
print(nan == nan, nan ~= nan, nan < 1.0, nan > 1.0) -- F T F F
-- int vs float equality (mixed -> generic path, must stay exact)
print(2 == 2.0, 2.0 == 2, 3 ~= 3.0) -- T T F
-- comparison result stored and reused
local big = (10 > 3)
print(big, type(big)) -- true boolean
-- comparison feeding boolean ops
print(1 < 2 and 3 < 4, 1 > 2 or 5 > 4) -- T T