Python: Math Function

Introduction

In this article we will learn about python maths function. To perform mathematical operations we have math module in Python.
 
What is Module

In C and C++ Programming we have header files, in which we can have functions, classes variables, etc. By including that header files in our code we don't write more code and reuse our functions. Same like in Python we have modules, which may have functions, classes, variables and compiled code. A module contains group of related functions, classes and variables.

There are three kinds of modules in python:
  1. Modules written in Python(.py).

  2. Modules which are written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc).

  3. Modules which are written in C but linked with interpreter to get the list of all modules which are written in C, but linked with Python you can use the following code.
    1. import sys  
    2. print(sys.builtin_module_names)  
    Output:

    ('_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_csv', '_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_pickle', '_random', '_sha1', '_sha256', '_sha512', '_sre', '_stat', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'signal', 'sys', 'time', 'winreg', 'xxsubtype', 'zipimport', 'zlib').

Math Module

You can see in the above list, the math module is written in C programming but linked with Interpreter that contains Math functions and variables. which we will discuss in this article.

Number Representational Function

Ceil and floor function

Ceil and floor functions are common functions. The ceil function rounds up the number to the next highest one. The floor function removes digits decimal points. Both function takes decimal number as argument and returns integer.
Example:
  1. #Importing math Module    
  2. import math    
  3.     
  4. #Fractional number    
  5. number=8.10    
  6.     
  7. #printing ceiling value of the fractional number    
  8. print("Ceiling value of 8.10 is :",math.ceil(number))    
  9.     
  10. #printing floor value of the fractional number    
  11. print("Floor value of 8.10 is :",math.floor(number))  
Output:
   
fabs function

fabs function is used to compute absolute value of any number. If a number contains any negative(-) sign, then it removes that sign and returnss positive fractional number.
Example:
  1. #Importing math Module  
  2. import math  
  3. number = -8.10  
  4. #printing absolute value of the number  
  5. print(math.fabs(number))  
Output:

   
factorial function

This functions takes positive integer number and prints factorial of that number. 
Example:
  1. #Importing math Module    
  2. import math    
  3. number = -8.10    
  4. #printing absolute value of the number    
  5. print(math.fabs(number))   
Output:

 
 
Note: When you will enter negative number, then you will get a Value Error. 
Example:
  1. #Importing math Module  
  2. import math  
  3. number = -5  
  4. #printing factorial of the number  
  5. print("fatorial of the number is",math.factorial(number))  
Output:

  
fmod function

function fmod(x,y) returns x%y. But the difference is x%y preferred when working with integers fmod(x,y) and is used when working with floats.
Example: 
  1. #Importing math Module  
  2. import math  
  3. print(math.fmod(5,2))  
  4. print(math.fmod(-5,2))  
  5. print(math.fmod(-5.2,2))  
  6. print(math.fmod(5.2,2))  
Output:

  
frexp function

This function returns mantissa and exponent in the pair(m,n) of any number x by solving the following equation.
 
Example:
  1. #Importing math Module    
  2. import math    
  3. print(math.frexp(24.8))    
Output:

 
 
fsum function

It calculates accurate floating point sum of values in iterable and calculates sum of list of data or range of data.  
Example:
  1. #Importing math Module    
  2. import math    
  3.     
  4. #sum of list of data    
  5. numbers=[.1,.2,.3,.4,.5,.6,.7,.8,8.9]    
  6. print("sum of ",numbers," is :",math.fsum(numbers))    
  7.     
  8. #sum of range     
  9. print("sum of 1 to 10 numbers is :",math.fsum(range(1,11)))   
Output:

  
Power and Logarithmic Function

exp function

This function takes one parameter as a fractional number and returns e^x.
Example:
  1. #Importing math Module  
  2. import math  
  3.   
  4. print("e ^ 5 is ",math.exp(5))  
  5. print("e ^ 2.5 is",math.exp(2.5))  
Output:

  
expm1 function

This function works same like exp function but returns exp(x)-1. Here expm1 means exp-m-1 that is exp-minus-1.
Example:
  1. #Importing math Module  
  2. import math  
  3.   
  4. print(math.exp(5)-1)  
  5. print(math.expm1(5))  
Output:

   
log function

