In this post, we will discuss the below-mentioned methods which are present in the math module.
-
factorial
-
fmod
-
fexp
1. The factorial method
This method returns the factorial of a number, i.e the number continuously multiplied by one less than the original number until it reaches 1. Also, remember that the factorial of 0 is 1. Factorial of a number is represented with the help of the symbol !
symbol. The factorial method has been pre-defined in the math module and can be accessed by importing the math module.
Following is the syntax of factorial method:
math.factorial(number)
If no value has been provided to the factorial function, it throws TypeError. If the value provided to the factorial method is a floating-point value, it throws ValueError.
Time for an example:
import math
num = 12
print("Factorial of %d is : ", num)
print(math.factorial(num))
print(math.factorial(12.0))
Output:
Factorial of %d is : 12
479001600
479001600
When erroneous values are passed to the factorial method:
print (math.factorial())
print (math.factorial(12.1))
Output:
TypeError: factorial() takes exactly one argument (0 given)
ValueError: factorial() only accepts integral values
2. The fmod method
The fmod
method of the math module takes two parameters, i.e x and y (which are valid numbers in Python) and returns the module value of x and y. When we divide a number with another number, the remainder value is known as module. For example, if we divide 10 with 4, 2.0 will be the remainder and hence the result of fmod(10, 4)
function call.
In case the values x and y passed to the function are not valid numbers, it returns TypeError. In case one of x or y is 0, is returns ValueError since 0 can't be divided by any number and 0 can't divide any number.
Following is the syntax of the fmod
function,
math.fmod(x, y)
The return value is a floating-point value.
Time for an example:
import math
my_list = [1,2,-3,5, 0]
my_tuple = (5,7,9,0)
# my_tuple[2] = 9 and my_list[2] = -3
result_1 = math.fmod(my_tuple[2], my_list[2])
# my_tuple[2] = 9 and my_list[1] = 2
result_2 = math.fmod(my_tuple[2], my_list[1])
print(result_1)
print(result_2)
# my_tuple[3] = 0 and my_list[4] = 0
result_3 = math.fmod(my_tuple[3], my_list[4])
print(result_3)
Output:
0.0
1.0
Traceback (most recent call last):
File "<ipython-input-128-bbfb413e6ccc>", line 10, in <module>
result_3 = math.fmod(my_tuple[3], my_list[4])
ValueError: math domain error
3. The frexp method
This method takes one value as an argument and returns the mantissa and the exponent values as a pair (m, e). The 'mantissa' value m is a floating-point number whereas the 'exponent' e is an integer.
How are m
and e
calculated?
x = m *2**e
If 0 is passed as a parameter to the frexp
method, it returns an ordered pair (0.0, 0). If the parameter is not a number, it throws a TypeError.
Following is th syntax of the frexp
method:
math.frexp(value)
Time for an example:
When valid numbers are passed,
import math
print(math.frexp(12))
print(math.frexp(16.45))
print(math.frexp(-12))
Output:
(0.75, 4)
(0.5140625, 5)
(-0.75, 4)
Note: Elements indexed from a tuple or list could also be passed as parameters to the frexp
method.
When a string is passed,
import math
print(math.frexp('25'))
Output:
TypeError: must be real number, not str
Conclusion:
In this post, we understood three built-in methods from the math module: factorial()
, fmod()
and frexp()
. 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 Functions - isinf(), isfinite() and isnan()
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