Open In App

Mathematical Functions in Python | Set 1 (Numeric Functions)

Last Updated : 30 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In python a number of mathematical operations can be performed with ease by importing a module named “math” which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns the greatest integral value smaller than the number. If number is already integer, same number is returned. 

Python




# Python code to demonstrate the working of
# ceil() and floor()
 
# importing "math" for mathematical operations
import math
 
a = 2.3
 
# returning the ceil of 2.3
print ("The ceil of 2.3 is : ", end="")
print (math.ceil(a))
 
# returning the floor of 2.3
print ("The floor of 2.3 is : ", end="")
print (math.floor(a))


Output:

The ceil of 2.3 is : 3
The floor of 2.3 is : 2

Time Complexity: O(1)

Auxiliary Space: O(1)

3. fabs() :- This function returns the absolute value of the number. 4. factorial() :- This function returns the factorial of the number. An error message is displayed if number is not integral. 

Python




# Python code to demonstrate the working of
# fabs() and factorial()
 
# importing "math" for mathematical operations
import math
 
a = -10
 
b= 5
 
# returning the absolute value.
print ("The absolute value of -10 is : ", end="")
print (math.fabs(a))
 
# returning the factorial of 5
print ("The factorial of 5 is : ", end="")
print (math.factorial(b))


Output:

The absolute value of -10 is : 10.0
The factorial of 5 is : 120

Time Complexity: O(b)

Auxiliary Space: O(1)

5. copysign(a, b) :- This function returns the number with the value of ‘a’ but with the sign of ‘b’. The returned value is float type. 6. gcd() :- This function is used to compute the greatest common divisor of 2 numbers mentioned in its arguments. This function works in python 3.5 and above. 

Python




# Python code to demonstrate the working of
# copysign() and gcd()
 
# importing "math" for mathematical operations
import math
 
a = -10
b = 5.5
c = 15
d = 5
 
# returning the copysigned value.
print ("The copysigned value of -10 and 5.5 is : ", end="")
print (math.copysign(5.5, -10))
 
# returning the gcd of 15 and 5
print ("The gcd of 5 and 15 is : ", end="")
print (math.gcd(5,15))


Output:

The copysigned value of -10 and 5.5 is : -5.5
The gcd of 5 and 15 is : 5

Time Complexity: O(min(c,d))

Auxiliary Space: O(1)



Similar Reads

Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Convert Factor to Numeric and Numeric to Factor in R Programming
Factors are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels. They can be stored as integers with a corresponding label to every unique integer. Though factors may look similar to character vectors, they are integers, and care must be taken while using them as strings. The fac
5 min read
Python | Mathematical Median of Cumulative Records
Sometimes, while working with Python tuple list, we can have a problem in which we need to find the median of tuple values in the list. This problem has the possible application in many domains including mathematics. Let’s discuss certain ways in which this task can be performed. Method #1 : Using loops + "~" operator This task can be performed in
8 min read
Plot Mathematical Expressions in Python using Matplotlib
For plotting equations we will use two modules Matplotlib.pyplot and Numpy. This module helps you to organize your Python code logically. Numpy Numpy is a core library used in Python for scientific computing. This Python library supports you for a large, multidimensional array object, various derived objects like matrices and masked arrays, and ass
5 min read
Evaluate the Mathematical Expressions using Tkinter in Python
This article focuses on the evaluation of mathematical expression using the Tkinter and math packages in Python. Tkinter: Python Tkinter is a GUI programming package or built-in package. Tkinter provides the Tk GUI toolkit with a potent object-oriented interface. Python with Tkinter is the fastest and easiest way to create GUI applications. Creatin
2 min read
Mathematical Foundations in Tensorflow
Before constructing a basic TensorFlow program, it's critical to grasp the mathematical ideas required for TensorFlow. Any machine learning algorithm's core is considered mathematics. A strategy or solution for a certain machine learning algorithm is established with the aid of key mathematical principles. Let's dive into the mathematical foundatio
4 min read
How to define a mathematical function in SymPy?
SymPy is a Python Library that makes 'Symbolic Computation' possible in Python. Mathematical Functions using SymPy We can define mathematical functions by using SymPy in Python. There are two types of functions that we can define with the help of SymPy: 'Undefined Functions' and 'Custom Functions'. Undefined Functions: A programmer can create 'Unde
4 min read
Numpy | Mathematical Function
NumPy contains a large number of various mathematical operations. NumPy provides standard trigonometric functions, functions for arithmetic operations, handling complex numbers, etc. Trigonometric Functions –NumPy has standard trigonometric functions which return trigonometric ratios for a given angle in radians. numpy.sin(x[, out]) = ufunc ‘sin’)
9 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg