logo

Funcions decimals en Python | Set 1

Python en la seva definició proporciona certs mètodes per realitzar aritmètica decimal de coma flotant més ràpid mitjançant el mòdul 'decimal'. 
Operacions importants sobre decimals
1. quadrat() :- Aquesta funció calcula el arrel quadrada del nombre decimal.
2. exp() :- Aquesta funció retorna el e^x (exponent) del nombre decimal.
 

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

Sortida: 



The exponent of decimal number is : 90.01713130052181355011545675  
The square root of decimal number is : 2.121320343559642573202533086


3. ln() :- Aquesta funció s'utilitza per calcular logaritme natural del nombre decimal.
4. log10() :- Aquesta funció s'utilitza per calcular registre (base 10) d'un nombre decimal.
 

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

Sortida: 

The natural logarithm of decimal number is : 1.504077396776274073373258352  
The log(base 10) of decimal number is : 0.6532125137753436793763169118


5. com_tupla() :- Retorna el nombre decimal com a tupla que conté 3 arguments signe (0 per + 1 per -) dígits i valor de l'exponent .
6. fma(ab) :- Aquest 'fma' significa fusionat multiplicar i sumar . Es calcula (núm*a)+b a partir dels números en argument. Sense arrodoniment de (núm*a) té lloc en aquesta funció.
Exemple:  
 



decimal.Decimal(5).fma(23) --> (5*2)+3 = 13


 

Python
# 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(23) # 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) 

Sortida: 

The tuple form of decimal number is : DecimalTuple(sign=1 digits=(4 5) exponent=-1)  
The fused multiply and addition of decimal number is : 13


7. compara () :- Aquesta funció s'utilitza per comparar nombres decimals. Retorna 1 si el 1r argument decimal és més gran que el 2n -1 si el 1r argument decimal és menor que el 2n i 0 si tots dos són iguals.
8. compare_total_mag() :- Compara la magnitud total dels nombres decimals. Retorna 1 si el 1r argument decimal és més gran que el 2n (ignorant el signe) -1 si el 1r argument decimal és més petit que el 2n (ignorant el signe) i 0 si tots dos són iguals (ignorant el signe).
 



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

Sortida: 

The result of comparison using compare() is : 1  
The result of comparison using compare_total_mag() is : -1


9. copy_abs() :- Aquesta funció imprimeix el absolut valor de l'argument decimal.
10. copy_negate() :- Aquesta funció imprimeix el negació d'argument decimal.
11. copy_sign() :- Aquesta funció imprimeix el primer argument copiant el signe del segon argument .
 

cadena java a enter
Python
# 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)) 

Sortida: 

The absolute value using copy_abs() is : 9.5600000000000004973799150320701301097869873046875  
The negated value using copy_negate() is : 9.5600000000000004973799150320701301097869873046875
The sign effected value using copy_sign() is : -9.5299999999999993605115378159098327159881591796875


12. màxim() :- Aquesta funció calcula el màxim de dos nombres decimals.
13. min() :- Aquesta funció calcula el mínim de dos nombres decimals.
 

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

Sortida: 

The minimum of two numbers is : 7.429999999999999715782905696  
The maximum of two numbers is : 9.529999999999999360511537816