Mathematical Functions in Python?


From doing simple to complex mathematical operations(like trigonometric, logarithmic operations etc) in python we may need to use the math() module.

The python math module is used to access mathematical functions. All methods of math() function are used for integer or real type objects but not for complex numbers.

To use this function, we need to import it in our code

import math

Constants

We use these constants for calculation in python -

Constants
Descriptions
Pi
Return the value of pi: 3.141592
E
Return the value of natural base e. e is 0.718282
tau
Returns the value of tau. tau = 6.283185
inf
Returns the infinite
nan
Not a number type

Numbers and Numeric Representation

Python provides different functions which are used to represent numbers in different forms, for example -

Function
Description
Ceil(x)
It returns the ceiling value which is the smallest value, greater or equal to the number x.
copysign(x, y)
Returns the number of x and copy the sign of y to x.
fabs(x)
Return absolute value of x.
factorial(x)
Returns factorial of x where x>=0
floor(x)
Returns the floor value which is the largest integer, less or equal to the number x.
fsum(iterable)
Returns sum of the elements in an iterable object
gcd(x,y)
Returns the greatest common divisor of x and y.
isfinite(x)
Checks whether x is neither an infinity nor nan.
isinf(x)
Checks if x is infinity
isnan(s)
Checks whether s is not a number
remainder(x,y)
Find remainder after dividing x by y.

Let’s write a program to demonstrate the use of above mathematical functions -

#Import math library
import math
#Floor and Ceiling
print('The Floor and Ceiling value of 9.45 are:
   ' + str(math.ceil(9.45)) + ', ' + str(math.floor(9.45)))

#Copysign
x = 94
y = -27
print('The value of x after copying the sign from y is: ' + str(math.copysign(x, y)))

#Absolute
print('Absolute value of -94 and 54 are: ' + str(math.fabs(-94)) + ', ' + str(math.fabs(54)))

#Fsum & gcd
my_list = [12, 9.25, 89, 3.02, -75.23, -7.2, 6.3]
print('Sum of the elements of the list: ' + str(math.fsum(my_list)))
print('The GCD of 24 and 56 : ' + str(math.gcd(24, 48)))

#isnan
x = float('nan')
if math.isnan(x):
   print('It is not a number')
      x = float('inf')

#isinf
y = 54
if math.isinf(x):
   print('It is Infinity')
      #x is not a finite number
print(math.isfinite(x))
   #y is a finite number
print(math.isfinite(y))

Result

The Floor and Ceiling value of 9.45 are: 10, 9
The value of x after copying the sign from y is: -94.0
Absolute value of -94 and 54 are: 94.0, 54.0
Sum of the elements of the list: 37.13999999999999
The GCD of 24 and 56 : 24
It is not a number
It is Infinity
False
True

Power & Logarithmic Functions

These functions are used to calculate different power and logarithmic related tasks in python.

Function
Description
pow(x,y)
Return- x to the power y value
sqrt(x)
Finds the square root of x
exp(x)
Finds xe, where e = 2.718281
log(x[,base])
Returns the log of x where base is given. The default base is e
log2(x)
Returns the log of x, where base is 2
log10(x)
Returns the log of x, where base is 10

 

Example program to demonstrate use of above functions

import math
print("The value of 2^5: " + str(math.pow(2, 5)))
print("Square root of 625: " + str(math.sqrt(625)))
print("The value of 5^e: " + str(math.exp(5)))
print("The value of log(625), base 5: " + str(math.log(625, 5)))
print("The value of log(1024), base 10: " + str(math.log10(1024)))
print("The value of log(1024), base 2: " + str(math.log2(1024)))

Result

The value of 2^5: 32.0
Square root of 625: 25.0
The value of 5^e: 148.4131591025766
The value of log(625), base 5: 4.0
The value of log(1024), base 10: 3.010299956639812
The value of log(1024), base 2: 10.0

Trigonometric and Angular Conversion Functions

These functions are used to calculate different trigonometric operations -

Function
Description
sin(x)
Return the sine of x in radians
cos(x)
It returns the cosine of x in radians
tan(x)
It returns the tangent of x in radians
asin(x)
It returns the inverse of the sine, similarly we have acos, atan also
degrees(x)
It conver angle x from radian to degrees
radians(x)
It convert angle x from degrees to radian

 Example program to demonstrate the use of above functions

import math
print("The value of sin(45 degree): " + str(math.sin(math.radians(45))))
print('The value of cos(pi): ' + str(math.cos(math.pi)))
print("The value of tan(45 degree): " + str(math.tan(math.pi/2)))
print("the angle of sin(0.95504050560):" + str(math.degrees(math.sin(0.95504050560))))

Result

The value of sin(45 degree): 0.7071067811865475
The value of cos(pi): -1.0
The value of tan(45 degree): 1.633123935319537e+16
the angle of sin(0.95504050560):46.77267256206895

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements