Math - mathematical functions

The math module provides access to all the mathematical functions defined by C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if you require support for complex numbers. The distinction between functions which support complex numbers and those which don’t is made since most users do not want to learn quite as much mathematics as required to understand complex numbers. Receiving an exception instead of a complex result allows earlier detection of the unexpected complex number used as a parameter, so that the programmer can determine how and why it was generated in the first place.


math.ceil (x)
ceil of an integral value will return the same integer and ceil of an floating value will return the closest integer which is more then x. For example , ceil(2)=2 and ceil(2.5)=3. Let us run a code using this function.
Note that all the functions in math module have the return type of float and so you may wanna typecast it to an integer.
import math
print(math.ceil(100))
print(math.ceil(50.5))
print(math.ceil(-1.5))
output :
100
51
-1
math.fabs(x)
Returns the absolute value of x.
import math
print(math.fabs(-5))
print(math.fabs(-100))
output :
5.0
100.0
math.factorial(x)
Returns the factorial of x . Raises ValueError if x is not integral or is negative.
import math
print(math.factorial(0))
print(math.factorial(10))
print(math.factorial(50))
output :
1
3628800
30414093201713376000000000000000000000000000000000000000000000000
math.floor(x)
floor of an integral value will return the same integer and floor of an floating value will return the closest integer which is less then x. For example , floor(2)=2 and floor(2.5)=2.
import math
print(math.floor(2))
print(math.floor(2.5))
print(math.floor(100.5))
output :
2
2
100
math.exp(x)
Returns the value of e raised to the power of x.
import math
print(math.exp(2))
print(math.exp(3))
output :
7.38905609893
20.0855369232
math.pow(x,y)
Equivalent to x**y. Returns x raised to the power of y.
import math
print(math.pow(2,5))
print(math.pow(5,2))
output :
32.0
25.0
math.sqrt(x)
Returns the square root of x .
import math
print(math.sqrt(45))
print(math.sqrt(100))
output :
6.7082039325
10.0
math.trunc(x)
Returns the Real value x truncated to an Integral .
import math
print(math.trunc(45.342))
print(math.trunc(100.324))
output :
45
100
math.radians(x)
Convert angle x in degrees to radians.
import math
print(math.radians(45))
print(math.radians(37))
output :
0.785398163397
0.645771823238
math.degrees(x)
Converts the angle x in radians to degrees.
import math
print(math.degrees(3.14/4))
print(math.degrees(3.14/2))
output :
44.9771869178
89.9543738355
math.log(x,y)
Returns the logarithm of x to the base y . If the base is not mentioned , then natural logarithm of x is returned ( base = e ).
import math
print(math.log(2,4))
print(math.log(81,3))
output :
0.5
4.0
math.log10(x)
Returns the logarithm of x to the base 10.
import math
print(math.log10(10))
print(math.log10(100))
print(math.log10(1000))
output :
1.0
2.0
3.0
math.sin(x)
Returns the sine of x ( measured in Radians ).
import math
print(math.sin(3.14/4))
print(math.sin(3.14/2))
output :
0.706825181105
0.999999682932
math.cos(x)
Returns the cosine of x ( measured in Radians ).
import math
print(math.cos(3.14/4))
print(math.cos(3.14/2))
output :
0.707388269167
0.000796326710733
math.tan(x)
Returns the tan of x ( measured in Radians ).
import math
print(math.tan(3.14/4))
print(math.tan(3.14/2))
output :
0.999203990105
1255.7655915
math.acos(x)
Returns the arc cosine ( measured in radians ) of x .
import math
print(math.acos(1))
print(math.acos(1/2))
output :
0.0
1.0471975512
math.asin(x)
Returns the arc sine ( measured in radians ) of x .
import math
print(math.asin(1))
print(math.asin(1/2))
output :
1.57079632679
0.523598775598
math.atan(x)
Returns the arc tangent ( measured in radians ) of x .
import math
print(math.atan(5))
print(math.atan(50))
output :
1.37340076695
1.55079899282
math.cosh(x)
Returns the hyperbolic cosine of x .
import math
print(math.cosh(1))
print(math.cosh(3))
output :
1.54308063482
10.0676619958
math.sinh(x)
Returns the hyperbolic sine of x .
import math
print(math.sinh(1))
print(math.sinh(3))
output :
1.17520119364
10.0178749274
math.tanh(x)
Returns the hyperbolic tangent of x .
import math
print(math.tanh(1))
print(math.tanh(3))
output :
0.761594155956
0.995054753687




Popular Tutorials

Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.