logo

Matematiske funktioner i Python | Sæt 1 (numeriske funktioner)

I python kan en række matematiske operationer let udføres ved at importere et modul ved navn 'math', som definerer forskellige funktioner, som gør vores opgaver lettere. 1. loft() :- Denne funktion returnerer mindste integralværdi større end tallet . Hvis tallet allerede er heltal, returneres det samme tal. 2. etage() :- Denne funktion returnerer største integralværdi mindre end tallet . Hvis tallet allerede er heltal, returneres det samme tal. 

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

Produktion:

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

Tidskompleksitet: O(1)



Hjælpeplads: O(1)


3. fabs() :- Denne funktion returnerer absolut værdi af nummeret. 4. factorial() :- Denne funktion returnerer faktorielle af nummeret. Der vises en fejlmeddelelse, 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)) 

Produktion:

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

Tidskompleksitet: O(b)

Hjælpeplads: O(1)


5. kopitegn(a b) :- Denne funktion returnerer tallet med værdien af ​​'a' men med tegnet 'b' . Den returnerede værdi er flydende type. 6. gcd() :- Denne funktion bruges til at beregne største fælles deler af 2 tal nævnt i sine argumenter. Denne funktion 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)) 

Produktion:

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

Tidskompleksitet: O(min(cd))

Hjælpeplads: O(1)


afinstaller angular cli