From a351bd56a89c6b03a7aa06d21be8debf8d1ebac6 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Sun, 6 Mar 2022 02:08:24 +0530 Subject: [PATCH 1/4] Write the test for `math.copysign` --- integration_tests/test_math.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index f2edc32efc..a9b0fef59e 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -84,6 +84,18 @@ def test_lcm(): assert i == 84 +def test_copysign(): + f: f64 + f = copysign(4, -1) + assert f == -4.0 + f = copysign(-8, 97.21) + assert f == 8.0 + f = copysign(0, -1) + assert f == -0.0 + f = copysign(-43, -76) + assert f == -43.0 + + test_factorial_1() test_comb() test_isqrt() @@ -96,3 +108,4 @@ def test_lcm(): test_ldexp() test_gcd() test_lcm() +test_copysign() From 0cb4588cdc2e561e69577a9b00d2a407ca28514e Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Sun, 6 Mar 2022 02:15:27 +0530 Subject: [PATCH 2/4] Implement `math.copysign` --- integration_tests/test_math.py | 12 ++++-------- src/runtime/math.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index a9b0fef59e..e3a9aa508f 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,5 +1,5 @@ from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow, - ldexp, fabs, gcd, lcm) + ldexp, fabs, gcd, lcm, copysign) from ltypes import i32, f64 @@ -86,13 +86,9 @@ def test_lcm(): def test_copysign(): f: f64 - f = copysign(4, -1) - assert f == -4.0 - f = copysign(-8, 97.21) - assert f == 8.0 - f = copysign(0, -1) - assert f == -0.0 - f = copysign(-43, -76) + f = copysign(-8.56, 97.21) + assert f == 8.56 + f = copysign(-43.0, -76.67) assert f == -43.0 diff --git a/src/runtime/math.py b/src/runtime/math.py index fcd594bf68..9cf60b035a 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -140,6 +140,17 @@ def lcm(a: i32, b: i32) -> i32: return 0 return (a*b)//gcd(a, b) + +def copysign(x: f64, y: f64) -> f64: + """ + Return `x` with the sign of `y`. + """ + if y > 0.0 or (y == 0.0 and atan2(y, -1.0) > 0.0): + return fabs(x) + else: + return -fabs(x) + + def sqrt(x: f64) -> f64: return x**(1/2) From d1aecc459a87427bfc0ce12bc4b95a4cd0eae606 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Sun, 6 Mar 2022 02:31:28 +0530 Subject: [PATCH 3/4] Write the test for `math.hypot` --- integration_tests/test_math.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index e3a9aa508f..06b7674a46 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,5 +1,5 @@ from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow, - ldexp, fabs, gcd, lcm, copysign) + ldexp, fabs, gcd, lcm, copysign, hypot) from ltypes import i32, f64 @@ -92,6 +92,16 @@ def test_copysign(): assert f == -43.0 +def test_hypot(): + f: f64 + f = hypot(3, 4) + assert f == 5.0 + f = hypot(-3, 4) + assert f == 5.0 + f = hypot(6, 6) + assert f == 8.485281374238571 + + test_factorial_1() test_comb() test_isqrt() @@ -105,3 +115,4 @@ def test_copysign(): test_gcd() test_lcm() test_copysign() +test_hypot() From d88919c5314b850993ff147ac797e798e94ded7f Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Sun, 6 Mar 2022 02:45:53 +0530 Subject: [PATCH 4/4] Implement `math.hypot`; modify tests --- integration_tests/test_math.py | 22 +--------------------- integration_tests/test_math_02.py | 15 ++++++++++++++- src/runtime/math.py | 7 +++++++ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 06b7674a46..f2edc32efc 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,5 +1,5 @@ from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow, - ldexp, fabs, gcd, lcm, copysign, hypot) + ldexp, fabs, gcd, lcm) from ltypes import i32, f64 @@ -84,24 +84,6 @@ def test_lcm(): assert i == 84 -def test_copysign(): - f: f64 - f = copysign(-8.56, 97.21) - assert f == 8.56 - f = copysign(-43.0, -76.67) - assert f == -43.0 - - -def test_hypot(): - f: f64 - f = hypot(3, 4) - assert f == 5.0 - f = hypot(-3, 4) - assert f == 5.0 - f = hypot(6, 6) - assert f == 8.485281374238571 - - test_factorial_1() test_comb() test_isqrt() @@ -114,5 +96,3 @@ def test_hypot(): test_ldexp() test_gcd() test_lcm() -test_copysign() -test_hypot() diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 2b63a6ac0d..fc8f1af35e 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,6 +1,6 @@ from math import (sin, cos, tan, pi, sqrt, log, log10, log2, erf, erfc, gamma, lgamma, asin, acos, atan, atan2, asinh, acosh, atanh, - tanh, sinh, cosh) + tanh, sinh, cosh, hypot, copysign) def test_trig(): # TODO: importing from `math` doesn't work here yet: @@ -46,7 +46,20 @@ def test_hyperbolic(): assert abs(cosh(1.0) - 0) < eps assert abs(tanh(0.0) - 0) < eps +def test_copysign(): + eps: f64 = 1e-12 + assert abs(copysign(-8.56, 97.21) - 8.56) < eps + assert abs(copysign(-43.0, -76.67) - (-43.0)) < eps + +def test_hypot(): + eps: f64 = 1e-12 + assert abs(hypot(3, 4) - 5.0) < eps + assert abs(hypot(-3, 4) - 5.0) < eps + assert abs(hypot(6, 6) - 8.48528137423857) < eps + test_trig() test_sqrt() test_log() test_special() +test_copysign() +test_hypot() diff --git a/src/runtime/math.py b/src/runtime/math.py index 9cf60b035a..8ebf4d8a74 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -151,6 +151,13 @@ def copysign(x: f64, y: f64) -> f64: return -fabs(x) +def hypot(x: i32, y: i32) -> f64: + """ + Returns the hypotenuse of the right triangle with sides `x` and `y`. + """ + return sqrt(1.0*(x**2 + y**2)) + + def sqrt(x: f64) -> f64: return x**(1/2)