Skip to content

Commit 975eb1f

Browse files
authored
Merge pull request #1527 from Shaikh-Ubaid/wasm_complex_nums3
WASM: Initial support for complex numbers
2 parents 8bd6143 + 573d806 commit 975eb1f

26 files changed

+1172
-1664
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ RUN(NAME bindc_06 LABELS llvm c
343343
EXTRAFILES bindc_06b.c)
344344
RUN(NAME test_generics_01 LABELS cpython llvm c)
345345
RUN(NAME test_cmath LABELS cpython llvm c)
346-
RUN(NAME test_complex LABELS cpython llvm c)
346+
RUN(NAME test_complex_01 LABELS cpython llvm c wasm)
347+
RUN(NAME test_complex_02 LABELS cpython llvm c)
347348
RUN(NAME test_max_min LABELS cpython llvm c)
348349
RUN(NAME test_global LABELS cpython llvm c)
349350
RUN(NAME test_global_decl LABELS cpython llvm c)

integration_tests/test_complex.py

Lines changed: 0 additions & 142 deletions
This file was deleted.

integration_tests/test_complex_01.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from ltypes import i32, i64, f32, f64, c32, c64
2+
3+
def test_real_imag():
4+
x: c64
5+
x = c64(2) + 3j
6+
a: f64
7+
b: f64
8+
eps: f64
9+
eps = 1e-12
10+
a = x.real
11+
b = x.imag
12+
assert abs(a - 2.0) <= eps
13+
assert abs(b - 3.0) <= eps
14+
15+
def test_complex():
16+
x: c64
17+
x = complex(4.5, 6.7)
18+
eps: f64
19+
eps = 1e-12
20+
assert abs(x.real - 4.5) <= eps
21+
assert abs(x.imag - 6.7) <= eps
22+
23+
x = complex(-4, 2)
24+
assert abs(x.real - (-4.0)) <= eps
25+
assert abs(x.imag - 2.0) <= eps
26+
27+
x = complex(4, 7.89)
28+
assert abs(x.real - 4.0) <= eps
29+
assert abs(x.imag - 7.89) <= eps
30+
31+
x = complex(5.6, 0)
32+
assert abs(x.real - 5.6) <= eps
33+
assert abs(x.imag - 0.0) <= eps
34+
35+
a: f64
36+
a = 534.6
37+
x = complex(a, -a) # (f64, f64)
38+
39+
assert abs(x.real - 534.60000000000002274) <= eps
40+
assert abs(x.imag - (-534.60000000000002274)) <= eps
41+
42+
a2: f32
43+
a2 = -f32(423.5430806348152437)
44+
a3: f32
45+
a3 = f32(34.5)
46+
x2: c32
47+
x2 = c32(complex(a2, a3)) # (f32, f32)
48+
49+
assert f64(abs(x2.imag - f32(34.5))) <= eps
50+
51+
i1: i32
52+
i1 = -5
53+
i2: i64
54+
i2 = -i64(6)
55+
56+
x = complex(a3, a) # (f32, f64)
57+
x = complex(a, a3) # (f64, f32)
58+
x = complex(i1, i2) # (i32, i64)
59+
x = complex(i1, -i1) # (i32, i32)
60+
x = complex(-i2, -i2) # (i64, i64)
61+
x = complex(i2, -i1) # (i64, i32)
62+
63+
def test_complex_unary_minus():
64+
c: c32
65+
c = c32(complex(3, 4.5))
66+
_c: c32
67+
_c = -c
68+
assert abs(f64(_c.real) - (-3.0)) <= 1e-12
69+
assert abs(f64(_c.imag) - (-4.5)) <= 1e-12
70+
_c = c32(complex(5, -78))
71+
_c = -_c
72+
assert abs(f64(_c.real) - (-5.0)) <= 1e-12
73+
assert abs(f64(_c.imag) - 78.0) <= 1e-12
74+
c2: c64
75+
c2 = complex(-4.5, -7.8)
76+
c2 = -c2
77+
assert abs(c2.real - 4.5) <= 1e-12
78+
assert abs(c2.imag - 7.8) <= 1e-12
79+
c2 = c64(3) + 4j
80+
c2 = -c2
81+
assert abs(c2.real - (-3.0)) <= 1e-12
82+
assert abs(c2.imag - (-4.0)) <= 1e-12
83+
84+
def test_complex_not():
85+
c: c32
86+
c = c32(complex(4, 5))
87+
b: bool
88+
b = not c
89+
assert not b
90+
91+
c2: c64
92+
c2 = complex(0, 0)
93+
b = not c2
94+
assert b
95+
96+
def check():
97+
test_real_imag()
98+
test_complex()
99+
test_complex_unary_minus()
100+
test_complex_not()
101+
102+
check()

integration_tests/test_complex_02.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from ltypes import f64, c32, c64
2+
3+
def test_complex_abs():
4+
x: c32
5+
x = c32(complex(3, 4))
6+
eps: f64
7+
eps = 1e-12
8+
assert f64(abs(f64(abs(x)) - 5.0)) < eps
9+
y: c64
10+
y = complex(6, 8)
11+
assert abs(abs(y) - 10.0) < eps
12+
13+
def test_complex_binop_32():
14+
x: c32
15+
y: c32
16+
z: c32
17+
x = c32(c64(2) + 3j)
18+
y = c32(c64(4) + 5j)
19+
z = x + y
20+
z = x - y
21+
z = x * y
22+
# TODO:
23+
#z = x / y
24+
z = x ** y
25+
26+
def test_complex_binop_64():
27+
x: c64
28+
y: c64
29+
z: c64
30+
x = c64(2) + 3j
31+
y = c64(4) + 5j
32+
z = x + y
33+
z = x - y
34+
z = x * y
35+
# TODO:
36+
#z = x / y
37+
z = x ** y
38+
39+
def check():
40+
test_complex_abs()
41+
test_complex_binop_32()
42+
test_complex_binop_64()
43+
44+
check()

0 commit comments

Comments
 (0)