-
Notifications
You must be signed in to change notification settings - Fork 165
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
Added support for i32, i64, f32 and f64's sqrt and cbrt #2571
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from math import (cbrt, sqrt) | ||
from lpython import f32, f64, i32, i64 | ||
|
||
eps: f64 | ||
eps = 1e-12 | ||
|
||
def test_cbrt(): | ||
eps: f64 = 1e-12 | ||
a : i32 = 64 | ||
b : i64 = i64(64) | ||
c : f32 = f32(64.0) | ||
d : f64 = f64(64.0) | ||
assert abs(cbrt(124.0) - 4.986630952238646) < eps | ||
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. We can also print the values before each assert so that it is helpful for debugging later (if the test case fails for some reason). 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. Ok, I'll follow this practice from now on. |
||
assert abs(cbrt(39.0) - 3.3912114430141664) < eps | ||
assert abs(cbrt(39) - 3.3912114430141664) < eps | ||
assert abs(cbrt(a) - 4.0) < eps | ||
assert abs(cbrt(b) - 4.0) < eps | ||
assert abs(cbrt(c) - 4.0) < eps | ||
assert abs(cbrt(d) - 4.0) < eps | ||
|
||
def test_sqrt(): | ||
eps: f64 = 1e-12 | ||
a : i32 = 64 | ||
b : i64 = i64(64) | ||
c : f32 = f32(64.0) | ||
d : f64 = f64(64.0) | ||
assert abs(sqrt(a) - 8.0) < eps | ||
assert abs(sqrt(b) - 8.0) < eps | ||
assert abs(sqrt(c) - 8.0) < eps | ||
assert abs(sqrt(d) - 8.0) < eps | ||
|
||
def check(): | ||
test_cbrt() | ||
test_sqrt() | ||
|
||
check() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -539,18 +539,74 @@ def trunc(x: f32) -> i32: | |
else: | ||
return ceil(x) | ||
|
||
@overload | ||
def sqrt(x: f32) -> f64: | ||
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. Should this return a It seems we might need some discussion here. I do not yet have an opinion about the type of the return value that we should return. @certik Do you have any ideas on this? 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. Similar to the return value of 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. I think we have to return f32, see the following the cases. >>> import numpy as n
>>> x = n.float32(3.14)
>>> type(x)
<class 'numpy.float32'>
>>> type(n.sqrt(x))
<class 'numpy.float32'>
>>> y = 3.14
>>> type(y)
<class 'float'>
>>> import math as m
>>> type(m.sqrt(y))
<class 'float'> If we move this implement to IntrinsicFunction in the future, this can be utilised by fortran as well, as it returns the same: https://gcc.gnu.org/onlinedocs/gfortran/SQRT.html |
||
""" | ||
Returns square root of a number x | ||
""" | ||
y : f64 | ||
y = f64(x) | ||
return y**(1/2) | ||
|
||
@overload | ||
def sqrt(x: f64) -> f64: | ||
""" | ||
Returns square root of a number x | ||
""" | ||
return x**(1/2) | ||
|
||
@overload | ||
def sqrt(x: i32) -> f64: | ||
""" | ||
Returns square root of a number x | ||
""" | ||
y : f64 | ||
y = float(x) | ||
return y**(1/2) | ||
|
||
@overload | ||
def sqrt(x: i64) -> f64: | ||
""" | ||
Returns square root of a number x | ||
""" | ||
y : f64 | ||
y = float(x) | ||
return y**(1/2) | ||
|
||
@overload | ||
def cbrt(x: f32) -> f64: | ||
""" | ||
Returns cube root of a number x | ||
""" | ||
y : f64 | ||
y = f64(x) | ||
return y**(1/3) | ||
|
||
@overload | ||
def cbrt(x: f64) -> f64: | ||
""" | ||
Returns cube root of a number x | ||
""" | ||
return x**(1/3) | ||
|
||
@overload | ||
def cbrt(x: i32) -> f64: | ||
""" | ||
Returns cube root of a number x | ||
""" | ||
y : f64 | ||
y = float(x) | ||
return y**(1/3) | ||
|
||
@overload | ||
def cbrt(x: i64) -> f64: | ||
""" | ||
Returns cube root of a number x | ||
""" | ||
y : f64 | ||
y = float(x) | ||
return y**(1/3) | ||
|
||
@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.
Why not enable CPython for this test? Does it fail with CPython?
I think a test should run with CPython so that we are sure that we are supporting and delivering the right syntax and runtime output.
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.
The test fails when I enable CPython. Here is the error:
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.
Is it because CPython does not have this implemented?
The math module has cbrt(), according to the official python documentation: https://docs.python.org/3/library/math.html.
Is this what I'm supposed to refer to? I couldn't find CPython documentation.
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.
I get the same error when I enable CPython for test_math_03 (which has already been merged).
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.
It seems
cbrt
in python is supported from version3.11
. I think we currently use python3.10
locally and at the CI. Hence, you get the above import error.I think it is fine for now to not enable the test for CPython.
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.
Ok. I face the same issue here too, please check: #2556