logo

Matematické funkce v Pythonu | Sada 1 (numerické funkce)

V pythonu lze snadno provádět řadu matematických operací importem modulu s názvem 'math', který definuje různé funkce, které nám usnadňují úkoly. 1. ceil() :- Tato funkce vrací nejmenší integrální hodnota větší než číslo . Pokud je číslo již celé číslo, vrátí se stejné číslo. 2. patro() :- Tato funkce vrací největší integrální hodnota menší než číslo . Pokud je číslo již celé číslo, vrátí se stejné číslo. 

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

výstup:



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

Časová náročnost: O(1)

Pomocný prostor: O(1)


3. fabs() :- Tato funkce vrací absolutní hodnota čísla. 4. faktoriál() :- Tato funkce vrací faktoriál čísla. Pokud číslo není integrální, zobrazí se chybové hlášení. 



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

výstup:

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

Časová náročnost: O(b)

Pomocný prostor: O(1)




5. copysign(a b) :- Tato funkce vrací číslo s hodnota „a“, ale se znaménkem „b“ . Vrácená hodnota je typu float. 6. gcd() :- Tato funkce se používá k výpočtu největší společný dělitel 2 čísel zmíněný ve svých argumentech. Tato funkce funguje v pythonu 3.5 a výše. 

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

výstup:

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

Časová náročnost: O(min(cd))

Pomocný prostor: O(1)


linuxový hostitel