In mathematics, there are operations which do not work on negative numbers or zero numbers. Consider the square root, for example. You cannot find the square root of a negative number. Python recognizes that not all operations work with negative or zero numbers.
Python will raise an error when you try to use a negative number on an operation that does not support one. In this guide, we’re going to talk about the cause of the ValueError: math domain error
. Toward the end of the guide, we’ll walk through a solution to this issue.
ValueError: math domain error
The Python ValueError: math domain error
is raised when you use a number that is not supported by a mathematical operation. This error is commonly raised with the sqrt()
method and the log()
method.
The ValueError is a type of error that indicates you are performing a mathematical operation on a value that does not work with that operation. In the case of the “math domain error”, we are using a negative number or a zero number where we should not be.
Let’s walk through an example of the ValueError: math domain error
issue in action.
An Example Scenario
We are building a program that calculates the square root of a given number. This program is designed to help students revise their knowledge of square roots.
Let’s write a program that calculates the square root of a given number. We will start by importing the math library that we need to calculate a square root:
import math
Next, we’re going to collect a number from the user:
number = input("Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: ")
We prompt the user to try finding the answer themselves, as our program is designed to help people check their answers. Next, we’re going to find the square root of the value the user inserts:
answer = math.sqrt(int(number))
We convert the value of “number”, which stores the number whose square root the user wants to find, into an integer. This is necessary because the input()
method, which we used to collect the aforementioned number, returns a string. We cannot find the square root of a string value.
Finally, let’s print the answer to the console:
print("The square root of {} is {}.".format(number, answer))
We use a format()
statement to add numbers to our string. Our string will show:
"The square root of [Number user inserted] is [The square root our program calculated]"
Let’s test our program with a negative number:
Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: -16 Traceback (most recent call last): File "test.py", line 5, in <module> answer = math.sqrt(int(number)) ValueError: math domain error
We inserted the value -16
into our program. Our code returned an error.
Let’s fix this error.
The Solution
To fix this error, we need to prompt the user that you cannot calculate the square root of a negative number before we execute the math.sqrt()
function.
Let’s revise our code to make this happen:
import math number = input("Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: ") if int(number) >= 0: answer = math.sqrt(int(number)) print("The square root of {} is {}.".format(number, answer)) else: print("You cannot find the square root of a number less than 0.")
We use an if
statement to check if the number the user inserts into the program is equal to or greater than zero. If the number meets this criterion, the contents of the if
statement run. Otherwise, the else
statement executes, presenting us with a message that we have inserted an invalid number.
Let’s run our program again. Our program returns:
Try solving the problem first using pencil and paper. Then, insert the number whose square root you want to verify: -16 You cannot find the square root of a number less than 0.
Our code works successfully.
Conclusion
The ValueError: math domain error
is raised when you perform a mathematical function on a negative or zero number which cannot be computed. To solve this error, make sure you are using a valid number for the mathematical function you are using.
If you want to learn more about coding in Python, check out our How to Learn Python guide. This guide contains a number of learning resources, courses, and books designed for people who are learning the Python programming language.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.