log function log(x[,base]) finds log of the given number like x with base "e" default. Here base is the optional parameter. If you want to calculate log with specific base then you can specify the base.
Example:
  1. #Importing math Module  
  2. import math  
  3.   
  4. # log with Default base e  
  5. print(math.log(2))  
  6.   
  7. # log with specific base  
  8. print(math.log(64,2))  
Output:

  
log1p function

This is similar to log function but adds 1 in log result. log1p means log-1-p that is log-1-plus.
Example:
  1. #Importing math Module  
  2. import math  
  3.   
  4. print(math.log1p(2))   
Output:

 
log10 function

This calculates log with base 10.
Example:
  1. #Importing math Module  
  2. import math  
  3.   
  4. print(math.log10(1000))  
Output:

  
pow function

This is used to find out the power of the number. Syntax of the pow function is pow(Base, Power). It takes 2 arguments ;1st is base and 2nd is power.
Example: 
  1. #Importing math Module  
  2. import math  
  3.   
  4. print(math.pow(5,4))   
Output: 

 
sqrt function

This function is used to find out the square root of the number. It takes number as a argument and finds square root.
Example: 
  1. #Importing math Module  
  2. import math  
  3.   
  4. print(math.sqrt(256))  
Output:

  
Trigonometric Functions

In python we have the following trigonometric functions.
  1. sin: Takes one parameter in radian and return sine of that radian.
  2. cos: Takes one parameter in radian and return cosine of that radia.
  3. tan: Takes one parameter in radian and return tangent of that radian.
  4. asin: Takes one parameter and returns arc sine(Inverse of sine).
  5. acos: Takes one parameter and returns arc cosine(Inverse of cosine).
  6. atan: Takes one parameter and returns arc tangent(inverse of tangent).
  7. sinh: Takes one parameter and returns hyperbolic sine.
  8. cosh: Takes one parameter and returns hyperbolic cosine.
  9. tanh: Takes one parameter and returns hyperbolic tangent.
  10. asinh: Takes one parameter and returns inverse hyperbolic sine.
  11. acosh: Takes one parameter and returns inverse hyperbolic cosine.
  12. atanh: Takes one parameter and returns inverse hyperbolic tangent.
Example:
  1. #Importing math Module  
  2. import math  
  3.   
  4. #sin function  
  5. print("sin PI/2 :",math.sin(math.pi/2))  
  6. #cos function  
  7. print("cos 0 :",math.cos(0))  
  8. #tan function  
  9. print("tan PI/4 :",math.tan(math.pi/4))  
  10.   
  11. print()  
  12.   
  13. #asin finction  
  14. print("asin 0 :",math.acos(0))  
  15. #acos function  
  16. print("acos 1 :",math.acos(1))  
  17. #atan function  
  18. print("atan 0.5 :",math.atan(0.5))  
  19.   
  20. print()  
  21.   
  22. #sinh function  
  23. print("sinh 1 :",math.sinh(1))  
  24. #cosh function  
  25. print("cosh 0 :",math.cos(0))  
  26. #tanh function  
  27. print("tanh 1 :",math.tan(1))  
  28.   
  29. print()  
  30.   
  31. #asinh finction  
  32. print("asinh 1 :",math.acosh(1))  
  33. #acosh function  
  34. print("acosh 1 :",math.acosh(1))  
  35. #atanh function  
  36. print("atanh 0.5 :",math.atanh(0.5))  
Output:

 

Angular Conversion Function

These functions convert angle. In maths we can write angles in the following two ways: degree and radian. There are two functions in Python which converts degree to radian and vice versa.
  • degrees: Converts radian to degrees. 
  • radians: Converts degree to radians.  
Example: 
  1. #Importing math Module  
  2. import math  
  3.   
  4. print(math.degrees(1.57))  
  5.   
  6. print(math.radians(90))  
Output:

 
 
Math Constants

In python there are two math constants. pi and e  
  1. pi : This is the mathematical constant whose value is : 3.1416
  2. : This is also the mathematical constant whose value is : 2.7183
Example
  1. #Importing math Module  
  2. import math  
  3.   
  4. #printing value of PI  
  5. print("value of PI is",math.pi)  
  6. #printing value of e  
  7. print("value of e is",math.e)  
Output

  

My Other Python Article links