How to Fix ValueError: math domain error in Python

ValueError: math domain error typically occurs when you carry out a math operation that falls outside the domain of the operation.

Here are three ways to fix this error, depending on your situation:

  1. Ensure the value passed to math.sqrt() is non-negative.
  2. Ensure the value passed to logarithmic functions is positive.
  3. Ensure the values for such functions are within the permissible range.

Common reasons and fixes for the error

A negative number for a square root

Visual representation of Negative Number for a Square Root

import math

result = math.sqrt(-1)

print(result)

Output

ValueError: math domain error

You can see that it throws an error because of the math.sqrt() function is not defined for negative numbers, and trying to find the square root of a negative number results in a ValueError: math domain error.

How to fix it?

Fixing the error

To fix it, pass a valid input for which the function can calculate a numerical output.

import math

var = -1

if var >= 0:
  result = math.sqrt(var)
else:
  print("Error: Cannot find square root of negative number")

Output

Error: Cannot find square root of negative number

You can see that we used an if-else statement to check if the number is negative, and if it is, then we print the statement; otherwise, it will find the square root of that number.

Invalid input for logarithmic functions

Visual representation of Invalid input for logarithmic functions

If you are doing a log of a number less than or equal to zero. Unfortunately, that’s mathematically undefined, so the 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

How to fix it?

Flowchart for fixing Invalid input for logarithmic functions

To fix this error, you must ensure that the argument passed to the log function is a positive number.

from math import log

# Value to be used in log()
x = -1

# Check if the value is positive
if x > 0:
  result = log(x)
  print("log(", x, ") =", result)
else:
  print("ValueError: math domain error, value must be positive")

Output

ValueError: math domain error, value must be positive

Invalid Input for trigonometric functions

Visual representation of Invalid Input for Trigonometric Functions

Functions like math.cos() or math.asin(), which calculates the arc cosine or sine, expect a value between -1 and 1. Passing a value outside this range will cause the error.

import math

math.acos(2)

Output

ValueError: math domain error

How to fix it?

Visual representation of fixing Invalid Input for Trigonometric Functions

To fix it, you must ensure that the input to math.acos() is between -1 and 1.

import math

# Value to be used in acos()
x = 2

# Check if the value is within the valid range
if -1 <= x <= 1:
  result = math.acos(x)
  print("acos(", x, ") =", result)
else:
  print("ValueError: math domain error, value must be in the range [-1, 1]")

Output

ValueError: math domain error, value must be in the range [-1, 1]

Best Practices

  • Validate the inputs before passing them to mathematical functions.
  • Understand the domain of each math function you are using.
  • If dealing with a range of input data, especially data from external sources, implement error handling to manage out-of-domain values.

Leave a Comment

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