logo

Matematičke funkcije u Pythonu | Skup 1 (numeričke funkcije)

U pythonu se brojne matematičke operacije mogu izvesti s lakoćom uvozom modula pod nazivom 'math' koji definira različite funkcije što olakšava naše zadatke. 1. strop() :- Ova funkcija vraća najmanja integralna vrijednost veća od broja . Ako je broj već cijeli broj vraća se isti broj. 2. kat() :- Ova funkcija vraća najveća integralna vrijednost manja od broja . Ako je broj već cijeli broj vraća se isti broj. 

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

Izlaz:



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

Vremenska složenost: O(1)

Pomoćni prostor: O(1)


3. fabs() :- Ova funkcija vraća apsolutna vrijednost od broja. 4. faktorijel() :- Ova funkcija vraća faktorijel od broja. Prikazuje se poruka o pogrešci ako broj nije integralan. 



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

Izlaz:

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

Vremenska složenost: O(b)

Pomoćni prostor: O(1)




5. kopija (a b) :- Ova funkcija vraća broj s vrijednost 'a', ali s predznakom 'b' . Vraćena vrijednost je tipa float. 6. gcd() :- Ova se funkcija koristi za izračunavanje najveći zajednički djelitelj 2 broja spominje u svojim argumentima. Ova funkcija radi u pythonu 3.5 i novijim. 

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

Izlaz:

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

Vremenska složenost: O(min(cd))

Pomoćni prostor: O(1)


faktorijel u Javi