• 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
  • 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.  Python getsizeof() Function – Get Size of Object
  • 2.  Add Minutes to Datetime Variable Using Python timedelta() Function
  • 3.  Writing a Table Faster to Word Document Using python-docx
  • 4.  Using Python to Add Trailing Zeros to String
  • 5.  Split Column by Delimiter in pandas DataFrame
  • 6.  Python Decrement Counter with -= Decrement Operator
  • 7.  How to Create Block and Multi-Line Comments in Python
  • 8.  Python Try Until Success with Loop or Recursion
  • 9.  Using Python to Get Day of Week
  • 10.  Using Lambda Expression with max() 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