-8

如何将大于最大限制的 2 个数字相乘,即1.89731e+4932使用long doubleC++/C,例如。2.79654E+256783.89574e+35890 ...

4

1 回答 1

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();

PS 通常在乘法的情况下,使用对数和加法更方便:

A * B -> Log(A) + Log(B)
于 2013-08-03T09:52:47.883 回答