Python Forum
looking for a math function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: looking for a math function (/thread-41236.html)

Pages: 1 2


looking for a math function - Skaperen - Dec-04-2023

there is "sqrt()" in math. is there a function to calculate cube roots?


RE: looking for a math function - deanhystad - Dec-04-2023

print(27**(1/3)) or print(math.cbrt(27)) if you are using Python 3.11


RE: looking for a math function - gulshan212 - Dec-05-2023

Yes, there is a function called pow(), which you can use to calculate cube rootes.
For example:
import math

# Calculate cube root
number = 8
cube_root = math.pow(number, 1/3)

print(f"The cube root of {number} is: {cube_root}")
Thanks


RE: looking for a math function - EdwardMatthew - Dec-06-2023

Embrace the sqrt() function in math for calculating cube roots, breaking free from the square-root norm. Just as languages vary, transcend the linguistic stereotype by celebrating your unique multilingual flair!


RE: looking for a math function - Skaperen - Dec-07-2023

(Dec-06-2023, 02:07 PM)EdwardMatthew Wrote: Embrace the sqrt() function in math for calculating cube roots, breaking free from the square-root norm. Just as languages vary, transcend the linguistic stereotype by celebrating your unique multilingual flair!
what are you talking about? i there a way to accurately, precisely, and efficiently, get the cube root by calling only sqrt()?


RE: looking for a math function - buran - Dec-07-2023

There is math.cbrt(x) - Return the cube root of x.

New in 3.11

https://docs.python.org/3/library/math.html#math.cbrt


RE: looking for a math function - Skaperen - Dec-07-2023

... as previously mentioned by deanhystad in message #2


RE: looking for a math function - Skaperen - Dec-07-2023

the other ways to calculate roots tend to be less accurate when powers involved (such as 1/3) cannot be represented exactly. maybe someone can develop a math.powroot() function.


RE: looking for a math function - buran - Dec-08-2023

(Dec-07-2023, 11:24 PM)Skaperen Wrote: ... as previously mentioned by deanhystad in message #2
My mistake, didn't see it


RE: looking for a math function - Gribouillis - Dec-08-2023

(Dec-07-2023, 06:41 PM)buran Wrote: There is math.cbrt(x) - Return the cube root of x.
If I understand well, the problem with **(1/3) is for negative real numbers. Instead of returning the negative real cubic root, it return the principal complex cubic root
>>> (-1)**(1/3)
(0.5000000000000001+0.8660254037844386j)
>>> 
Before 3.11 one can also use numpy.cbrt()
>>> numpy.cbrt(-1)
-1.0
>>> 
There is also a rounding issue
>>> 64**(1/3)
3.9999999999999996
>>> numpy.cbrt(64)
4.0
>>>