Cubic root of the negative number on python
Question: 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?


Answer: A simple use of [De Moivre's
formula](http://en.wikipedia.org/wiki/De%5FMoivre%27s%5Fformula), 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 ]
    

