Matematiska funktioner i Python | Set 1 (numeriska funktioner)

I python kan ett antal matematiska operationer enkelt utföras genom att importera en modul som heter 'math' som definierar olika funktioner som gör våra uppgifter enklare. 1. tak() :- Denna funktion returnerar minsta integralvärde större än talet . Om talet redan är heltal returneras samma nummer. 2. floor() :- Denna funktion returnerar största integralvärdet mindre än talet . Om talet redan är heltal returneras samma nummer. 

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 

Tidskomplexitet: O(1)

Hjälputrymme: O(1)


3. fabs() :- Denna funktion returnerar absolut värde av numret. 4. factorial() :- Denna funktion returnerar faktoriellt av numret. Ett felmeddelande visas om numret inte är 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 

Tidskomplexitet: O(b)

Hjälputrymme: O(1)


5. copysign(a b) :- Denna funktion returnerar numret med värdet på 'a' men med tecknet 'b' . Det returnerade värdet är flytande typ. 6. gcd() :- Denna funktion används för att beräkna största gemensamma delare av 2 tal nämns i sina argument. Denna funktion fungerar i python 3.5 och senare. 

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  (  5    15  ))   

Produktion:

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

Tidskomplexitet: O(min(cd))

Hjälputrymme: O(1)