From 55aeeed310ec1e3386d593ba47e86bc3b575eb65 Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 24 Mar 2022 19:02:46 +0530 Subject: [PATCH 1/2] Implement abs for complex --- src/runtime/lpython_builtin.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/runtime/lpython_builtin.py b/src/runtime/lpython_builtin.py index c08ec8b686..fd17dcb247 100644 --- a/src/runtime/lpython_builtin.py +++ b/src/runtime/lpython_builtin.py @@ -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: From fd095ea21b596b7ead5dbbf8c4da1e45c5bb448e Mon Sep 17 00:00:00 2001 From: Smit-create Date: Thu, 24 Mar 2022 19:03:00 +0530 Subject: [PATCH 2/2] add a test for abs with complex --- integration_tests/test_complex.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/integration_tests/test_complex.py b/integration_tests/test_complex.py index ab79484541..31962e7460 100644 --- a/integration_tests/test_complex.py +++ b/integration_tests/test_complex.py @@ -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()