如何使用C ++ / C乘以大于最大限制的2个数,即1.89731e+4932
long double
,例如。 2.79654E+25678
和3.89574e+35890
...
答案 0 :(得分: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;
你可以分开操作尾数和指数:
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();
P.S。通常在乘法的情况下,使用对数和添加更方便:
A * B -> Log(A) + Log(B)