乘以两个大于长双的最大限制的数字
multiply 2 numbers that are bigger than maximum limit of long double
如何使用C++/C将大于最大极限的两个数字相乘,即long double
的1.89731e+4932
,例如2.79654E+25678
和3.89574e+35890
。。。
有两种可能性(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();
附言:通常在乘法的情况下,使用对数和加法更方便:
A * B -> Log(A) + Log(B)
相关文章:
- 查找最接近的大于当前数字的数字的索引
- 如何找到大于整数的最小数字
- 如何查找数字大于 5 的数字总和
- 为什么 CSpinButtonCtrl 不能正确处理大于 1000 的数字?
- 计算向量中大于数字的元素
- 如何在向量中找到大于某些数字的所有元素
- 如何在给出前两个数字的级数中找到大于 x 的第 n 个最小子数组总和?
- 显示用户输入的平均数大于 50 和所有可被 3 整除的数字的乘积
- 如何在数字大于类型 size_t 的最大数量时使用 std::bitset
- 如何将一个数字(大于8个字节)从字符阵列转换为其ASCII表示
- 如何生成数字,直到它们大于10 000
- C 用户输入4个数字,并且该程序检查它们大于少于
- 为什么此代码只能识别文本文件中的第一行?输入任何大于 1 的数字都会返回"Atomic Number Not Found"
- 给定一个数字n,打印序列中大于或等于n的第一个数字
- bignumber.h arduino如何解析大于10位数字的数字
- 使用单个堆栈的中缀到后缀不适用于大于 9 的数字
- 在字符串中的一行中搜索大于0的数字
- 从字符串转换为数字大于 std::numeric_limit<double>::d igits10 的双精度
- 在C++中打开一个数字大于9的COM端口
- 检查数字是否为完全平方数(数字大于15位)