ValueError: math domain error (Fix)

ValueError: math domain error happens when you pass a value to a function in Python’s math module that is outside the valid input range.

This usually happens with functions like:

  • math.sqrt()
  • math.log()
  • math.log10()
  • math.asin()
  • math.acos()

For example, math.sqrt(-1) fails because the math module expects a real number, and -1 is not a valid input for a real square root.

Quick fix

import math

x = -1

if x >= 0:
    print(math.sqrt(x))
else:
    print("x must be 0 or greater")

This error happens when you pass a value that is outside the valid range for a math function, such as math.sqrt(-1) or math.log(0).

What this error means

This error means:

  • A math function received a value it cannot use
  • The value is outside the function's valid input range
  • Common examples include:
    • square root of a negative number
    • logarithm of 0
    • inverse trig functions with values outside -1 to 1

In short, Python is telling you: “This number is not allowed for this math function.”

When this error happens

You will often see this error in cases like these:

  • Using math.sqrt() with a negative number
  • Using math.log() or math.log10() with 0 or a negative number
  • Using math.acos() or math.asin() with a value less than -1 or greater than 1
  • Passing user input that was converted into an invalid number

If you are new to exceptions, see Python errors and exceptions explained for a simple overview.

Example that causes the error

Here are some common examples.

math.sqrt(-1)

import math

print(math.sqrt(-1))

Output:

ValueError: math domain error

math.log(0)

import math

print(math.log(0))

Output:

ValueError: math domain error

math.acos(2)

import math

print(math.acos(2))

Output:

ValueError: math domain error

How to fix it

There are a few simple ways to fix this error.

  • Check the valid range before calling the function
  • Clean and validate user input first
  • Use if statements to block invalid values
  • Use try-except if a value may sometimes be invalid
  • Use cmath instead of math if you need complex-number results

Fix 1: Check the value before calling the function

import math

x = 25

if x >= 0:
    print(math.sqrt(x))
else:
    print("x must be 0 or greater")

Output:

5.0

This is the safest approach when you know the required range.

For more on square roots, see math.sqrt() explained.

Fix 2: Validate values for logarithms

math.log() and math.log10() require a value greater than 0.

import math

x = 10

if x > 0:
    print(math.log(x))
else:
    print("x must be greater than 0")

If x is 0 or negative, do not call math.log(x).

Fix 3: Validate inverse trig input

math.asin() and math.acos() only accept values from -1 to 1.

import math

x = 0.5

if -1 <= x <= 1:
    print(math.acos(x))
else:
    print("x must be between -1 and 1")

Fix 4: Validate user input

A bad value often comes from input entered by a user.

import math

text = input("Enter a number: ")
x = float(text)

if x >= 0:
    print(math.sqrt(x))
else:
    print("Please enter a number that is 0 or greater")

This prevents calling math.sqrt() with an invalid number.

Fix 5: Use try-except

If invalid values are possible and you want your program to continue running, catch the error.

import math

x = -4

try:
    print(math.sqrt(x))
except ValueError:
    print("Invalid value for math.sqrt()")

This is useful when values come from calculations or unpredictable input.

If you want to learn this pattern, read how to handle exceptions in Python.

Fix 6: Use cmath for complex results

The math module works with real numbers. If you want the square root of a negative number, use cmath.

import cmath

print(cmath.sqrt(-1))

Output:

1j

If you are working with standard math functions, see the Python math module overview.

Common valid ranges to remember

These are the most common valid ranges that cause this error:

  • math.sqrt(x): x must be 0 or greater
  • math.log(x): x must be greater than 0
  • math.log10(x): x must be greater than 0
  • math.acos(x): x must be between -1 and 1
  • math.asin(x): x must be between -1 and 1

If you are not sure about a function, check its documentation before using it.

Debugging steps

If you are not sure where the error is coming from, try these steps:

  • Print the value before the math function runs
  • Check which math function caused the error
  • Read the function's allowed input range
  • Trace where the bad value came from
  • Add validation close to the input or calculation

Useful checks:

print(x)
print(type(x))

You can also inspect help for the function:

import math
print(help(math.sqrt))
print(help(math.log))
print(help(math.acos))

This can help you confirm what values are allowed.

Common mistakes

These are the most common causes of this error:

  • Calling math.sqrt() with a negative number
  • Calling math.log() with 0
  • Calling math.log() with a negative number
  • Calling math.asin() or math.acos() with a value outside -1 to 1
  • Using unexpected user input
  • Passing a value from an earlier calculation that turned out to be invalid

A related point: this is still a ValueError, but it has a specific message. If you want a broader explanation, see ValueError in Python: causes and fixes.

FAQ

Why do I get ValueError: math domain error in Python?

You passed a value to a math function that is outside its allowed range.

Why does math.sqrt(-1) fail?

The math module only works with real-number square roots. Negative numbers are not valid for math.sqrt().

How do I fix math.log(0)?

Make sure the value is greater than 0 before calling math.log().

Can I calculate the square root of a negative number in Python?

Yes. Use the cmath module if you want a complex-number result.

See also