Funkcje matematyczne w Pythonie | Zestaw 1 (funkcje numeryczne)
W Pythonie wiele operacji matematycznych można z łatwością wykonać, importując moduł o nazwie „math”, który definiuje różne funkcje, co ułatwia nam zadania. 1. sufit() :- Ta funkcja zwraca najmniejsza wartość całkowita większa od liczby . Jeśli liczba jest już liczbą całkowitą, zwracana jest ta sama liczba. 2. piętro() :- Ta funkcja zwraca największa wartość całkowita mniejsza od liczby . Jeśli liczba jest już liczbą całkowitą, zwracana jest ta sama liczba.
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 ))
Wyjście:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
Złożoność czasowa: O(1)
Przestrzeń pomocnicza: O(1)
3. super() :- Ta funkcja zwraca wartość bezwzględna numeru. 4. silnia() :- Ta funkcja zwraca silnia numeru. Jeśli liczba nie jest całkowitą, wyświetlany jest komunikat o błędzie.
# 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 ))
Wyjście:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
Złożoność czasowa: O(b)
Przestrzeń pomocnicza: O(1)
5. podpis (a b) :- Ta funkcja zwraca liczbę za pomocą wartość „a”, ale ze znakiem „b” . Zwracana wartość jest typu zmiennoprzecinkowego. 6. gcd() :- Ta funkcja służy do obliczania największy wspólny dzielnik 2 liczb wspomniano w jej argumentach. Ta funkcja działa w Pythonie 3.5 i nowszych wersjach.
# 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 ))
Wyjście:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
Złożoność czasowa: O(min(cd))
Przestrzeń pomocnicza: O(1)