AppDividend
Latest Code Tutorials

Python isfinite() Function Example

0

Python isfinite() is an inbuilt method that is used to find whether a number is finite or not. It accepts a number (integer, float, NaN ) and checks whether it is finite or not. The infinite() function is under python math library, so we have to import math if we want to use the infinite() function.

Python isfinite()

Python isfinite() function exists in the Standard math library of Python language. It is used to determine whether the particular python number is a finite number or not.

Syntax

math.isfinite(x)

The isfinite() function takes x as an argument and checks if x is finite or not. Here x can be an integer, float, double, inf, NaN, etc.

Return Value

The isfinite() function returns two types of output:

True: If the number is finite.

False: If the number is not finite.

Programming Example

See the following code.

# Importing math library
import math

# Checking working of isfinite()

# When the number is integer
print(math.isfinite(100))

# When the number is float
print(math.isfinite(10.05))

# When the number is negative
print(math.isfinite(-14))

# When the number is not finite
print(math.isfinite(float('inf')))

Output

True
True
True
False

In this program, we have the first imported math library and then we have checked the output of isfinite() function using various input.

We have checked output for integer, float, and negative numbers, respectively; for all these cases, the output is True. Then we have checked output for inf, here the output is False.

Pass string as an argument in Python isfinite()

If we pass a string as an argument, then we should get an error.

See the following code.

# Importing math library
import math

print(math.isfinite('AppDividend'))

Output

python3 app.py
Traceback (most recent call last):
  File "app.py", line 4, in <module>
    print(math.isfinite('AppDividend'))
TypeError: must be real number, not str

If we pass a string as an argument, then we got the TypeError.

Pass empty value as an argument

If we pass the empty values as an argument, then let’s see what we will get?

# Importing math library
import math

print(math.isfinite())

Output

python3 app.py
Traceback (most recent call last):
  File "app.py", line 4, in <module>
    print(math.isfinite())
TypeError: isfinite() takes exactly one argument (0 given)

So, we get: TypeError: isfinite() takes exactly one argument (0 given).

The function requires a minimum one argument, and if we don’t pass that, then we get TypeError.

Conclusion

Python isfinite() function is beneficial when we need to find whether a number is finite or not. In Mathematical calculations, this function helps determine whether we got an infinite value or not.

Finally, Python isfinite() function example is over.

See also

Python fmod()

Python fabs()

Python copysign()

Python fsum()

Python frexp()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.