Funcions matemàtiques en Python | Conjunt 1 (funcions numèriques)

A Python es poden realitzar diverses operacions matemàtiques amb facilitat important un mòdul anomenat "matemàtiques" que defineix diverses funcions que facilita les nostres tasques. 1. cel () :- Aquesta funció retorna el valor integral més petit més gran que el nombre . Si el número ja és enter, es retorna el mateix nombre. 2. pis() :- Aquesta funció retorna el valor integral més petit que el nombre . Si el número ja és enter, es retorna el mateix nombre. 

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

Sortida:

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

Complexitat temporal: O(1)

Espai auxiliar: O(1)


3. Fabs() :- Aquesta funció retorna el valor absolut del nombre. 4. factorial() :- Aquesta funció retorna el factorial del nombre. Es mostra un missatge d'error si el número no és enter. 

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

Sortida:

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

Complexitat temporal: O(b)

Espai auxiliar: O(1)


5. copysign(a b) :- Aquesta funció retorna el número amb el valor de 'a' però amb el signe de 'b' . El valor retornat és de tipus flotant. 6. gcd() :- Aquesta funció s'utilitza per calcular el màxim comú divisor de 2 nombres esmentat en els seus arguments. Aquesta funció funciona a Python 3.5 i posteriors. 

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

Sortida:

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

Complexitat temporal: O(min(cd))

Espai auxiliar: O(1)