logo

Python の 10 進関数 |セット1

Python の定義では、モジュール「10 進数」を使用して高速な 10 進浮動小数点演算を実行するための特定のメソッドが提供されています。 
小数に関する重要な操作
1.sqrt() :- この関数は、 平方根 10 進数の。
2.exp() :- この関数は、 e^x (指数) 10 進数の。
 

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.ln() :- この関数は計算に使用されます 自然対数 10 進数の。
4.log10() :- この関数は計算に使用されます 対数(基数10) 10 進数の。
 

Javaの配列リスト
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. as_tuple() :- 10 進数を次の値を含むタプルとして返します。 3 つの引数の符号 (0 の場合 + 1 - -) の桁と指数値
6. fma(ab) :- この「fma」は、 融合乗算と加算 。計算します (num*a)+b 引数の数字から。 (num*a) の四捨五入なし この関数で発生します。
例 :  
 



YouTubeをダウンロードするにはvlc
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) 

出力: 

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.比較() :- この関数は 10 進数を比較するために使用されます。 最初の 10 進数の引数が 2 番目より大きい場合は 1 を返し、最初の 10 進数の引数が 2 番目より小さい場合は -1 を返し、両方が等しい場合は 0 を返します。
8.compare_total_mag() :- 10 進数の合計の大きさを比較します。 最初の 10 進数の引数が 2 番目 (符号を無視) より大きい場合は 1 を返し、最初の 10 進数の引数が 2 番目 (符号を無視) より小さい場合は -1 を返し、両方が等しい場合 (符号を無視) は 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. copy_abs() :- この関数は、 絶対 10 進数の引数の値。
10.copy_negate() :- この関数は、 否定 10 進引数の。
11.copy_sign() :- この関数は、 第 2 引数の符号をコピーして第 1 引数を作成する
 

等価法則
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.max() :- この関数は、 最大 2 つの 10 進数。
13.分() :- この関数は、 最小 2 つの 10 進数。
 

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