Децималне функције у Питхон-у | Сет 1

Питхон у својој дефиницији пружа одређене методе за извођење брже децималне аритметике са плутајућим зарезом помоћу модула 'децимал'. 
Важне операције над децималима
1. скрт() :- Ова функција израчунава квадратни корен децималног броја.
2. екп() :- Ова функција враћа е^к (експонент) децималног броја.
 

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  )   

Излаз: 

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


3. лн() :- Ова функција се користи за израчунавање природни логаритам децималног броја.
4. лог10() :- Ова функција се користи за израчунавање дневник (база 10) децималног броја.
 

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  )   

Излаз: 

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


5. ас_тупле() :- Враћа децимални број као тупле који садржи 3 аргумента знак (0 за + 1 за -) цифре и вредност експонента .
6. фма(аб) :- Ово 'фма' значи спојени помножити и додати . Рачуна (број*а)+б од бројева у аргументу. Нема заокруживања од (нум*а) одвија у овој функцији.
Пример:  
 

 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  )   

Излаз: 

 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. упореди() :- Ова функција се користи за поређење децималних бројева. Враћа 1 ако је 1. децимални аргумент већи од 2. -1 ако је 1. децимални аргумент мањи од 2. и 0 ако су оба једнака.
8. цомпаре_тотал_маг() :- Упоређује укупну величину децималних бројева. Враћа 1 ако је 1. децимални аргумент већи од 2. (знак игнорисања) -1 ако је 1. децимални аргумент мањи од 2. (знак игнорисања) и 0 ако су оба једнака (знак игнорисања).
 

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

Излаз: 

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


9. цопи_абс() :- Ова функција штампа апсолутни вредност децималног аргумента.
10. цопи_негате() :- Ова функција штампа негација децималног аргумента.
11. цопи_сигн() :- Ова функција штампа први аргумент копирањем знака из 2. аргумента .
 

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

Излаз: 

 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. мак() :- Ова функција израчунава максимум од два децимална броја.
13. мин() :- Ова функција израчунава минимум од два децимална броја.
 

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

Излаз: 

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