[Tutor] Little problem with math module

Kent Johnson kent37 at tds.net
Mon Apr 21 04:23:19 CEST 2008


Tiago Katcipis wrote:
> im not understanding why is this a problem...i have this simple function
> 
> def newton_divergente(x): 
>   return math.pow(x, 1.0/3.0)
> 
> but when x = -20 it returns this error
> 
> return math.pow(x, 1.0/3.0)
> ValueError: math domain error
> 
> but why is that? is it impossible to calculate -20 ^ (1/3) ?
> 
> here on my calculator i get the result -6,666666667, but python seens to
> dont now the answer, why? am i doing something wrong? =/

Your calculator is not doing so well either since

In [15]: -6.666666667**3
Out[15]: -296.29629634074081

but this seems to work:
In [13]: -20**(1./3.)
Out[13]: -2.7144176165949063

Kent


More information about the Tutor mailing list