Number Theoretic And Representation Functions In Python

In this article, We will look at number theoretic and representation functions that are available in the math module.

Which are used to make different calculations on numeric values.

Note:  This function can not be used with complex numbers. To do so use a function with the same name from the cmath module which supports complex numbers.

1.  math.ceil(x)

This function returns a real number let’s say (y) whose value is equal to the nearest integer (x) and is greater than or equal to the (x).

import math
# positive numbers
x = 2.65
y = math.ceil(x) # y = 3.0
print(y)

# float numbers
x = 2.000001
y = math.ceil(x) # y = 3.0
print(y)

#  negative numbers
x = -2.95 # y = -2.0
y = math.ceil(x)
print(y)

# an integer 
x = 13
y = math.ceil(x) # y = 13.0
print(y)

#complex
x = 3 +6j
y = math.ceil(x) # y = 13.0
print(y)

Output:
3
3
-2
13
TypeError: can not convert complex to float

Here when we try to convert complex numbers it will throw an error. also, this ceil function result type is float.

2. math.floor(x)

Returns the floor value. which is equal to the nearest number and is less than or equal to (x).

import math
# positive numbers
x = 2.65
y = math.floor(x) # y = 3.0
print(y)

# float numbers
x = 2.000001
y = math.floor(x) # y = 3.0
print(y)
#  negative numbers
x = -2.95 # y = -2.0
y = math.floor(x)
print(y)
# an integer 
x = 13
y = math.floor(x) # y = 13.0
print(y)


Output: 
2
2
-3
13

3. math.copysign(x, y)

Returns a float value with the absolute value of x but the sign of y.

import math

# Function math.copysign(x,y)

x = 3.64
y = -5.44
z = math.copysign(x, y) 
print(z)

x = -2.85
y = 0.00001
z = math.copysign(x, y)
print(z)

# case when y = -0.0
x = 2.85
y = -0.0 
z = math.copysign(x, y) 
print(z)

# case when y = 0.0
x = 3.18
y = 0.0
z = math.copysign(x, y) 
print(z)


Output:
-3.64
2.85
-2.85
3.18

4. math.fabs(x)

Return the absolute value of the argument.

#math.fabs()
import math

x = -15.55
y = math.fabs(x) 
print(y)

Output:
15.55

5. math.factorial(x)

Return factorial value of x. if it is negative than return value error.

#math.factorial()

import math
x = 5.0
y = math.factorial(x) 
print(y)

# float value
x = 5.5 
y = math.factorial(x) 
print(y)

#negative value 
x = -5.0 
y = math.factorial(x) 
print(y)

Output:
120
value error: only accept the integral value
value error: factorial not defined for negative numbers

6. math.fmod(x,y)

It is generally used when working with float values instead of the % operator in python. also, the return type is float while % return integer value.

# Function math.fmod(x)
import math

# float arguments 
x = 5.2
y = 0.4
y = math.fmod(x, y) # y = 0.3999999999999999
print(y)

#  integer arguments
x = 15
y = 4
z = math.fmod(x, y) 
v = x % y 
print(z)
print(v)

Output:
0.3999999999999999
3.0
3

7. math.gcd(x, y)

This function is introduced in python 3.5. It returns the greatest common divisor of its arguments.

# Function gcd(a,b)
import math
a = 60
b = 40
c = math.gcd(a,b) # c = 20


a = -60
b = 24
c = math.gcd(a,b) # c = 12

8. math.trunc(x)

This function returns a value that is equal to the integer part of (x)

# Function trunc(x)
import math

x = 2.8
y = math.trunc(x) # y = 2

x = -3.001
y = math.trunc(x) # y = -3

9. math.comb(n,k)

Return number of ways to choose k items from n items without repetition and without order. And raise value error if either value is negative or Type error if the value is not an integer.

# math.comb()
import math

#  items to choose from
n = 3

#  possibilities to choose
k = 2

# Print total number of possible combinations
print(math.comb(n, k))

Output: 
3

10. math.fsum(x)

This function calculates the exact sum of floating points in iterable.

# Function math.fsum(x)
import math

y = math.fsum([3.2, 3.2, 3.2 ,3.2, 3.2]) # y = 0.1
print(y)

Output: 
16

11. math.lcm(x,y)

This function is introduced in version 3.9. It returns the lcm of given arguments and if  any one argument is zero than return value is zero. and without arguments it returns 1.

#function math.lcm()
import math;
z= math.lcm(24,12)
print (z);

#with one value as zero 
z= math.lcm(24,0)
print (z);

#without arguments 
z= math.lcm()
print (z);


Output: 
24
0
1

12. math.prod(iterable, *, start=1)

This function returns the product of all elements in the input iterable. The default start value for prod is 1.

Generally used with numeric types may reject non-numeric types. And this was introduced in python 3.8.

#math.prod()

import math
a=[2,3,4,5,6,]
x= math.prod(a,start=1)
print(x)

Output:
720

 13. math.isfinite(x)

return true if the value is neither nan nor infinite.

14.  math.isinf(x)

Return true if the value is positive or negative infinite.

15.   math.isnan(x)

Return true if  a value is nan.

16.   math.isqrt(x)

Return the square root of non negative integer n.

#math.isfinite()
import math
x= math.isfinite(10023)
print(x) # x-- true

#math.isinf()
x= math.isinf(10023)
print(x) #x-- false 

#math.isnan()
import math
q=float("nan")
x= math.isnan(q)
print(x) #x--true

#math.isqrt()
import math
x= math.isqrt(9)
print(x) #x--3

That is pretty much. for more give a visit to the official documentation.

Thanks for read.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories