[Tutor] Little problem with math module

Alan Gauld alan.gauld at btinternet.com
Mon Apr 21 09:07:10 CEST 2008


"Tiago Katcipis" <katcipis at inf.ufsc.br> wrote 

> 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) ?

It may be your use of the pow() function:

>>> pow(20, 0.3333333)
2.7144173455393048
>>> pow(-20, 0.3333333)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> -20**0.3333333
-2.7144173455393048
>>>

So the exponentiation operator seems happy but pow isn't.

Very strange and potentially a bug in pow()? 
Unless someone knows a reason for it?

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list