In previous posts, we discussed a variety of math module functions. Continuing the same, we will discuss the below-mentioned math module functions.
-
isfinite
-
isinf
-
isnan
These methods are built-in and are present in the math module. They can be accessed and used after importing the math module and referencing it with the help of the dot operator.
1. The isfinite method
It takes one argument and returns True if the argument is a valid python number. This number could be positive or negative. If the parameter passed is infinite or Nan (Not a number), it returns False.
Following is the sntax of the isfinite
method,
math.isfinite(number)
If no value has been provided to the isfinite function, it throws TypeError.
Time for an example:
The below example demonstrates different cases (values) that could be passed to the isfinite method.
import math
print(math.isfinite(12))
print(math.isfinite(0.0)) # Python considers 0.0 as finite number
print(math.isfinite(0/12))
print(math.isfinite(0.0/21))
print(math.isfinite(-79))
print(math.isfinite(-79.89))
print(math.isfinite(float('nan')))
print(math.isfinite(math.inf))
print(math.isfinite('{')) # When a string is passed (error)
print(math.isfinite()) # When no parameter is passed (error)
Output:
True
True
True
True
True
True
False
False
Traceback (most recent call last):
File "<ipython-input-197-511869dbfa73>", line 9, in <module>
print(math.isfinite('{'))
TypeError: must be real number, not str
Note: Don't confuse this with numpy module's isfinite method.
2. The isinf method
The isinf
method of the math module takes one argument and checks if the number is infinite. If yes, it returns True and it returns False if the number passed as argument is finite or a Nan (Not a number).
In case no parameter is passed to the function, it returns TypeError.
Following is the syntax of the isinf
method,
math.isinf(argument)
Time for an example:
import math
print(math.isinf(12))
print(math.isinf(0.0)) # Python considers 0.0 as finite number
print(math.isinf(0/12))
print(math.isinf(0.0/21))
print(math.isinf(-79))
print(math.isinf(-79.89))
print(math.isinf(math.inf)) # When infinity is passed as an argument
print(math.isinf()) # When no parameter is passed
Output:
False
False
False
False
False
False
True
Traceback (most recent call last):
File "<ipython-input-199-9781ae1653ba>", line 10, in <module>
print(math.isinf())
TypeError: isinf() takes exactly one argument (0 given)
Note: Don't confuse this with numpy module's isinf method.
3. The isnan method
This method takes one argument and checks if it Nan(not a number). If yes, it returns True and if the argument passed is a valid Python number, it returns False. If no parameter is passed to the isnan
method, it throws TypeError.
Following is the syntax of the isnan
method,
math.isnan(argument)
Time for an example:
import math
print(math.isnan(12))
print(math.isnan(0.0)) # Python considers 0.0 as finite number
print(math.isinf(0/12))
print(math.isnan(-79))
print(math.isnan(-79.89))
print(math.isnan(math.pi))
print(math.isnan(float('nan'))) # Passing nan to the isnan method
print(math.isnan()) # No parameter passed
Output:
False
False
False
False
False
False
True
Traceback (most recent call last):
File "<ipython-input-204-849cf7490bda>", line 10, in <module>
print(math.isnan())
TypeError: isnan() takes exactly one argument (0 given)
Note: Don't confuse this method with numpy.isnan method
Conclusion
In this post, we understood three more built-in methods from the math module namely isfinite()
, isinf()
and isnan()
. These methods come in handy while manipulating numbers. Don't forget to execute these Python programs on your IDE. Meddle with the input to see how the output changes.
Similar Posts:
Math Log Functions - log(), log1p(), log2(), log10()
Math Functions - ceil(), floor(), fabs() and copysign()
Trigonometric Functions - sin, cos, tan, etc
math.remainder() Funciton in Python
Math functions- factorial(), fmod(), and frexp()