logo

Funzioni matematiche in Python | Set 1 (Funzioni numeriche)

In Python è possibile eseguire facilmente una serie di operazioni matematiche importando un modulo denominato "math" che definisce varie funzioni che semplificano i nostri compiti. 1. cella() :- Questa funzione restituisce il file valore integrale più piccolo maggiore del numero . Se il numero è già intero viene restituito lo stesso numero. 2. piano() :- Questa funzione restituisce il file valore integrale massimo minore del numero . Se il numero è già intero viene restituito lo stesso numero. 

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)) 

Produzione:



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

Complessità temporale: O(1)

Spazio ausiliario: O(1)


3. favolosi() :- Questa funzione restituisce il file valore assoluto del numero. 4. fattoriale() :- Questa funzione restituisce il file fattoriale del numero. Viene visualizzato un messaggio di errore se il numero non è intero. 



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)) 

Produzione:

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

Complessità temporale: O(b)

Spazio ausiliario: O(1)




5. copysign(a b) :- Questa funzione restituisce il numero con il valore di 'a' ma con il segno di 'b' . Il valore restituito è di tipo float. 6. mcd() :- Questa funzione viene utilizzata per calcolare il massimo comun divisore di 2 numeri citato nelle sue argomentazioni. Questa funzione funziona in Python 3.5 e versioni successive. 

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(515)) 

Produzione:

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

Complessità temporale: O(min(cd))

Spazio ausiliario: O(1)


java int in carattere