Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python math.isfinite() Method



The Pythonmath.isfinite() method is used to determine whether a given number is a finite floating-point number. It returns "True" if the number is finite, and "False" otherwise. A floating-point number is considered finite if it is neither positive nor negative infinity and not NaN.

For example, if you have a floating-point number "x = 3.14", themath.isfinite(3.14) method will return "True", indicating that 3.14 is a finite number.

Similarly, if you have a floating-point number "y =float('inf')", which represents positive infinity, themath.isfinite(y) method will return "False", indicating that ∞ is not finite.

Syntax

Following is the basic syntax of the Pythonmath.isfinite() method −

math.isfinite(x)

Parameters

This method accepts a numeric value as a parameter representing the floating-point number to be checked for finiteness.

Return Value

The method returns aboolean value (True or False) indicating whether the given value "x" is a finite floating-point number.

Example 1

In the following example, we check if the number "10.5" is a finite floating-point number using themath.isfinite() method −

import mathresult = math.isfinite(10.5)print("The result is:",result)

Output

The output obtained is as follows −

The result is: True

Example 2

Here, we check if the number "0" is a finite floating-point number using themath.isfinite() method −

import mathresult = math.isfinite(0)print("The result is:",result)

Output

Following is the output of the above code −

The result is: True

Example 3

Now, we are checking if negative infinity is a finite floating-point number using themath.isfinite() method −

import mathresult = math.isfinite(float('-inf'))print("The result is:",result)

Output

We get the output as shown below −

The result is: False

Example 4

In this example, we are checking if NaN (Not a Number) is a finite floating-point number using themath.isfinite() method −

import mathresult = math.isfinite(float('nan'))print("The result is:",result)

Output

The result produced is as shown below −

The result is: False
python_maths.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp