funciones decimales en Python | Serie 1

Python en su definición proporciona ciertos métodos para realizar aritmética de coma flotante decimal más rápida utilizando el módulo «decimal». 
Operaciones importantes con decimales
1. sqrt() :- Esta función calcula la raíz cuadrada del número decimal.
2. exp() :- Esta función devuelve el e^x (exponente) del número decimal.
 

Python3

# Python code to demonstrate the working of
# sqrt() and exp()
 
# importing "decimal" module to use decimal functions
import decimal
 
# using exp() to compute the exponent of decimal number
a = decimal.Decimal(4.5).exp()
 
# using sqrt() to compute the square root of decimal number
b = decimal.Decimal(4.5).sqrt()
 
# printing the exponent
print ("The exponent of decimal number is : ",end="")
print (a)
 
# printing the square root
print ("The square root of decimal number is : ",end="")
print (b)

Producción: 
 

Python3

# Python code to demonstrate the working of
# ln() and log10()
 
# importing "decimal" module to use decimal functions
import decimal
 
# using ln() to compute the natural log of decimal number
a = decimal.Decimal(4.5).ln()
 
# using sqrt() to compute the log10 of decimal number
b = decimal.Decimal(4.5).log10()
 
# printing the natural logarithm
print ("The natural logarithm of decimal number is : ",end="")
print (a)
 
# printing the log10
print ("The log(base 10) of decimal number is : ",end="")
print (b)

Python3

# Python code to demonstrate the working of
# as_tuple() and fma()
 
# importing "decimal" module to use decimal functions
import decimal
 
# using as_tuple() to return decimal number as tuple
a = decimal.Decimal(-4.5).as_tuple()
 
# using fma() to compute fused multiply and addition
b = decimal.Decimal(5).fma(2,3)
 
# printing the tuple
print ("The tuple form of decimal number is : ",end="")
print (a)
 
# printing the fused multiple and addition
print ("The fused multiply and addition of decimal number is : ",end="")
print (b)

Python3

# Python code to demonstrate the working of
# compare() and compare_total_mag()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(9.53)
 
# Initializing decimal number
b = decimal.Decimal(-9.56)
 
# comparing decimal numbers using compare()
print ("The result of comparison using compare() is : ",end="")
print (a.compare(b))
 
# comparing decimal numbers using compare_total_mag()
print ("The result of comparison using compare_total_mag() is : ",end="")
print (a.compare_total_mag(b))

Python3

# Python code to demonstrate the working of
# copy_abs(),copy_sign() and copy_negate()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(9.53)
 
# Initializing decimal number
b = decimal.Decimal(-9.56)
 
# printing absolute value using copy_abs()
print ("The absolute value using copy_abs() is : ",end="")
print (b.copy_abs())
 
# printing negated value using copy_negate()
print ("The negated value using copy_negate() is : ",end="")
print (b.copy_negate())
 
# printing sign effected value using copy_sign()
print ("The sign effected value using copy_sign() is : ",end="")
print (a.copy_sign(b))

Python3

# Python code to demonstrate the working of
# min() and max()
 
# importing "decimal" module to use decimal functions
import decimal
 
# Initializing decimal number
a = decimal.Decimal(9.53)
 
# Initializing decimal number
b = decimal.Decimal(7.43)
 
# printing minimum of both values
print ("The minimum of two numbers is : ",end="")
print (a.min(b))
 
# printing maximum of both values
print ("The maximum of two numbers is : ",end="")
print (a.max(b))

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *