-
Notifications
You must be signed in to change notification settings - Fork 170
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
Changes from all commits
8c41b91
41551aa
d245c55
26b85a1
5307d98
139b2cf
27e36fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
Comment on lines
+7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these expressions actually work with Lpython? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, they should. |
||
|
||
@ccall | ||
def _lfortran_dsin(x: f64) -> f64: | ||
pass | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.