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..9a0f3959a6 --- /dev/null +++ b/integration_tests/test_math_02.py @@ -0,0 +1,20 @@ +from math import sin, cos, tan, pi, sqrt + +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 + +def test_sqrt(): + eps: f64 = 1e-12 + assert abs(sqrt(2.0) - 1.4142135623730951) < eps + assert abs(sqrt(9.0) - 3) < eps + +test_trig() +test_sqrt() diff --git a/src/runtime/math.py b/src/runtime/math.py index 2ee848857e..9cda71a852 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,27 @@ def lcm(a: i32, b: i32) -> i32: if a*b == 0: return 0 return (a*b)//gcd(a, b) + +def sqrt(x: f64) -> f64: + return x**(1/2) + +@ccall +def _lfortran_dsin(x: f64) -> f64: + pass + +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)