Matemaattiset funktiot Pythonissa | Sarja 1 (numeeriset funktiot)
Pythonissa useita matemaattisia operaatioita voidaan suorittaa helposti tuomalla "math"-niminen moduuli, joka määrittelee erilaisia tehtäviämme helpottavia toimintoja. 1. ceil() :- Tämä funktio palauttaa pienin integraaliarvo suurempi kuin luku . Jos luku on jo kokonaisluku, sama luku palautetaan. 2. kerros() :- Tämä funktio palauttaa suurin integraaliarvo pienempi kuin luku . Jos luku on jo kokonaisluku, sama luku palautetaan.
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 ))
Lähtö:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
Aika monimutkaisuus: O(1)
Aputila: O(1)
3. fabs() :- Tämä funktio palauttaa absoluuttinen arvo numerosta. 4. factorial() :- Tämä funktio palauttaa tekijällinen numerosta. Näyttöön tulee virheilmoitus, jos numero ei ole kiinteä.
# 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 ))
Lähtö:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
Aika monimutkaisuus: O(b)
Aputila: O(1)
5. kopiomerkki(a b) :- Tämä funktio palauttaa numeron, jossa on arvo 'a', mutta merkki 'b' . Palautettu arvo on float-tyyppi. 6. gcd() :- Tätä funktiota käytetään laskemaan 2 luvun suurin yhteinen jakaja perusteluissaan. Tämä toiminto toimii python 3.5:ssä ja uudemmissa.
# 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 ))
Lähtö:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
Aika monimutkaisuus: O(min(cd))
Aputila: O(1)