Python math module : Number-theoretic and representation functions

जैसा की आप जानते है python मे math module के अंदर बहुत सारे फंक्शन होते है इनका उपयोग  mathematical operation मे किया जाता है।  इन फंक्शन को अलग अलग category मे रखा गया है जिसमे से आज हम Number-theoretic and representation functions के बारे मे जानेंगे।

Number-theoretic and representation functions


math.fabs() :  ये फंक्शन दिए गए नंबर के absolute value को return करता है।

Syntax :

math.fabs(x)

Source Code :

import math

print("abs value is : ",math.fabs(15.9))
print("abs value is : ",math.fabs(-16.58))

OUTPUT  : 
abs value is : 15.9
abs value is : 16.58

math.factorial() : ये फंक्शन दिए गए नंबर के factorial को return करता है। लेकिन दिया गया नंबर यदि floating point या negative number होता है तो ‘valueError’ exception आती है।

Syntax :

math.factorial(x)

Source Code :

Example 1: 
import math

a= math.factorial(7)
b= math.factorial(4)
print("factorial is : ", a)
print("factorial is : ", b)

OUTPUT :
factorial is : 5040
factorial is : 24

Example 2: 
import math

a= math.factorial(-9)
print("factorial is : ", a)

OUTPUT :
Traceback (most recent call last):
File "C:/Users/home/PycharmProjects/untitled/aa.py",line 3,in <module>
a= math.factorial(-9)
ValueError: factorial() not defined for negative values


math.ceil() :  ये फंक्शन दिए गए नंबर के ceiling value  को return करता है।  यदि parameter मे x को floating-point number दिया जाता है तो यह फंक्शन उससे बड़ी integer value को return करता है |

Syntax :

math.ceil(x)

Source Code :

import math

print(math.ceil(9.09))
print(math.ceil(3.85))
print(math.ceil(7.0))

OUTPUT : 
10
4
7

math.floor() : ये फंक्शन दिए गए नंबर के floor value  को return करता है।  यदि parameter मे x को floating-point number दिया जाता है  तो यह फंक्शन उससे छोटी integer value को return करता है |

Syntax :

math.floor(x)

Source Code :

import math

print("floor value :",math.floor(2.49))
print("(floor value :",math.floor(-10.80))

OUTPUT :

floor value : 2
(floor value : -11

math.fmod() :  ये Function दिए गए x नंबर को y नंबर से divide करके उनका remainder को floating point number मे return करता है |

Syntax :

math.fmod(x, y)

यदि parameter मे x और y दोनों की वैल्यू या सिर्फ y की वैल्यू को शून्य (0) वैल्यू दी जाती है तो यहां ‘valueError’ exception आती है।

Source Code :

Example 1:
import math

print(math.fmod(23, 4))
print(math.fmod(0, 5))

OUTPUT :
3.0
0.0

Example 2:
import math
print(math.fmod(0,0))

OUTPUT :
Traceback (most recent call last):
File "C:/Users/home/PycharmProjects/untitled/aa.py",line 3,in <module>
print(math.fmod(0,0))
ValueError: math domain error

math.gcd() : ये Function दिए गए नंबर X और Y का gcd (Greatest Common Divisor) return करता है। लेकिन  X और Y को floating point value नहीं दे सकते यदि ऐसा करते है तो type error आ जाती है।

Syntax :

math.gcd(x, y)

Source Code :

Example 1: 
import math

print(math.gcd(6, 4))
print(math.gcd(84, 24))

OUTPUT : 
2
12

Example 2: 
import math

print(math.gcd(6.4, 4.2))

OUTPUT : 
Traceback (most recent call last):
File "C:/Users/home/PycharmProjects/untitled/aa.py",line 3,in <module>
print(math.gcd(6.4, 4.2))
TypeError: 'float' object cannot be interpreted as an integer