-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
应该尝试的任务...他们被给出了一个原因。 –