Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"test_builtin_int.py",
"test_builtin_float.py",
"test_math1.py",
"test_math_02.py",
"test_c_interop_01.py",
]

Expand Down
20 changes: 20 additions & 0 deletions integration_tests/test_math_02.py
Original file line number Diff line number Diff line change
@@ -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()
Comment thread
certik marked this conversation as resolved.
Resolved
test_sqrt()
26 changes: 25 additions & 1 deletion src/runtime/math.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ltypes import i32, f64
from ltypes import i32, f64, ccall


pi: f64 = 3.141592653589793238462643383279502884197
Expand Down Expand Up @@ -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)
Comment thread
namannimmo10 marked this conversation as resolved.
Resolved

@ccall
def _lfortran_dtan(x: f64) -> f64:
pass

def tan(x: f64) -> f64:
return _lfortran_dtan(x)