A. Johnson
A. Johnson

Reputation: 139

How to find the cube root in Python?

I've tried to find the cube root in Python but I have no idea how to find it. There was 1 line of code that worked but he wouldn't give me the full number. Example:

math.pow(64, 1/3)

This doesn't give me 4 tough but 3.99999. Does anyone know how I am supposed to fix this?

Upvotes: 13

Views: 27612

Answers (5)

Szabolcs
Szabolcs

Reputation: 4086

UPDATE: Since python 3.11 one can use math.cbrt for the most accurate results

Python >= 3.11:

>>> import math
>>> math.cbrt(8)
2.0

OLD ANSWER:

You can use the power operator ** with fractions like:

Python3:

>>> 8**(1/3)
2.0

Python2:

>>> 8**(1.0/3)
2.0

Upvotes: 14

qwr
qwr

Reputation: 10891

x**(1/3) won't return the real root for negative numbers, so use math.cbrt in python 3.11+ or a quick workaround:

from math import copysign

def real_cbrt(x):
    return copysign(abs(x)**(1/3), x)

Upvotes: 1

Max
Max

Reputation: 476

For an approximate result, x**(1/3) should be fine. If you know that x = n**3 is the cube of an integer n, then you can use round() to get the nearest integer which should be n up to n ~ 2**53 ~ 10**16. For larger numbers, the approach using floats will necessarily give wrong results. For example, x = 2**54+1 is odd, but round((x**3)**(1/3)) will give you an even number, which is certainly wrong.

You can write your own function using Newton's (a.k.a. the Babylonian) method to get the exact integer result for integers of arbitrary size. That means, you consider the sequence of approximations defined by x(k+1) = φ(x(k)) with φ(x) = x - f(x)/f'(x) = ((p-1)*x + n/x^(p-1))/p for f(x) = x^p - n whose zero is the p-th root of n. Thus, at each step you take the weighted average of the approximation x (with weight p-1) and n/x^(p-1) (which would be equal to x if x = n^(1/p)) with weight 1. (This should converge faster then the simple average corresponding to binary search.) You stop when the approximations are close enough. (Due to integer arithmetic, the weighted average may not always result in |a-b| <= 1 for p > 2, so the code below has some ugly terminating criteria/process (which can probably be improved) to ensure it always returns the expected result.).

def root(x: int, n: int = 3):
    "Return integer part of the n-th root (n > 1) of x > 0."
    p = n-1; a = x >> (x.bit_length()*p//n) # initial guess
    while True:
        b = x // a**p  # too small if a is too big and vice versa
        if abs(a-b) < n:
            if a < b: a = b       
            while a**n >= x: a -= 1  # ugly but safe
            return a
        a = (p*a + b) // n

Upvotes: 1

SuperNova
SuperNova

Reputation: 27456

in Python 3.11, math.cbrt

 x = 64
 math.cbrt(x)

(or)

use numpy

import numpy as np
x = 64
np.cbrt(x)

Upvotes: 11

Julio CamPlaz
Julio CamPlaz

Reputation: 917

This is one option without using math library

>>> 64**(1/3)
3.9999999999999996
>>> round(64**(1/3.),2)
4.0

If you want to do with your code, you can apply 'round()' method

>>>import math
>>>round(math.pow(64,1/3.))
4

Upvotes: 8

Related Questions