AppDividend
Latest Code Tutorials

How to Solve ValueError: math domain error in Python

0

You come across a special ValueError when working with Python’s math module. It is ValueError: math domain error. Python raises this error when you try to do something that is not mathematically possible or mathematically defined.

Python math domain error

The math domain error occurs when you’ve passed an undefined input into the math function. For example, you are doing a log of a number that is less than or equal to zero. That’s mathematically undefined, so Python’s log function raises an exception.

from math import log

print(log(-1))

Output

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3, in <module>
    print(log(-1))
ValueError: math domain error

And we get the ValueError: math domain error.

Whenever you are getting the math domain error for either reason: either you are trying to use a negative number inside the log function or a zero value.

Logarithms decide the base after being given a number and the power it was raised to.

The log(0) means that something raised to the power of 2 is 0. An exponent can never result in 0*, which means that log(0) has no answer, thus throwing the math domain error.

The domain of a function is the set of all possible input values. If Python throws the ValueError: math domain error, you’ve passed an undefined input into the math function.

To resolve this math domain error in Python, passing a valid input for which the function can calculate a numerical output.

In our case, don’t calculate the log of a negative number or zero, and it will resolve the error.

There are various scenarios in which this error can occur. Let’s see some of them one by one.

Python sqrt: Math domain error

To calculate the square root of a number in Python, use math.sqrt() method.

The math domain error appears if you pass a negative argument into the math.sqrt() function.

It’s mathematically impossible to calculate the square root of a negative number without using complex numbers.

from math import sqrt

print(sqrt(-1))

Output

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3, in <module>
    print(sqrt(-1))
ValueError: math domain error

The square root of a negative number is not mathematically possible. That’s why it throws an error.

Python pow: Math domain error

The math domain error for the math.pow(a,b) function to calculate a**b arises if you pass the negative base value into it and try to calculate a negative power of it.

from math import pow

print(pow(-1, 0.5))

Output

Traceback (most recent call last):
  File "/Users/krunal/Desktop/code/pyt/database/app.py", line 3, in <module>
    print(pow(-1, 0.5))
ValueError: math domain error

And we get the ValueError.

That is it for this tutorial.

See also

String Indices must be Integers in Python

Python expected an indented block Error

Python cannot import name

Return outside function Python

Python ValueError

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.