diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 9a0f3959a6..2b63a6ac0d 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,4 +1,6 @@ -from math import sin, cos, tan, pi, sqrt +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) def test_trig(): # TODO: importing from `math` doesn't work here yet: @@ -10,11 +12,41 @@ def test_trig(): assert abs(cos(pi/2)-0) < eps assert abs(tan(0.0)-0) < eps assert abs(tan(pi/4)-1) < eps + assert abs(asin(1.0) - pi/2) < eps + assert abs(acos(1.0) - 0) < eps + assert abs(atan(1.0) - pi/4) < eps + assert abs(atan2(1.0, 1.0) - pi/4) < eps def test_sqrt(): eps: f64 = 1e-12 assert abs(sqrt(2.0) - 1.4142135623730951) < eps assert abs(sqrt(9.0) - 3) < eps +def test_log(): + eps: f64 = 1e-12 + assert abs(log(1.0) - 0) < eps + assert abs(log(2.718281828459) - 1) < eps + assert abs(log2(2.0) - 1) < eps + assert abs(log10(10.0) - 1) < eps + +def test_special(): + eps: f64 = 1e-12 + assert abs(erf(2.0) - 0.9953222650189527) < eps + assert abs(erfc(2.0) - 0.0046777349810472645) < eps + assert abs(erf(2.0) + erfc(2.0) - 1) < eps + assert abs(gamma(5.0) - 24) < eps + assert abs(lgamma(5.0) - log(gamma(5.0))) < eps + +def test_hyperbolic(): + eps: f64 = 1e-12 + assert abs(sinh(1.0) - 1.1752011936438014) < eps + assert abs(cosh(1.0) - 1.5430806348152437) < eps + assert abs(tanh(1.0) - 0.7615941559557649) < eps + assert abs(asinh(1.0) - 0.881373587019543) < eps + assert abs(cosh(1.0) - 0) < eps + assert abs(tanh(0.0) - 0) < eps + test_trig() test_sqrt() +test_log() +test_special() diff --git a/src/runtime/math.py b/src/runtime/math.py index 9cda71a852..fcd594bf68 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -163,3 +163,118 @@ def _lfortran_dtan(x: f64) -> f64: def tan(x: f64) -> f64: return _lfortran_dtan(x) + +@ccall +def _lfortran_dlog(x: f64) -> f64: + pass + +def log(x: f64) -> f64: + return _lfortran_dlog(x) + +@ccall +def _lfortran_dlog10(x: f64) -> f64: + pass + +def log10(x: f64) -> f64: + return _lfortran_dlog10(x) + +def log2(x: f64) -> f64: + return _lfortran_dlog(x)/_lfortran_dlog(2.0) + +@ccall +def _lfortran_derf(x: f64) -> f64: + pass + +def erf(x: f64) -> f64: + return _lfortran_derf(x) + +@ccall +def _lfortran_derfc(x: f64) -> f64: + pass + +def erfc(x: f64) -> f64: + return _lfortran_derfc(x) + +@ccall +def _lfortran_dgamma(x: f64) -> f64: + pass + +def gamma(x: f64) -> f64: + return _lfortran_dgamma(x) + +@ccall +def _lfortran_dlog_gamma(x: f64) -> f64: + pass + +def lgamma(x: f64) -> f64: + return _lfortran_dlog_gamma(x) + +@ccall +def _lfortran_dasin(x: f64) -> f64: + pass + +def asin(x: f64) -> f64: + return _lfortran_dasin(x) + +@ccall +def _lfortran_dacos(x: f64) -> f64: + pass + +def acos(x: f64) -> f64: + return _lfortran_dacos(x) + +@ccall +def _lfortran_datan(x: f64) -> f64: + pass + +def atan(x: f64) -> f64: + return _lfortran_datan(x) + +@ccall +def _lfortran_datan2(y: f64, x: f64) -> f64: + pass + +def atan2(y: f64, x: f64) -> f64: + return _lfortran_datan2(y, x) + +@ccall +def _lfortran_dsinh(x: f64) -> f64: + pass + +def sinh(x: f64) -> f64: + return _lfortran_dsinh(x) + +@ccall +def _lfortran_dcosh(x: f64) -> f64: + pass + +def cosh(x: f64) -> f64: + return _lfortran_dcosh(x) + +@ccall +def _lfortran_dtanh(x: f64) -> f64: + pass + +def tanh(x: f64) -> f64: + return _lfortran_dtanh(x) + +@ccall +def _lfortran_dasinh(x: f64) -> f64: + pass + +def asinh(x: f64) -> f64: + return _lfortran_dasinh(x) + +@ccall +def _lfortran_dacosh(x: f64) -> f64: + pass + +def acosh(x: f64) -> f64: + return _lfortran_dacosh(x) + +@ccall +def _lfortran_datanh(x: f64) -> f64: + pass + +def atanh(x: f64) -> f64: + return _lfortran_datanh(x)