Skip to content

cbrt and exp2 functions implemented #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
12 changes: 11 additions & 1 deletion integration_tests/test_math.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -151,6 +160,7 @@ def test_trunc():


def check():
test_exp2()
test_factorial_1()
test_comb()
test_isqrt()
Expand Down
19 changes: 18 additions & 1 deletion integration_tests/test_math_02.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this level of precision for testing? I think 4-5 decimal places is fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately these constants should be imported from the builtin math module.

Comment on lines +7 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please try removing these global variables and try running tests with local variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I'll do that


eps: f64
eps = 1e-12

def test_trig():
eps: f64 = 1e-12
assert abs(sin(0.0)-0) < eps
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these expressions actually work with Lpython?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, they should.


@ccall
def _lfortran_dsin(x: f64) -> f64:
pass
Expand Down