logo

Funções matemáticas em Python | Conjunto 1 (Funções Numéricas)

Em python, uma série de operações matemáticas podem ser realizadas facilmente importando um módulo chamado 'math' que define várias funções que facilitam nossas tarefas. 1. teto() : - Esta função retorna o menor valor integral maior que o número . Se o número já for inteiro, o mesmo número será retornado. 2. andar() : - Esta função retorna o maior valor integral menor que o número . Se o número já for inteiro, o mesmo número será retornado. 

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

Saída:



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

Complexidade de tempo: O(1)

Espaço Auxiliar: O(1)


3. fabulosos() : - Esta função retorna o valor absoluto do número. 4. fatorial() : - Esta função retorna o fatorial do número. Uma mensagem de erro será exibida se o número não for 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)) 

Saída:

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

Complexidade de tempo: Ó (b)

Espaço Auxiliar: O(1)




5. copysign(a b) : - Esta função retorna o número com o valor de 'a' mas com o sinal de 'b' . O valor retornado é do tipo float. 6. mdc() : - Esta função é usada para calcular o máximo divisor comum de 2 números mencionado em seus argumentos. Esta função funciona em python 3.5 e superior. 

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

Saída:

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

Complexidade de tempo: O(min(cd))

Espaço Auxiliar: O(1)


linguagem de computador bacana