Fonctions mathématiques en Python | Ensemble 1 (fonctions numériques)
En Python, un certain nombre d'opérations mathématiques peuvent être effectuées facilement en important un module nommé « math » qui définit diverses fonctions qui facilitent nos tâches. 1. plafond() :- Cette fonction renvoie le plus petite valeur intégrale supérieure au nombre . Si le nombre est déjà un nombre entier, le même nombre est renvoyé. 2. étage() :- Cette fonction renvoie le plus grande valeur intégrale inférieure au nombre . Si le nombre est déjà un nombre entier, le même nombre est renvoyé.
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 ))
Sortir:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
Complexité temporelle : O(1)
Espace auxiliaire : O(1)
3. fabs() :- Cette fonction renvoie le valeur absolue du numéro. 4. factorielle() :- Cette fonction renvoie le factorielle du numéro. Un message d'erreur s'affiche si le nombre n'est pas entier.
# 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 ))
Sortir:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
Complexité temporelle : O(b)
Espace auxiliaire : O(1)
5. copiesign(a b) :- Cette fonction renvoie le numéro avec le valeur de 'a' mais avec le signe de 'b' . La valeur renvoyée est de type float. 6. pgcd() :- Cette fonction est utilisée pour calculer le plus grand diviseur commun de 2 nombres mentionné dans ses arguments. Cette fonction fonctionne sous python 3.5 et supérieur.
# 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 ))
Sortir:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
Complexité temporelle : O(min(cd))
Espace auxiliaire : O(1)