logo

Matematiske funksjoner i Python | Sett 1 (numeriske funksjoner)

I python kan en rekke matematiske operasjoner enkelt utføres ved å importere en modul kalt 'math' som definerer ulike funksjoner som gjør oppgavene våre enklere. 1. tak() :- Denne funksjonen returnerer minste integralverdi større enn tallet . Hvis tallet allerede er heltall, returneres samme tall. 2. etasje() :- Denne funksjonen returnerer største integralverdi mindre enn tallet . Hvis tallet allerede er heltall, returneres samme tall. 

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

Produksjon:



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

Tidskompleksitet: O(1)

Hjelpeplass: O(1)


3. fabs() :- Denne funksjonen returnerer absolutt verdi av nummeret. 4. faktoriell() :- Denne funksjonen returnerer faktoriell av nummeret. En feilmelding vises hvis nummeret ikke er integral. 



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

Produksjon:

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

Tidskompleksitet: O(b)

Hjelpeplass: O(1)




5. kopitegn(a b) :- Denne funksjonen returnerer tallet med verdien av 'a' men med tegnet 'b' . Den returnerte verdien er flytende type. 6. gcd() :- Denne funksjonen brukes til å beregne største felles deler av 2 tall nevnt i sine argumenter. Denne funksjonen fungerer i python 3.5 og nyere. 

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

Produksjon:

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

Tidskompleksitet: O(min(cd))

Hjelpeplass: O(1)


starter med java