Funzioni decimali in Python | Insieme 1
Python nella sua definizione fornisce alcuni metodi per eseguire operazioni aritmetiche decimali in virgola mobile più veloci utilizzando il modulo 'decimal'.
Operazioni importanti sui decimali
1. sqrt() :- Questa funzione calcola il radice quadrata del numero decimale.
2. espressione() :- Questa funzione restituisce il file e^x (esponente) del numero decimale.
# 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 )
Produzione:
The exponent of decimal number is : 90.01713130052181355011545675
The square root of decimal number is : 2.121320343559642573202533086Python
3. ln() :- Questa funzione viene utilizzata per calcolare logaritmo naturale del numero decimale.
4.log10() :- Questa funzione viene utilizzata per calcolare logaritmo(base 10) di un numero decimale.
# 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 )Produzione:
The natural logarithm of decimal number is : 1.504077396776274073373258352
The log(base 10) of decimal number is : 0.6532125137753436793763169118
5. as_tuple() :- Restituisce il numero decimale come tupla contenente 3 argomenti firmano (0 per + 1 per -) cifre e valore dell'esponente .
6. fma(ab) :- Questo 'fma' sta per fuso moltiplicare e aggiungere . Calcola (num*a)+b dai numeri in discussione. Nessun arrotondamento di (num*a) avviene in questa funzione.
Esempio :
decimal.Decimal(5).fma(23) --> (5*2)+3 = 13Python
# 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 )Produzione:
The tuple form of decimal number is : DecimalTuple(sign=1 digits=(4 5) exponent=-1)
The fused multiply and addition of decimal number is : 13Python
7. confronta() :- Questa funzione viene utilizzata per confrontare i numeri decimali. Restituisce 1 se il primo argomento decimale è maggiore del secondo -1 se il primo argomento decimale è minore del secondo e 0 se entrambi sono uguali.
8. compare_total_mag() : - Confronta la grandezza totale dei numeri decimali. Restituisce 1 se il primo argomento decimale è maggiore del secondo (segno ignorato) -1 se il primo argomento decimale è inferiore al secondo (segno ignorato) e 0 se entrambi sono uguali (segno ignorato).
# 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 ))Produzione:
The result of comparison using compare() is : 1
The result of comparison using compare_total_mag() is : -1Python
9. copia_abs() :- Questa funzione stampa il assoluto valore dell'argomento decimale.
10. copia_negato() :- Questa funzione stampa il negazione dell'argomento decimale.
11. copia_segno() :- Questa funzione stampa il primo argomento copiando il segno dal 2° argomento .
# 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 ))Produzione:
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.5299999999999993605115378159098327159881591796875Python
12. massimo() :- Questa funzione calcola il massimo di due numeri decimali.
13. min() :- Questa funzione calcola il minimo di due numeri decimali.
# 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 ))Produzione:
The minimum of two numbers is : 7.429999999999999715782905696
The maximum of two numbers is : 9.529999999999999360511537816