-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Add math.cbrt() function: Cube Root #88523
Comments
See also bpo-27353. |
+1. This is part of C99, so if it's also supported by Visual Studio, then this seems like a no-brainer. If it's _not_ also supported by Visual Studio, or if there are implementations that have serious numerical problems (as was the case with fma) we'll need to write our own fallback implementation, which is less simple. In principle it should be relatively easy for a math library to provide a correctly-rounded cbrt. |
BTW, to forestall questions about adding cmath.cbrt: it's not obvious how to extend the real cube root function to a complex cube root, so it might make sense to wait for actual use-cases before doing so. The issue is that the most natural way to define a complex cube root would use a branch cut along the negative real axis (the same branch cut that cmath.sqrt and cmath.log use). But then the principal branch would *not* be an extension of math.cbrt: it would return non-real values on the negative real axis. (The *real* cube roots of negative numbers would fall on the two non-principal branches.) See for example https://math.stackexchange.com/questions/71775/extending-the-cube-root-function-to-mathbbc |
When I needed a function to find cube root, I couldn't find one in the math module. Also found the |
I am surprised that this was not proposed before. Perhaps because it is so easy to write x**(1/3), and if you want a real root of negative argument, it is -(-x)**(1/3). It can be slightly less accurate that the result of C library function cbrt(), but good enough for any practical applications. Ajith, was you aware of x**(1/3)? |
Yes I was aware of x**(1/3) and used that. I just thought it would be useful if it was present in math module as it is also used in many situations like |
I consider And not only is that correct spelling unwieldy, but it still doesn't do the right thing for Of course, even with |
There's also a decent chance that a libm implementation of cbrt will be correctly rounded, while |
I didn't know the "cbrt" function name. It seems like it exists in the libc, but also in numpy. So it's a good idea to reuse this name ;-) numpy.cbrt(): Return the cube-root of an array, element-wise. |
@victor: Yep, the name is pretty standard. Not just C, but JavaScript, Java, R, and likely a lot of other languages that I haven't checked. |
If we *really* wanted to bikeshed on the name, back in 1991 Kahan wrote:
Source: https://csclub.uwaterloo.ca/~pbarfuss/qbrt.pdf But that was 30 years ago, and I think the "CBRT"-shaped ship has long since sailed. |
All done; closing. Thank you for the contribution! |
math.cbrt()
function: Cube Root #26622Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: