Python Forum
looking for a math function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking for a math function
#1
there is "sqrt()" in math. is there a function to calculate cube roots?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
print(27**(1/3)) or print(math.cbrt(27)) if you are using Python 3.11
Skaperen likes this post
Reply
#3
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
Reply
#4
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!
Reply
#5
(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()?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
... as previously mentioned by deanhystad in message #2
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(Dec-07-2023, 11:24 PM)Skaperen Wrote: ... as previously mentioned by deanhystad in message #2
My mistake, didn't see it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
(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
>>> 
Skaperen likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020