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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • 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.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 2.  pandas mean – Get Average of Series or DataFrame Columns
  • 3.  Using Python to Generate Random String of Specific Length
  • 4.  Using Python to Split String by Tab
  • 5.  Check if File Exists in AWS S3 Bucket Using Python
  • 6.  Get Elapsed Time in Seconds in Python
  • 7.  pandas product – Get Product of Series or DataFrame Columns
  • 8.  pandas T Function – Transposing DataFrames with pandas
  • 9.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 10.  Python max float – What’s the Maximum Float Value in Python?

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy