Python math.isinf()

chmst_promotheus's avatar
Published Sep 4, 2024
Contribute to Docs

The math.isinf() function in Python checks whether a given value is infinite. It returns True if the value is positive or negative infinity, and False otherwise. This function is part of the math module and is useful when dealing with calculations that may result in infinite values.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

math.isinf(x)
  • x: The number to be checked for infinity.

Example

The following example demonstrates how to use the math.isinf() function:

import math
print(math.isinf(float('inf'))) # Positive infinity
print(math.isinf(float('-inf'))) # Negative infinity
print(math.isinf(1000)) # Finite number

This code outputs:

True
True
False

Codebyte Example

Run the following codebyte to understand how the math.isinf() works in various scenarios:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours