Special Functions
Besides all the numeric, logarithmic functions we have discussed yet, the math module provides some more useful functions that does not fall under any category discussed above but may become handy at some point while coding.
1. Finding gamma value
The gamma() function is used to return the gamma value of the argument.
Example: This code imports the math
module, which provides mathematical functions and constants. It then defines a variable gamma_var
and assigns it the value 6. Next, the code calculates and prints the gamma value of gamma_var
using the math
module’s gamma()
function. The math.gamma()
function calculates the gamma value of a given argument.
Python3
import math gamma_var = 6 print ( "The gamma value of the given argument is : " + str (math.gamma(gamma_var))) |
Output:
The gamma value of the given argument is : 120.0
2. Check if the value is infinity or NaN
isinf() function is used to check whether the value is infinity or not.
Example: This code imports the math
module and then checks whether the values of math.pi
, math.e
, and float('inf'
)
are infinite using the math.isinf()
function. The math.isinf()
function takes a single argument, which is the value to be checked for infinity. It returns True
if the value is infinite and False
Python3
import math print (math.isinf(math.pi)) print (math.isinf(math.e)) print (math.isinf( float ( 'inf' ))) |
Output:
False
False
True
isnan() function returns true if the number is “NaN” else returns false.
Example: This code imports the math
module and then checks whether the values of math.pi
, math.e
, and float('nan')
are Not a Number (NaN) using the math.isnan()
function. The math.isnan()
function takes a single argument, which is the value to be checked for NaN. It returns True
if the value is NaN and False
.
Python3
import math print (math.isnan(math.pi)) print (math.isnan(math.e)) print (math.isnan( float ( 'nan' ))) |
Output:
False
False
True
Refer to the below article to get detailed information about the special functions.
We have explained math module and it’s uses with example. Math module is very important mode in Python and every beginner should know how to use it.
It is used in various basic Python codes, school projects, etc. Hope you understood how to use math module in Python.
Python Math Module
Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks.
The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric, logarithmic, and exponential functions.
In this article, we learn about the math module from basics to advanced, we will study the functions with the help of good examples.
Table of Content
- What is a Math Module in Python?
- Constants in Math Module
- List of Mathematical function in Math Module
- Numeric Functions in Math Module
- Logarithmic and Power Functions
- Trigonometric and Angular Functions
- Special Functions