• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Python isfinite() Function – Check if Number is Finite with math.isfinite()

Python isfinite() Function – Check if Number is Finite with math.isfinite()

May 19, 2022 Leave a Comment

To check if a number is finite or not in Python, you can use the math module isfinite() function. isfinite() returns a boolean value which tells us if the input number is finite or not.

import math

print(math.isfinite(10))
print(math.isfinite(float('inf')))

#Output:
True
False

The Python math module has many powerful functions which make performing certain calculations in Python very easy.

One such piece of information which can be useful is if we want to check if a number is finite or infinite.

We can use the math module isfinite() function to check if a number is finite in our Python code.

isfinite() takes an integer or float input and returns a boolean. If the number passed is finite, isfinite() returns True. If the number passed is not finite, then isfinite() returns False.

Below are a few examples showing you how to use isfinite() in Python to check if a number is finite or not.

import math

print(math.isfinite(10))
print(math.isfinite(-10))
print(math.isfinite(1000000000000000000000))
print(math.isfinite(float('inf')))
print(math.isfinite(-float('inf')))

#Output:
True
True
True
False
False

How to Check if Number is Infinite in Python

If you want to go the other way and check if a number is infinite or equal to infinity, then you want to take the negation of what is returned from isfinite().

Below is a simple example which shows you how to check if a number is infinite in Python.

import math

def isinfinite(num):
    return not math.isfinite(num)

print(isinfinite(10))
print(isinfinite(-10))
print(isinfinite(1000000000000000000000))
print(isinfinite(float('inf')))
print(isinfinite(-float('inf')))

#Output:
False
False
False
True
True

Hopefully this article has been useful for you to learn how to use the Python math module isfinite() function in your Python programs.

Other Articles You'll Also Like:

  • 1.  Python cosh – Find Hyperbolic Cosine of Number Using math.cosh()
  • 2.  Get Current Year in Python
  • 3.  Clear File Contents with Python
  • 4.  PROC MEANS Equivalent in Python
  • 5.  Using Python to Generate Random String of Specific Length
  • 6.  Python max float – What’s the Maximum Float Value in Python?
  • 7.  Using Python to Increment Dictionary Value
  • 8.  math gcd Python – Find Greatest Common Divisor with math.gcd() Function
  • 9.  Find Magnitude of Complex Number in Python
  • 10.  max function Python – Get Maximum of List or Dictionary with max() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy