Cubic root of the negative number on python

python cube root
binary search cube root python
python cube root of large number
find cube root of a number
find cube root of a number in java
perfect cube in python
numpy cube root

Can someone help me to find a solution on how to calculate a cubic root of the negative number using python?

>>> math.pow(-3, float(1)/3)
nan

it does not work. Cubic root of the negative number is negative number. Any solutions?


You could use:

-math.pow(3, float(1)/3)

Or more generally:

if x > 0:
    return math.pow(x, float(1)/3)
elif x < 0:
    return -math.pow(abs(x), float(1)/3)
else:
    return 0

How to get the real cube root of a negative number in Python3 , Quite recently I've encountered a weird issue. I tried to calculate a cubic root from a negative number in Python: $ python3 Python 3.5.3 (default,  In this tutorial, we are going to learn how to find the cube root of a number in Python. Let us understand with an example. Suppose a number stored in a variable. We can find the cube root of 125 using a trick : As we know that the cube root of 125 is 5. So it will return 5. If we want to find the cube root of a negative integer.


A simple use of De Moivre's formula, is sufficient to show that the cube root of a value, regardless of sign, is a multi-valued function. That means, for any input value, there will be three solutions. Most of the solutions presented to far only return the principle root. A solution that returns all valid roots, and explicitly tests for non-complex special cases, is shown below.

import numpy
import math
def cuberoot( z ):
    z = complex(z)
    x = z.real
    y = z.imag
    mag = abs(z)
    arg = math.atan2(y,x)
    return [ mag**(1./3) * numpy.exp( 1j*(arg+2*n*math.pi)/3 ) for n in range(1,4) ]

Edit: As requested, in cases where it is inappropriate to have dependency on numpy, the following code does the same thing.

def cuberoot( z ):
    z = complex(z) 
    x = z.real
    y = z.imag
    mag = abs(z)
    arg = math.atan2(y,x)
    resMag = mag**(1./3)
    resArg = [ (arg+2*math.pi*n)/3. for n in range(1,4) ]
    return [  resMag*(math.cos(a) + math.sin(a)*1j) for a in resArg ]

Cubic root from negative number in Python, -8 has three cube roots: −2, 1+i√3 and 1−i√3. So you can't answer the question "Is 3√−8 real" without specifying which of them you're talking about. For some  This takes the cube root of x, rounds it to the nearest integer, raises to the third power, and finally checks whether the result equals x. The reason to take the absolute value is to make the code work correctly for negative numbers across Python versions (Python 2 and 3 treat raising negative numbers to fractional powers differently).


math.pow(abs(x),float(1)/3) * (1,-1)[x<0]

cubic root of negative numbers, Can someone help me to find a solution on how to calculate a cubic root of the negative number using python? >>> math.pow(-3, float(1)/3) nan. Write a Python Program to Calculate the Cube of a Number using Arithmetic Operators and Functions with an example. This Python program allows users to enter any numeric value. Next, Python finds a Cube of that number using an Arithmetic Operator. This python cube number example is the same as above, but here, we are using the Exponent operator.


Taking the earlier answers and making it into a one-liner:

import math
def cubic_root(x):
    return math.copysign(math.pow(abs(x), 1.0/3.0), x)

Cubic root of the negative number on python, When the exponent is odd (like 1 or 3), the result is negative. When exponent is even (like 2), the result is positive. Cubes always involve multiplying a number by​  The cubic root of a negative number is just the negative of the cubic root of the absolute value of that number. i.e. x^(1/3) for x < 0 is the same as (-1)*(|x|)^(1/3) Just make your number positive, and then perform cubic root.


You can get the complete (all n roots) and more general (any sign, any power) solution using:

import cmath

x, t = -3., 3  # x**(1/t)

a = cmath.exp((1./t)*cmath.log(x))
p = cmath.exp(1j*2*cmath.pi*(1./t))

r = [a*(p**i) for i in range(t)]

Explanation: a is using the equation xu = exp(u*log(x)). This solution will then be one of the roots, and to get the others, rotate it in the complex plane by a (full rotation)/t.

Finding Cube Roots of Negative Numbers, Yes, you can. Explanation: Example: Evaluate 3√−125. We can write −125 as the product of 3 negative 5's. Hence, 3√−125=−5. The reason  Use this calculator to find the cube root of positive or negative numbers. Given a number x, the cube root of x is a number a such that a3 = x. If x positive a will be positive, if x is negative a will be negative. Cube roots is a specialized form of our common radicals calculator. The cube root of x is the same as x raised to the 1/3 power.


Can you find the cube root of a negative number? If so, is it positive , Given a number n, find the cube root of n. The main steps of our algorithm for calculating the cubic root of a number n are Python 3 program to find cubic root​. The main steps of our algorithm for calculating the cubic root of a number n are: Initialize start = 0 and end = n. Calculate mid = (start + end)/2. Check if the absolute value of (n – mid*mid*mid) < e. If this condition holds true then mid is our answer so return mid. If (mid*mid*mid)>n then set end=mid.


Worked example: Cube root of a negative number (video), In mathematics, a square root of a number x is a number y such that y2 = x; in other words, Square roots of negative numbers can be discussed within the framework of complex numbers. with sqrt (often pronounced "squirt") being common, used in C, C++, and derived languages like JavaScript, PHP, and Python. Instead, the square root of a negative number would need to be complex, which is outside the scope of the Python square root function. Square Roots in the Real World. To see a real-world application of the Python square root function, let’s turn to the sport of tennis.


Find cubic root of a number, In real world cube root for a negative number should exist: cuberoot(-1)=-1, that means (-1)*(-1)*(-1)=-1 or cuberoot(-27)=-3, that means (-3)*(-3)*(-3)=-27 But when I calculate cube root of a negative number in C using pow function, I get nan (not a number)