The math module provides us various functions. Some of them are discussed in this article. Import the math function to begin.
The ten methods in this article are
- ceil and floor
- sin, cos, tan
Ceil and floor functions
The ceil() method returns an integer that is greater than or equal to the given float value. Similarly, the floor() method returns an integer that is lesser or equal to the given float value. If an integer value is given the same value is returned.
This method returns the factorial value of a number. In mathematics, the factorial of a positive integer n, denoted by n! is the product of all positive integers less than or equal to n.
Example 4! is 4*3*2*1 and 5! is 5*4*3*2*1
The gcd() method returns the gcd of the two numbers passed to the function. In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4.
The sqrt() method returns the square root of a number. In mathematics, a square root of a number x is a number y such that y² = x.
The pow(x, y) method returns the value of x raised to the power of y. The return type is always float and hence takes both integer or float as arguments.
sin, cos, and tan
The sin, cos, and tan methods take a degree and return their corresponding value.
Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.
The fabs() method returns the absolute value of the number as a floating-point value. This is similar to the abs() method except for the result here is in float.
The isclose() method takes two values. We can specify the value of the difference. A difference value greater than this returns False and a difference value less than this returns True. By default, the difference value is 0.
The difference value can be specified as a relative or absolute difference.
The first command returns false as the difference between 2 and 5 is 3, but the difference that we have specified is 1.
Whereas the second command returns true as the difference between 2 and 3 is 1, which matches our difference.
The values of mathematical constants such as π, Τ, and e are also available in the math module.
Hope these methods helped you. Happy coding!