Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions integration_tests/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,21 @@ def test_complex():
print(x.real)
print(x.imag)


def test_complex_abs():
x: c32
x = complex(3, 4)
eps: f64
eps = 1e-12
assert abs(abs(x) - 5.0) < eps
y: c64
y = complex(6, 8)
assert abs(abs(y) - 10.0) < eps


def check():
test_real_imag()
test_complex()
test_complex_abs()

check()
12 changes: 10 additions & 2 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ def abs(b: bool) -> i32:

@overload
def abs(c: c32) -> f32:
pass
a: f32
b: f32
a = c.real
b = _lfortran_caimag(c)
return (a**2 + b**2)**(1/2)

@overload
def abs(c: c64) -> f64:
pass
a: f64
b: f64
a = c.real
b = _lfortran_zaimag(c)
return (a**2 + b**2)**(1/2)


def str(x: i32) -> str:
Expand Down