Dinosaurius
Dinosaurius

Reputation: 8628

Cannot resolve the error ValueError: math domain error

I have the following piece of code in my function:

if (p < 0):
    return 0.01   
return a * math.pow(p, b) * max(e, 1)

It gives me the error ValueError: math domain error in line return a * math.pow(p, b) * max(e, 1). I assumed that the source of this error is a negative value of p in math.pow(p,a), however after adding if block, it still gives me an error. Any help is highly appreciated.

Upvotes: 0

Views: 409

Answers (1)

Amir Schnell
Amir Schnell

Reputation: 651

the b in

math.pow(p,b)

must be an integer as supposed by the documentation:

math.pow(x, y): Return x raised to the power y. Exceptional cases follow Annex ‘F’ of the C99 standard as far as possible. In particular, pow(1.0, x) and pow(x, 0.0) always return 1.0, even when x is a zero or a NaN. If both x and y are finite, x is negative, and y is not an integer then pow(x, y) is undefined, and raises ValueError.

If this does not help try narrowing the problem down:

if (p < 0):
    return 0.01
tmp_pow = math.pow(p, b)
tmp_ max = max(e, 1)
return a * tmp_pow * tmp_max

Upvotes: 1

Related Questions