Skip to content

Commit 1e9cf05

Browse files
authored
Merge pull request #276 from Smit-create/i-230
Implement abs for complex
2 parents 13be384 + fd095ea commit 1e9cf05

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

integration_tests/test_complex.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,21 @@ def test_complex():
4040
print(x.real)
4141
print(x.imag)
4242

43+
44+
def test_complex_abs():
45+
x: c32
46+
x = complex(3, 4)
47+
eps: f64
48+
eps = 1e-12
49+
assert abs(abs(x) - 5.0) < eps
50+
y: c64
51+
y = complex(6, 8)
52+
assert abs(abs(y) - 10.0) < eps
53+
54+
4355
def check():
4456
test_real_imag()
4557
test_complex()
58+
test_complex_abs()
4659

4760
check()

src/runtime/lpython_builtin.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ def abs(b: bool) -> i32:
6464

6565
@overload
6666
def abs(c: c32) -> f32:
67-
pass
67+
a: f32
68+
b: f32
69+
a = c.real
70+
b = _lfortran_caimag(c)
71+
return (a**2 + b**2)**(1/2)
6872

6973
@overload
7074
def abs(c: c64) -> f64:
71-
pass
75+
a: f64
76+
b: f64
77+
a = c.real
78+
b = _lfortran_zaimag(c)
79+
return (a**2 + b**2)**(1/2)
7280

7381

7482
def str(x: i32) -> str:

0 commit comments

Comments
 (0)