From 87415bfef674fd8a51b0e96ea597c78fecc09571 Mon Sep 17 00:00:00 2001 From: Kishan-Ved Date: Sun, 3 Mar 2024 15:18:40 +0530 Subject: [PATCH 1/4] Added Support for real and integer sqrt and cbrt --- integration_tests/test_math_04.py | 36 +++++++++++++++++++ src/runtime/math.py | 58 ++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 integration_tests/test_math_04.py diff --git a/integration_tests/test_math_04.py b/integration_tests/test_math_04.py new file mode 100644 index 0000000000..cf4f6d5974 --- /dev/null +++ b/integration_tests/test_math_04.py @@ -0,0 +1,36 @@ +from math import (cbrt, sqrt) +from lpython import f32, f64, i32, i64 + +eps: f64 +eps = 1e-12 + +def test_cbrt(): + eps: f64 = 1e-12 + a : i32 = 64 + b : i64 = i64(64) + c : f32 = f32(64.0) + d : f64 = f64(64.0) + assert abs(cbrt(124.0) - 4.986630952238646) < eps + assert abs(cbrt(39.0) - 3.3912114430141664) < eps + assert abs(cbrt(39) - 3.3912114430141664) < eps + assert abs(cbrt(a) - 4.0) < eps + assert abs(cbrt(b) - 4.0) < eps + assert abs(cbrt(c) - 4.0) < eps + assert abs(cbrt(d) - 4.0) < eps + +def test_sqrt(): + eps: f64 = 1e-12 + a : i32 = 64 + b : i64 = i64(64) + c : f32 = f32(64.0) + d : f64 = f64(64.0) + assert abs(sqrt(a) - 8.0) < eps + assert abs(sqrt(b) - 8.0) < eps + assert abs(sqrt(c) - 8.0) < eps + assert abs(sqrt(d) - 8.0) < eps + +def check(): + test_cbrt() + test_sqrt() + +check() \ No newline at end of file diff --git a/src/runtime/math.py b/src/runtime/math.py index 8655201892..245a6a3b64 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -539,18 +539,74 @@ def trunc(x: f32) -> i32: else: return ceil(x) +@overload +def sqrt(x: f32) -> f64: + """ + Returns cube root of a number x + """ + y : f64 + y = f64(x) + return y**(1/2) + +@overload def sqrt(x: f64) -> f64: """ - Returns square root of a number x + Returns cube root of a number x """ return x**(1/2) +@overload +def sqrt(x: i32) -> f64: + """ + Returns cube root of a number x + """ + y : f64 + y = float(x) + return y**(1/2) + +@overload +def sqrt(x: i64) -> f64: + """ + Returns cube root of a number x + """ + y : f64 + y = float(x) + return y**(1/2) + +@overload +def cbrt(x: f32) -> f64: + """ + Returns cube root of a number x + """ + y : f64 + y = f64(x) + return y**(1/3) + +@overload def cbrt(x: f64) -> f64: """ Returns cube root of a number x """ return x**(1/3) +@overload +def cbrt(x: i32) -> f64: + """ + Returns cube root of a number x + """ + y : f64 + y = float(x) + return y**(1/3) + +@overload +def cbrt(x: i64) -> f64: + """ + Returns cube root of a number x + """ + y : f64 + y = float(x) + return y**(1/3) + @ccall def _lfortran_dsin(x: f64) -> f64: pass From 6b11066c69a447b059c6a9cf592389faf4b1f525 Mon Sep 17 00:00:00 2001 From: Kishan-Ved Date: Sun, 3 Mar 2024 15:23:16 +0530 Subject: [PATCH 2/4] updated --- src/runtime/math.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/runtime/math.py b/src/runtime/math.py index 245a6a3b64..086130b8c0 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -542,7 +542,7 @@ def trunc(x: f32) -> i32: @overload def sqrt(x: f32) -> f64: """ - Returns cube root of a number x + Returns sqrt root of a number x """ y : f64 y = f64(x) @@ -551,14 +551,14 @@ def sqrt(x: f32) -> f64: @overload def sqrt(x: f64) -> f64: """ - Returns cube root of a number x + Returns sqrt root of a number x """ return x**(1/2) @overload def sqrt(x: i32) -> f64: """ - Returns cube root of a number x + Returns sqrt root of a number x """ y : f64 y = float(x) @@ -567,7 +567,7 @@ def sqrt(x: i32) -> f64: @overload def sqrt(x: i64) -> f64: """ - Returns cube root of a number x + Returns sqrt root of a number x """ y : f64 y = float(x) From f5c8b3d51a53a4f7fd084e86d244f851a02468e2 Mon Sep 17 00:00:00 2001 From: Kishan-Ved Date: Sun, 3 Mar 2024 15:24:12 +0530 Subject: [PATCH 3/4] updated --- src/runtime/math.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/runtime/math.py b/src/runtime/math.py index 086130b8c0..df8a6e2139 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -542,7 +542,7 @@ def trunc(x: f32) -> i32: @overload def sqrt(x: f32) -> f64: """ - Returns sqrt root of a number x + Returns square root of a number x """ y : f64 y = f64(x) @@ -551,14 +551,14 @@ def sqrt(x: f32) -> f64: @overload def sqrt(x: f64) -> f64: """ - Returns sqrt root of a number x + Returns square root of a number x """ return x**(1/2) @overload def sqrt(x: i32) -> f64: """ - Returns sqrt root of a number x + Returns square root of a number x """ y : f64 y = float(x) @@ -567,7 +567,7 @@ def sqrt(x: i32) -> f64: @overload def sqrt(x: i64) -> f64: """ - Returns sqrt root of a number x + Returns square root of a number x """ y : f64 y = float(x) From 2e347314bd8c0aba32a27a90d935fb7e95bc0f6f Mon Sep 17 00:00:00 2001 From: Kishan-Ved Date: Sun, 3 Mar 2024 15:35:44 +0530 Subject: [PATCH 4/4] added integration test --- integration_tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 1177bb5266..ede6d6b69c 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -626,6 +626,7 @@ RUN(NAME test_builtin_sum LABELS cpython llvm c) RUN(NAME test_math1 LABELS cpython llvm c) RUN(NAME test_math_02 LABELS cpython llvm NOFAST) RUN(NAME test_math_03 LABELS llvm) #1595: TODO: Test using CPython (3.11 recommended) +RUN(NAME test_math_04 LABELS llvm) #TODO: Test using CPython (as above) RUN(NAME test_pass_compare LABELS cpython llvm c) RUN(NAME test_c_interop_01 LABELS cpython llvm c) RUN(NAME test_c_interop_02 LABELS cpython llvm c