From 658e5046a48d4740e57e8062478e6dc2786ca673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Sun, 27 Feb 2022 23:00:01 -0700 Subject: [PATCH 1/3] Implement sin(x) --- integration_tests/run_tests.py | 1 + integration_tests/test_math_02.py | 10 ++++++++++ src/runtime/math.py | 10 +++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 integration_tests/test_math_02.py diff --git a/integration_tests/run_tests.py b/integration_tests/run_tests.py index 7a52662c45..0fd3ba4af3 100755 --- a/integration_tests/run_tests.py +++ b/integration_tests/run_tests.py @@ -21,6 +21,7 @@ "test_builtin_int.py", "test_builtin_float.py", "test_math1.py", + "test_math_02.py", "test_c_interop_01.py", ] diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py new file mode 100644 index 0000000000..3ffaef1636 --- /dev/null +++ b/integration_tests/test_math_02.py @@ -0,0 +1,10 @@ +from math import sin, pi + +def test_sin(): + # TODO: importing from `math` doesn't work here yet: + pi: f64 = 3.141592653589793238462643383279502884197 + eps: f64 = 1e-12 + assert abs(sin(0.0)-0) < eps + assert abs(sin(pi/2)-1) < eps + +test_sin() diff --git a/src/runtime/math.py b/src/runtime/math.py index 2ee848857e..bf3105a23d 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -1,4 +1,4 @@ -from ltypes import i32, f64 +from ltypes import i32, f64, ccall pi: f64 = 3.141592653589793238462643383279502884197 @@ -139,3 +139,11 @@ def lcm(a: i32, b: i32) -> i32: if a*b == 0: return 0 return (a*b)//gcd(a, b) + + +@ccall +def _lfortran_dsin(x: f64) -> f64: + pass + +def sin(x: f64) -> f64: + return _lfortran_dsin(x) From 805eef56d316e1d371d8e0f2a247d2dc7346d82a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Sun, 27 Feb 2022 23:07:29 -0700 Subject: [PATCH 2/3] Add cos, tan, sqrt --- integration_tests/test_math_02.py | 15 ++++++++++++--- src/runtime/math.py | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 3ffaef1636..40d460d598 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,10 +1,19 @@ -from math import sin, pi +from math import sin, cos, tan, pi, sqrt -def test_sin(): +def test_trig(): # TODO: importing from `math` doesn't work here yet: pi: f64 = 3.141592653589793238462643383279502884197 eps: f64 = 1e-12 assert abs(sin(0.0)-0) < eps assert abs(sin(pi/2)-1) < eps + assert abs(cos(0.0)-1) < eps + assert abs(cos(pi/2)-0) < eps + assert abs(tan(0.0)-0) < eps + assert abs(tan(pi/4)-1) < eps -test_sin() +def test_sqrt(): + eps: f64 = 1e-12 + assert abs(sqrt(2.0) - 1.4142135623730951) < eps + assert abs(sqrt(9.0) - 3) < eps + +test_trig() diff --git a/src/runtime/math.py b/src/runtime/math.py index bf3105a23d..9cda71a852 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -140,6 +140,8 @@ def lcm(a: i32, b: i32) -> i32: return 0 return (a*b)//gcd(a, b) +def sqrt(x: f64) -> f64: + return x**(1/2) @ccall def _lfortran_dsin(x: f64) -> f64: @@ -147,3 +149,17 @@ def _lfortran_dsin(x: f64) -> f64: def sin(x: f64) -> f64: return _lfortran_dsin(x) + +@ccall +def _lfortran_dcos(x: f64) -> f64: + pass + +def cos(x: f64) -> f64: + return _lfortran_dcos(x) + +@ccall +def _lfortran_dtan(x: f64) -> f64: + pass + +def tan(x: f64) -> f64: + return _lfortran_dtan(x) From ada81033dd5a217125515a1b8b0d57a9fc54d063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C4=8Cert=C3=ADk?= Date: Sun, 27 Feb 2022 23:22:44 -0700 Subject: [PATCH 3/3] Update integration_tests/test_math_02.py Co-authored-by: Smit Lunagariya --- integration_tests/test_math_02.py | 1 + 1 file changed, 1 insertion(+) diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 40d460d598..9a0f3959a6 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -17,3 +17,4 @@ def test_sqrt(): assert abs(sqrt(9.0) - 3) < eps test_trig() +test_sqrt()