Online Tutorials & Training Materials | STechies.com
Register Login

ValueError: math domain error

11 Feb 2020 12:20 pm || 0

ValueError: math domain error

While working with mathematical functions in Python, you might come across an error called "ValueError math domain error". This error is usually encountered when you are trying to solve quadratic equations or finding out the square root of a negative number.

You can avoid this error by providing the correct values to the math functions. Avoiding the use of negative values will be ideal.

Let us look at some examples where the error might be encountered.

Example 1: Square Root of Negative Number

We can calculate the square root of a number in python by importing the sqrt method from the math module. But what if a user entered a negative number?

Will it throw an error or will we get the desired output? let’s understand it with a few examples.

from math import sqrt
# Initialising the variable 'num'
num=float(input("Enter number :"))
#Square root of num
print("Square root of given number :",sqrt(num))

Output:

Enter number :12
Square root of given number : 3.4641016151377544

Enter number :-12
File "sqr.py", line 5, in <module>
print("Square root of given number :",sqrt(num))
ValueError: math domain error

If num less then 0 or negative number then this code throws a math domain error as mentioned above.

ValueError: math domain error

Solution:

We can either handle the ValueError by raising an exception or by importing sqrt method from cmath library lets discuss both of them.

Method 1: Using Try and Except Block for Handling the Error.

from math import sqrt
#try block for code to be tested
try:
#intialising the variable 'num'
num=float(input("Enter Number :"))
#Square root
print("Square root of given number :",sqrt(num))
#except block if error is raised
except ValueError:
print("Please enter a number greater than zero ")

OUTPUT :

Enter Number : 12
Square root of given number : 3.4641016151377544

Enter Number : -12
Please enter a number greater than zero

In the above code, when we enter a positive value we will get the desired output. But, when we will enter a negative value it’ll throw an error i.e "ValueError: math domain error".

And to handle the ValueError we use try and except block.

The try block includes the code to be tested.

The Except block handles the error by displaying the desired message. Which in this case is "Please enter the number greater than zero".

ValueError: math domain error

Method2: Importing Sqrt From "cmath" Which Will Return Square Root of Negative Number in Complex/Imaginary Form.

# Importing cmath module
from cmath import sqrt

# Input from user
num=float(input("Enter Number: "))

#Square root
print("Square root of given number : ", sqrt(num))

OUTPUT:

Enter Number : 12
Square root of given number : (3.4641016151377544+0j)

Enter Number : -12
Square root of given number : 3.4641016151377544j

In Method 1 we did not get the result instead we raised an exception. But what if we want the square root of a negative index in complex form.
To solve this issue import "sqrt" from cmath module. Which shows the result in complex/imaginary form as in mathematics.

When we import the cmath module the result which we will get will be in the complex form as shown in the output of "Method 2".

ValueError: math domain error

Example 2: Log of a Negative Number

#importing log from math module
from math import log
#Intialising the variable 'num'
num= float(input("Enter Number :"))
#Natural logarithm of num
print("Natural logarithm of provided number :",log(num))

OUTPUT:

Enter Number :12
Natural logarithm of provided number : 2.4849066497880004

Enter Number :-12
File "sqr.py", line 6, in <module>
print("Natural logarithm of provided number :",log(num))
ValueError: math domain error

In the above code, When we try to find the log of the positive value we get the desired output. But when we try to find the log of the negative index it throws an error "ValueError: math domain error".

ValueError: math domain error

This is because the negative of the log is not defined in python.