Fonctions décimales en Python | Ensemble 1

Python dans sa définition fournit certaines méthodes pour effectuer une arithmétique décimale à virgule flottante plus rapide en utilisant le module « decimal ». 
Opérations importantes sur les décimales
1. sqrt() :- Cette fonction calcule le racine carrée du nombre décimal.
2. exp() :- Cette fonction renvoie le e^x (exposant) du nombre décimal.
 

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  )   

Sortir: 

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


3. ln() :- Cette fonction est utilisée pour calculer logarithme népérien du nombre décimal.
4. log10() :- Cette fonction est utilisée pour calculer journal (base 10) d'un nombre décimal.
 

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  )   

Sortir: 

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


5. as_tuple() :- Renvoie le nombre décimal sous forme de tuple contenant 3 arguments signe (0 pour + 1 pour -) chiffres et valeur de l'exposant .
6. fma(ab) :- Ce 'fma' signifie fusionné multiplier et ajouter . Il calcule (num*a)+b à partir des chiffres en argument. Aucun arrondi de (num*a) a lieu dans cette fonction.
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  (  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  )   

Sortir: 

 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. comparer() :- Cette fonction est utilisée pour comparer des nombres décimaux. Renvoie 1 si le 1er argument décimal est supérieur au 2e -1 si le 1er argument décimal est inférieur au 2e et 0 si les deux sont égaux.
8. compare_total_mag() : - Compare l'ampleur totale des nombres décimaux. Renvoie 1 si le 1er argument décimal est supérieur au 2e (signe ignoré) -1 si le 1er argument décimal est inférieur au 2e (signe ignoré) et 0 si les deux sont égaux (signe ignoré).
 

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

Sortir: 

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


9. copie_abs() :- Cette fonction imprime le absolu valeur de l'argument décimal.
10. copie_negate() :- Cette fonction imprime le négation d'argument décimal.
11. copie_sign() :- Cette fonction imprime le premier argument en copiant le signe du 2ème argument .
 

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

Sortir: 

 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. maximum() :- Cette fonction calcule le maximum de deux nombres décimaux.
13. min() :- Cette fonction calcule le minimum de deux nombres décimaux.
 

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

Sortir: 

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