diff --git a/integration_tests/test_math.py b/integration_tests/test_math.py index 28a13f91a6..f2258bbb51 100644 --- a/integration_tests/test_math.py +++ b/integration_tests/test_math.py @@ -1,4 +1,4 @@ -from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow, +from math import (factorial, isqrt, perm, comb, degrees, radians, exp, exp2, pow, ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc) from ltypes import i32, f64, i64 @@ -58,6 +58,15 @@ def test_exp(): i = exp(2.34) assert abs(i - 10.381236562731843) < eps +def test_exp2(): + i: f64 + i = exp2(2.3) + assert abs(i - 4.924577653379665) < eps + + # p: f64 + # p = exp2(0.0) + # assert exp2(p) == 1.0, f"assertion error in exp2" + # assert exp2(1) == 2, f"assertion error in exp2" def test_pow(): i: f64 @@ -151,6 +160,7 @@ def test_trunc(): def check(): + test_exp2() test_factorial_1() test_comb() test_isqrt() diff --git a/integration_tests/test_math_02.py b/integration_tests/test_math_02.py index 5dc5d4f618..9fa8edb484 100644 --- a/integration_tests/test_math_02.py +++ b/integration_tests/test_math_02.py @@ -1,7 +1,16 @@ -from math import (sin, cos, tan, pi, sqrt, log, log10, log2, erf, erfc, gamma, +from math import (sin, cos, tan, pi, sqrt, cbrt, log, log10, log2, erf, erfc, gamma, lgamma, asin, acos, atan, atan2, asinh, acosh, atanh, tanh, sinh, cosh, hypot, copysign) +from ltypes import i32, f64, i64 + +pi: f64 = 3.141592653589793238462643383279502884197 +e: f64 = 2.718281828459045235360287471352662497757 +tau: f64 = 6.283185307179586 + +eps: f64 +eps = 1e-12 + def test_trig(): eps: f64 = 1e-12 assert abs(sin(0.0)-0) < eps @@ -15,6 +24,13 @@ def test_trig(): assert abs(atan(1.0) - pi/4) < eps assert abs(atan2(1.0, 1.0) - pi/4) < eps + +def test_cbrt(): + eps: f64 = 1e-12 + assert abs(cbrt(8.0) - 2) < eps + assert abs(cbrt(1.0) - 1) < eps + assert abs(cbrt(-27.0) + 3) < eps + def test_sqrt(): eps: f64 = 1e-12 assert abs(sqrt(2.0) - 1.4142135623730951) < eps @@ -54,6 +70,7 @@ def test_hypot(): assert abs(hypot(6, 6) - 8.48528137423857) < eps def check(): + test_cbrt() test_trig() test_sqrt() test_log() diff --git a/src/runtime/math.py b/src/runtime/math.py index d01edb4fe5..dcbf36fabc 100644 --- a/src/runtime/math.py +++ b/src/runtime/math.py @@ -226,6 +226,11 @@ def exp(x: f64) -> f64: """ return e**x +def exp2(x: f64) -> f64: + """ + Return `2` raised to the power `x`. + """ + return 2**x def pow(x: f64, y: f64) -> f64: """ @@ -312,8 +317,18 @@ def trunc(x: f32) -> i32: return ceil(x) def sqrt(x: f64) -> f64: + """ + Returns square root of a non negative x + """ + return x**(1/2) +def cbrt(x: f64) -> f64: + """ + Returns cube root of a number x + """ + return x**(1/3) + @ccall def _lfortran_dsin(x: f64) -> f64: pass