Learn with Yasir

Share Your Feedback

Python Math Module: isinf() vs isfinite() Explained


Learn the key differences between math.isinf() and math.isfinite() in Python. Understand how to detect infinite, finite, and NaN values effectively using the math module.

Here’s a clear comparison between math.isinf(x) and math.isfinite(x) in Python:


math.isinf(x)

  • Purpose: Checks whether a value is infinite.
  • Returns: True if x is either positive infinity (+inf) or negative infinity (-inf); otherwise, False.
  • Example:

    import math
    print(math.isinf(float('inf')))   # True
    print(math.isinf(10))             # False
    

math.isfinite(x)

  • Purpose: Checks whether a value is a finite number.
  • Returns: True if x is neither infinite (inf, -inf) nor NaN (Not a Number); otherwise, False.
  • Example:

    import math
    print(math.isfinite(10))            # True
    print(math.isfinite(float('inf')))  # False
    print(math.isfinite(float('nan')))  # False
    

Key Differences

Feature math.isinf(x) math.isfinite(x)
Checks for Infinity (+inf or -inf) Not infinite and not NaN
Returns True if x is infinite x is a normal finite number
Returns False if x is finite or NaN x is either infinite or NaN