Python mathematical theory and representation functions
Python Mathematical Theory and Representation Functions
ceil() Function
The ceil() function from the math module returns the ceiling of x, that is, the smallest integer not less than x.
Syntax
The syntax of the ceil() function is as follows:
import math
math.ceil(x)
Note: This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x − This is a numeric expression.
Return Value
This function returns the smallest integer greater than or equal to “x.”
Example
The following example demonstrates the use of the ceil() function.
from math import ceil, pi
a = -45.17
ceil_a = ceil(a)
print ("a: ",a, "ceil(a): ", ceil_a)
a = 100.12
ceil_a = ceil(a)
print ("a: ",a, "ceil(a): ", ceil_a)
a = 100.72
ceil_a = ceil(a)
print ("a: ",a, "ceil(a): ", ceil_a)
a = pi
ceil_a = ceil(a)
print ("a: ",a, "ceil(a): ", ceil_a)
When we run the above program, it produces the following output: –
a: -45.17 ceil(a): -45
a: 100.12 ceil(a): 101
a: 100.72 ceil(a): 101
a: 3.141592653589793 ceil(a): 4
comb() Function
The comb() function in the math module returns the number of ways to choose x
items from y
items, without duplicates and in no order. It evaluates to n! / (x! * (x-y)!)
when x <= y and zero when x > y.
Syntax
The following is the syntax of the comb() function –
math.comb(x, y)
Parameters
- x – Required. A positive integer number to choose from.
-
y – Required. A positive integer number to choose from.
Return Value
If the value of “x” is greater than the value of “y”, 0 is returned. If both x and y are negative, a ValueError error is raised. If neither argument is an integer, a TypeError error is raised.
Example
from math import comb
x=7
y=5
combinations = comb(x,y)
print ("x: ",x, "y: ", y, "combinations: ", combinations)
x=5
y=7
combinations = comb(x,y)
print ("x: ",x, "y: ", y, "combinations: ", combinations)
This will produce the following output −
x: 7 y: 5 combinations: 21
x: 5 y: 7 combinations: 0
copysign() Function
The copy() function in the math module returns a floating-point number with the same sign as y but the same magnitude (absolute value) as x.
Syntax
The syntax of the copysign() function is as follows:
math.copysign(x, y)
Parameters
- x – A number
- y – A number
Return Value
Returns a floating-point number with the same absolute value as x but the same sign as y.
Example
from math import copysign
x=10
y=-20
result = copysign(x,y)
print ("x: ",x, "y: ", y, "copysign: ", result)
x=-10
y=20
result = copysign(x,y)
print ("x: ",x, "y: ", y, "copysign: ", result)
x=-10
y= -0.0
result = copysign(x,y)
print ("x: ",x, "y: ", y, "copysign: ", result)
It will produce the following output −
x: 10 y: -20 copysign: -10.0
x: -10 y: 20 copysign: 10.0
x: -10 y: -0.0 copysign: -10.0
fabs() Function
The fabs() function in the math module returns the absolute value of a given number. It always returns a floating-point number, even if the argument is an integer.
Syntax
The syntax of the fabs() function is as follows −
math.fabs(x)
Parameters
- x – A number
Return Value
Returns a floating-point number with the magnitude (absolute value) of x
Example
from math import fabs
x=10.25
result = fabs(x)
print ("x: ",x, "fabs value: ", result)
x=20
result = fabs(x)
print ("x: ",x,"fabs value: ", result)
x=-10
result = fabs(x)
print ("x: ",x, "fabs value: ", result)
This will generate the following output: −
x: 10.25 fabs value: 10.25
x: 20 fabs value: 20.0
x: -10 fabs value: 10.0
factorial() Function
The factorial() function in the math module returns the factorial of a given integer. The factorial is the product of all integers from 1 to that number. It is denoted by x! , where 4! = 4X3X2X1.
Syntax
The syntax of the factorial() function is as follows:
math.factorial(x)
Parameters
- x − An integer
Return Value
Returns an integer that is the product of the integers from 1 to x. For negative x, a ValueError is raised.
Example
from math import factorial
x=10
result = factorial(x)
print ("x: ",x, "x! value: ", result)
x=5
result = factorial(x)
print ("x: ",x,"x! value: ", result)
x=-5
result = factorial(x)
print ("x: ",x, "x! value: ", result)
It will produce the following output –
x: 10 x! value: 3628800
x: 5 x! value: 120
Traceback (most recent call last):
File "C:Usersmlathexamplesmain.py", line 14, in <module>
result = factorial(x)
^^^^^^^^^^^^^
ValueError: factorial() not defined for negative values
floor() Function
The floor() function returns the floor of x, that is, the largest integer not greater than x.
Syntax
The syntax of the floor() function is as follows:
import math
math.floor(x)
Note – This function cannot be accessed directly, so we need to import the math module and then use the math static object to call the function.
Parameters
- x – This is a numeric expression.
Return Value
This function returns the largest integer not greater than x.
Example
The following example demonstrates the use of the floor() function.
from math import floor, pi
a = -45.17
floor_a = floor(a)
print ("a: ",a, "floor(a): ", floor_a)
a = 100.12
floor_a = floor(a)
print ("a: ",a, "floor(a): ", floor_a)
a = 100.72
floor_a = floor(a)
print ("a: ",a, "floor(a): ", floor_a)
a=pi
floor_a = floor(a)
print ("a: ",a, "floor(a): ", floor_a)
When we run the above program, it produces the following output −
a: -45.17 floor(a): -46
a: 100.12 floor(a): 100
a: 100.72 floor(a): 100
a: 3.141592653589793 floor(a): 3
<h2>fmod() Function</h2>
<p>In the math module, the fmod() function returns the same result as x%y. However, fmod() gives a more accurate modulo result than the modulo operator.</p>
<h3>Syntax</h3>
<p>The following is the syntax for the fmod() function:</p>
<pre><code class="language-python line-numbers">import math
math.fmod(x, y)
Note: This function cannot be accessed directly, so we need to import the math module and then use the math static object to call it.
Parameters
- x: Dividend, which can be positive or negative.
-
y: Divisor, which can be positive or negative.
Return Value
This function returns the remainder when x is divided by y.
Example
The following example demonstrates the use of the fmod() function.
from math import fmod
a=10
b=2
c=fmod(a,b)
print ("a=",a, "b=",b, "fmod(a,b)=", c)
a=10
b=4
c=fmod(a,b)
print ("a=",a, "b=",b, "fmod(a,b)=", c)
a=0
b=10
c=fmod(a,b)
print ("a=",a, "b=",b, "fmod(a,b)=", c)
a=10
b=1.5
c=fmod(a,b)
print ("a=",a, "b=",b, "fmod(a,b)=", c)
When we run the above program, it will produce the following output.
a= 10 b= 2 fmod(a,b)= 0.0
a= 10 b= 4 fmod(a,b)= 2.0
a= 0 b= 10 fmod(a,b)= 0.0
a= 10 b= 1.5 fmod(a,b)= 1.0
frexp() Function
The frexp() function in the math module returns the mantissa and exponent pair (m, e) of x. m is a floating-point number, and e is an integer such that x is m * 2**e.
Syntax
The syntax of the frexp() function is as follows:
import math
math.frexp(x)
Note – This function cannot be accessed directly. We need to import the math module and then call this function using the math static object.
Parameters
- x – A positive or negative number.
Return Value
This function returns the power and exponent (m,n) such that m*2**e
is equal to x.
Example
The following example shows the usage of the frexp() function.
from math import frexp
x = 8
y = frexp(x)
print ("x:", x, "frex(x):", y)
print ("Cross-check")
m,n=y
x = m*2**n
print ("frexp(x): ", y, "x:", x)
This will produce the following output:
x: 8 frex(x): (0.5, 4)
Cross-check
frexp(x): (0.5, 4) x: 8.0
fsum() Function
The fsum() function in the math module returns the floating-point sum of all the numeric items in an iterable (e.g., a list, tuple, or array).
Syntax
The following is the syntax of the fsum() function:
import math
math.sum(x)
Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x − An array containing numbers. Calling it with any other type will result in a TypeError.
Return Value
This function returns the floating-point sum of all items in the iterable.
Example
The following example demonstrates the use of the fsum() function −
from math import fsum
x = [1,2,3,4,5]
y = fsum(x)
print ("x:", x, "fsum(x):", y)
x = (10,11,12)
y = fsum(x)
print ("x:", x, "fsum(x):", y)
x = [1,'a',2]
y = fsum(x)
print ("x:", x, "fsum(x):", y)
It will produce the following output: output –
x: [1, 2, 3, 4, 5] fsum(x): 15.0
x: (10, 11, 12) fsum(x): 33.0
Traceback (most recent call last):
File "C:Usersmlathexamplesmain.py", line 13, in <module>
y = fsum(x)
^^^^^^^
TypeError: must be a real number, not a str
gcd() Function
The gcd() function in the math module returns the greatest common divisor of all integer numbers. The return value is the greatest positive integer divisor of all its arguments. If all numbers are zero, 0 is returned. The gcd() function with no arguments returns 0.
Syntax
The following is the syntax for the gcd() function:
import math
math.gcd(x)
Note – This function cannot be accessed directly. You must first import the math module and then call it using the math static object.
Parameters
- x1, x2 – integers
Return Value
This function returns the greatest common divisor as an integer.
Example
The following example demonstrates the use of the gcd() function:
from math import gcd
x, y, z = 12, 8, 24
result = gcd(x, y, z)
print ("x: {} y: {} z: {}".format(x,y,z), "gcd(x,y, z):",result)
x, y, z = 12, 6, 9
result = gcd(x, y, z)
print ("x: {} y: {} z: {}".format(x,y,z), "gcd(x,y, z):",result)
x, y = 7, 12
result = gcd(x, y)
print ("x: {} y: {} z: {}".format(x,y,z), "gcd(x,y, z):",result) {}".format(x,y), "gcd(x,y):",result)
x, y = 0, 12
result = gcd(x, y)
print ("x: {} y: {}".format(x,y), "gcd(x,y):",result)
It will generate the following output –
x: 12 y: 8 z: 24 gcd(x,y, z): 4
x: 12 y: 6 z: 9 gcd(x,y,z): 3
x: 7 y: 12 gcd(x,y): 1
x: 0 y: 12 gcd(x,y): 12
isclose() function
isclose() This function is from the math module and returns True if the values of its two numeric arguments are close to each other, otherwise it returns False.
Syntax
The syntax of the isclose() function is as follows:
import math
math.isclose(x,y)
Note − This function is not directly accessible, so we need to import the math module and then call it using the math static object. Closeness is determined based on the given absolute and relative tolerances.
Parameters
- x − The first value to check for closeness.
-
y − The second value to check for closeness.
-
rel_tol − The relative tolerance (optional).
-
abs_tol − Minimum absolute tolerance (optional).
Return Value
This function returns True if x and y are close, otherwise it returns False.
Example
The following example shows the use of the isclose() function.
from math import isclose
x = 2.598
y = 2.597
result = isclose(x, y)
print ("x: {} y:{}".format(x,y), "isclose(x,y):",result)
x = 5.263
y = 5.263000001
result = isclose(x, y)
print ("x: {} y:{}".format(x,y), "isclose(x,y):",result)
It will produce the following output –
x: 2.598 y:2.597 isclose(x,y): False
x: 5.263 y:5.263000001 isclose(x,y): True
isfinite() Function
The isfinite() function in the math module returns True if its argument is neither infinite nor NaN, otherwise it returns False.
Syntax
The following is the syntax of the isfinite() function:
import math
math.isfinite(x)
Note – This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x – The numeric operand.
Return Value
If x is neither infinite nor NaN, this function returns True; otherwise, it returns False.
Example
The following example shows the usage of the isfinite() function:
from math import isfinite
x = 1.23E-5
result = isfinite(x)
print ("x:", x, "isfinite(x):",result)
x = 0
result = isfinite(x)
print ("x:", x, "isfinite(x):",result)
x = float("Inf")
result = isfinite(x)
print ("x:", x, "isfinite(x):",result)
It produces the following output –
x: 1.23e-05 isfinite(x): True
x: 0 isfinite(x): True
x: inf isfinite(x): False
isinf() Function
The isinf() function in the math module returns True if its argument is positive or negative infinity, and False otherwise. It is the inverse of the isfinite() function.
Syntax
The syntax of the isinf() function is as follows:
import math
math.isinf(x)
Note – This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x – Numeric operand
Return Value
Returns True if x is positive infinity or negative infinity, otherwise returns False.
Example
The following example shows the usage of the isinf() function:
from math import isinf
x = 1.23E-5
result = isinf(x)
print ("x:", x, "isinf(x):",result)
x = float("-Infinity")
result = isinf(x)
print ("x:", x, "isinf(x):",result)
x = float("Inf")
result = isinf(x)
print ("x:", x, "isinf(x):",result)
It will produce the following output –
x: 1.23e-05 isinf(x): False
x: -inf isinf(x): True
x: inf isinf(x): True
isnan() Function
The isnan() function in the math module returns True if x is NaN (not a number), otherwise it returns False.
Syntax
The syntax of the isnan() function is as follows −
import math
math.isnan(x)
Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x − Numeric operand
Return Value
Returns True if x is NaN (not a number), otherwise it returns False.
Example
The following example demonstrates the use of the isnan() function:
from math import isnan
x = 156.78
result = isnan(x)
print ("x:", x, "isnan(x):",result)
x = float("-Infinity")
result = isnan(x)
print ("x:", x, "isnan(x):",result)
x = float("NaN")
result = isnan(x)
print ("x:", x, "isnan(x):",result)
x = "NaN"
result = isnan(x)
print ("x:", x, "isnan(x):",result)
It will produce the following output −
x: 156.78 isnan(x): False
x: -inf isnan(x): False
x: nan isnan(x): True
Traceback (most recent call last):
File "C:Usersmlathexamplesmain.py", line 16, in <module>
result = isnan(x)
^^^^^^^^
TypeError: must be a real number, not str
Note that “NaN” is a string, not a NaN numeric operand.
isqrt() Function
The isqrt() function in the math module returns the integer square root of a nonnegative integer. This is a base for the exact square root of a given positive integer. The square of the result is less than or equal to the argument number.
Syntax
The following is the syntax of the isqrt() function –
import math
math.isqrt(x)
Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x − A non-negative integer.
Return Value
This function returns the square root of the nearest integer rounded to the floor.
Example
The following example demonstrates the use of the isqrt() function −
from math import isqrt, sqrt
x = 12
y = isqrt(x)
z = sqrt(x)
print ("x:", x, "isqrt(x):", y, "sqrt(x):", z)
x = 16
y = isqrt(x)
z = sqrt(x)
print ("x:", x, "isqrt(x):", y, "sqrt(x):", z)
x = -100
y = isqrt(x)
z = sqrt(x)
print ("x:", x, "isqrt(x):", y, "sqrt(x):", z)
For comparison, the sqrt() function is also calculated. sqrt() returns a float type, while isqrt() returns an integer type. For isqrt(), the number must be non-negative.
It will produce the following output −
x: 12 isqrt(x): 3 sqrt(x): 3.4641016151377544
x: 16 isqrt(x): 4 sqrt(x): 4.0
Traceback (most recent call last):
File "C:Usersmlathexamplesmain.py", line 14, in <module>
y = isqrt(x)
^^^^^^^^
ValueError: isqrt() argument must be nonnegative
lcm() Function
The lcm() function in the math module returns the least common multiple of two or more integer arguments. For nonzero arguments, it returns the smallest positive integer that is divisible by all of them. If any argument is zero, the return value is 0.
Syntax
The syntax of the lcm() function is as follows −
import math
math.lcm(x1, x2, . . )
Note: This function is not directly accessible, so we need to import the math module and then call it using the math static object.
Parameters
- x1, x2, . . – Integers
Return Value
This function returns the least common multiple of all integers.
Example
The following example demonstrates the use of the lcm() function:
from math import lcm
x = 4
y = 12
z = 9
result = lcm(x,y,z)
print ("x: {} y: {} z: {}".format(x,y,z), "lcm(x,y,z):", result)
x = 5
y = 15
z = 0
result = lcm(x,y,z)
print ("x: {} y: {} z: {}".format(x,y,z), "lcm(x,y,z):", result)
x = 4
y = 3
z = 6
result = lcm(x,y,z)
print ("x: {} y: {} z: {}".format(x,y,z), "lcm(x,y,z):", result) {}".format(x,y,z), "lcm(x,y,z):", result)
This will generate the following output −
x: 4 y: 12 z: 9 lcm(x,y,z): 36
x: 5 y: 15 z: 0 lcm(x,y,z): 0
x: 4 y: 3 z: 6 lcm(x,y,z): 12
ldexp() Function
In the math module, the ldexp() function returns the product of the first number and the exponential of the second number. Therefore, ldexp(x,y) returns x*2**y. This is the reverse operation of the frexp() function.
Syntax
The syntax of the ldexp() function is as follows:
import math
math.lcm(x,y)
Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x − A positive or negative number.
-
y − A positive or negative number.
Return Value
This function returns x*2**y
.
Example
The following example demonstrates the use of the ldexp() function −
from math import ldexp, frexp
x = 0.5
y = 4
z = ldexp(x,y)
print ("x: {} y: {}".format(x,y), "ldexp(x,y)", z)
print ("Cross-check")
x,y = frexp(z)
print ("ldexp value:", z, "frexp(z):",x,y )
It produces the following output: Output &minu;
x: 0.5 y: 4 ldexp(x,y) 8.0
Cross-check
ldexp value: 8.0 frexp(z): 0.5 4
modf() Function
The modf() method returns a two-element tuple containing the fractional and integer parts of the argument x. The signs of the two parts of x are the same as x. The integer part is returned as a floating-point number.
Syntax
The syntax of the modf() method is as follows −
import math
math.modf( x )
Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x − This is a numeric expression.
Return Value
This method returns the fractional and integer parts of x as a two-element tuple. Both parts have the same sign as x. The integer part is returned as a floating-point number.
Example
The following example demonstrates the use of the modf() method −
from math import modf, pi
a = 100.72
modf_a = modf(a)
print ("a: ",a, "modf(a): ", modf_a)
a = 19
modf_a = modf(a)
print ("a: ",a, "modf(a): ", modf_a)
a = pi
modf_a = modf(a)
print ("a: ",a, "modf(a): ", modf_a)
It produces the following output −
a: 100.72 modf(a): (0.71999999999999989, 100.0)
a: 19 modf(a): (0.0, 19.0)
a: 3.141592653589793 modf(a): (0.14159265358979312, 3.0)
nextafter() Function
The nextafter() function returns the next floating-point value after the decimal point where x is closest to y.
- If y > x, then x is incremented.
-
If y < x, then x is decremented.
Syntax
Below is the syntax of the nextafter() function:
import math
math.nextafter( x, y )
Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- x and y – numeric operands.
Return Value
This function returns the next floating-point value.
Example
The following example demonstrates the use of the nextafter() function:
from math import nextafter
x=1.5
y=100
z=nextafter(x, y)
print("x: {} y: {}".format(x,y), "nextafter(x,y)", z)
x=1.5
y=float("-inf")
z=nextafter(x, y)
print("x: {} y: {}".format(x,y), "nextafter(x,y)", z)
x=0
y=float("inf")
z=nextafter(x, y)
print("x: {} y: {}".format(x,y), "nextafter(x,y)", z)
This will produce the following output −
x: 1.5 y: 100 nextafter(x,y) 1.5000000000000002
x: 1.5 y: -inf nextafter(x,y) 1.4999999999999998
x: 0 y: inf nextafter(x,y) 5e-324
perm() Function
The perm() function computes permutations. It returns the number of ways to choose x items from y items without duplicates and in order.
The permutation is defined as xPy = x! / (x-y)!, where y <= x and 0 when y > x.
Syntax
The following is the syntax of the perm() function –
import math
math.perm( x, y )
Note − This function cannot be accessed directly, so we need to import the math module and then use the math static object to call the function.
Parameters
- x − Required. The positive integer term to be selected.
-
y − Required. The positive integer term to be selected.
Return Value
This function returns the permutation value x P y = x! / (x-y)!
Example
The following example shows the usage of the perm() function.
from math import perm
x=5
y=3
permutations = perm(x,y)
print ("x: ",x, "y: ", y, "permutations: ", permutations)
x=3
y=5
permutations = perm(x,y)
print ("x: ",x, "y: ", y, "permutations: ", permutations)
will produce the following output −
x: 5 y: 3 permutations: 60
x: 3 y: 5 permutations: 0
prod() function
prod() This function computes the product of all the numeric items in the given iterable (list, tuple) as an argument. The default value of the second argument is 1.
Syntax
The syntax of the prod() function is as follows:
import math
math.prod(iterable, start)
Note − This function cannot be accessed directly, so we need to import the math module and then call it using the math static object.
Parameters
- iterable − Required. Must contain numeric operands.
-
start − Defaults to 1.
Return Value
This function returns the product of all the items in the iterable.
Example
The following example demonstrates the use of the prod() function −
from math import prod
x = [2,3,4]
product = prod(x)
print ("x: ",x, "product: ", product)
x = (5,10,15)
product = prod(x)
print ("x: ",x, "product: ", product)
x = [2,3,4]
y = 3
product = prod(x, start=y)
print ("x: ",x,"start:", y, "product: ", product)
It will produce the following output −
x: [2, 3, 4] product: 24
x: (5, 10, 15) product: 750
x: [2, 3, 4] start: 3 product: 72
remainder() Function
The remainder() function returns the remainder of x minus y. This is the difference x – n*y, where n is the integer closest to the quotient x / y. If x / y falls exactly halfway between two consecutive integers, the nearest even integer is used as n.
Syntax
The syntax of the remainder() function is as follows:
import math
math.remainder(x, y)
Note: This function is not directly accessible, so we need to import the math module and then call it through the math static object.
Parameters
- x, y: Numeric operands. y must be nonzero.
Return Value
This function returns the remainder when x is divided by y.
Example
The following example demonstrates the use of the remainder() function:
from math import remainder
x = 18
y = 4
rem = remainder(x, y)
print ("x: ",x, "y:", y, "remainder: ", rem)
x = 22
y = 4
rem = remainder(x, y)
print ("x: ",x, "y:", y, "remainder: ", rem)
x = 15
y = float("inf")
rem = remainder(x, y)
print ("x: ",x, "y:", y, "remainder: ", rem)
x = 15
y = 0
rem = remainder(x, y)
print ("x: ",x, "y:", y, "remainder: ", rem)
It will produce the following output − −
x: 18 y: 4 remainder: 2.0
x: 22 y: 4 remainder: -2.0
x: 15 y: inf remainder: 15.0
Traceback (most recent call last):
File "C:Usersmlathexamplesmain.py", line 20, in <module>
rem = remainder(x, y)
^^^^^^^^^^^^^^^^
ValueError: math domain error
trunc() Function
The trunc() function returns the integer portion of a number, removing the decimal point. For positive numbers x, trunc() is equivalent to floor(), and for negative numbers x, it is equivalent to ceil().
Syntax
The syntax of the trunc() function is as follows −
import math
math.trunc(x)
Note − This function cannot be accessed directly, so we need to import the math module and then call this function using the math static object.
Parameters
- x − Numeric operand
Return Value
This function returns the integer portion of the operand.
Example
The following example shows the use of the trunc() function −
from math import trunc
x = 5.79
rem = trunc(x)
print ("x: ",x, "truncated: ", rem)
x = 12.04
rem = trunc(x)
print ("x: ",x, "truncated: ", rem)
x = -19.98
rem = trunc(x)
print ("x: ",x, "truncated: ", rem)
x = 15
rem = trunc(x)
print ("x: ",x, "truncated: ", rem)
Will produce the following output –
x: 5.79 truncated: 5
x: 12.04 truncated: 12
x: -19.98 truncated: -19
x: 15 truncated: 15
ulp() Function
ULP stands for “unit in last place.” The ulp() function returns the value of the least significant bit of a floating-point number x. For positive x, t