-8
如何乘以大於最大限制的2個數字,即1.89731e+4932
的long double
使用C++/C例如。 2.79654E+25678
和3.89574e+35890
...乘以2大於長雙倍的最大限制的數字
如何乘以大於最大限制的2個數字,即1.89731e+4932
的long double
使用C++/C例如。 2.79654E+25678
和3.89574e+35890
...乘以2大於長雙倍的最大限制的數字
這裏有兩種可能性(C#示例):
您可以使用BigInteger的(似乎在非常情況下,效率不高,但方便,精度高的數字)
BigInteger a = 279654 * BigInteger.Pow(10, 25678 - 5); // <- 2.79654E25678 = 279654E25678 * 1E-5
BigInteger b = 389574 * BigInteger.Pow(10, 35890 - 5); // <- 3.89574E35890 = 389574E35890 * 1E-5
BigInteger result = a * b;
您可以分別操作mantissas和exponentas:
Double mantissaA = 2.79654;
int exponentA = 25678;
Double mantissaB = 3.89574;
int exponentB = 35890;
Double mantissaResult = mantissaA * mantissaB;
int exponentResult = exponentA + exponentB;
// Let's adjust mantissaResult, it should be in [1..10) (10 is not included) range
if ((mantissaResult >= 10) || (mantissaResult <= -10)) {
mantissaResult /= 10.0
exponentResult += 1;
}
else if (((mantissaResult < 1) && (mantissaResult > 0)) || ((mantissaResult > -1) && (mantissaResult < 0))) {
mantissaResult *= 10.0
exponentResult -= 1;
}
// Let's output the result
String result = mantissaResult.ToString() + "E+" + exponentResult.ToString();
PS通常在乘法的情況下,它更方便使用對數和補充:
A * B -> Log(A) + Log(B)
@harold:謝謝你的改進;我編輯了代碼以添加尾數調整 –
哪裏是你的企圖? –
我找不出一種方法來做到這一點..這是一個任務的問題,並嚴重地解決了它的一個解決方案 – rajat
應該嘗試的任務...他們被給出了一個原因。 –