C++ 如何避免大数的科学记数法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/265258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:08:56  来源:igfitidea点击:

How do I avoid scientific notation for large numbers?

c++math

提问by AntonioCS

I am doing 2^1000 and I am getting this:

我正在做 2^1000,我得到了这个:

1.07151e+301

1.07151e+301

Is there any way to actually turn this into a proper number without the e+301, or at least can anyone show me where I can see how to turn this in to a real number, by some way working with the e+301 part?

有没有办法在没有 e+301 的情况下将其实际转换为正确的数字,或者至少有人可以告诉我在哪里可以看到如何通过使用 e+301 部分的方式将其转换为实数?

采纳答案by tvanfosson

So, I'm thinking that what you really want is just the ability to print it without scientific notation. If you're using printf, what you want is:

所以,我认为您真正想要的只是能够在没有科学记数法的情况下打印它。如果您正在使用printf,您想要的是:

printf( "%f1000.0", value );
// note that 1000 is way larger than need be,
// I'm just too lazy to count the digits

With cout, try something like:

使用cout,尝试以下操作:

cout.setf(ios::fixed);
cout << setprecision(0) << value;

If you want to print it as a power of two (2^1000 vs 10715...), you're on your own.

如果您想将其打印为 2 的幂(2 ^ 1000 对 10715 ...),您需要自己解决。

回答by Louis Gerbarg

There is a practical limit to how large a number that can be directly manipulated in machine registers can be. if you are using double precision floats there are a total of 64 bits, some of which are devoted to the mantissa, some to the exponent, and 1 to the sign bit.

可以在机器寄存器中直接操作的数字有多大的实际限制。如果您使用双精度浮点数,则总共有 64 位,其中一些专用于尾数,一些专用于指数,1 专用于符号位。

2^1000 needs a 1001 bit integer to be represented without losing precision. In order to work with numbers like that you will need to use a library that has big number support, such as GNU MP.

2^1000 需要一个 1001 位的整数来表示而不损失精度。为了处理这样的数字,您需要使用一个支持大量数字的库,例如GNU MP

回答by Alnitak

You need to use a number class specifically designed for long numbers.

您需要使用专为长数字设计的数字类。

To represent 2^1000 as an exact number then by definition you need a number format that actually holds 1001 binary bits. The longest normal primitive integer format is usually only 64 bits.

要将 2^1000 表示为一个精确的数字,那么根据定义,您需要一个实际包含 1001 个二进制位的数字格式。最长的正常原始整数格式通常只有 64 位。

BTW, the answer is:

顺便说一句,答案是:

% perl -Mbigint -e 'print 2**1000'
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

回答by schnaader

If you want to do it yourself in C++, you can for example create an digit array and do the calculation yourself. Tested and verified example:

如果你想用 C++ 自己做,你可以例如创建一个数字数组并自己进行计算。经过测试和验证的示例:

unsigned int result[400]; // result digits
unsigned int i, j, carry;

// Initialize result digits
for (i = 0; i < 399; i++) {
  result[i] = 0;
}
result[399] = 2;

for (i = 2; i <= 1000; i++) { // Calculate 2^i
  carry = 0;
  for (j = 399; j > 0; j--) {
    result[j] <<= 1;    // multiply with 2
    result[j] += carry; // add carry
    carry = result[j] / 10;
    result[j] %= 10;    // we want one digit (0-9) only
  }
}

printf("2 ^ 1000 = ");
// print result digits
for (i = 0; i < 400; i++) {
  if (result[i] != 0) { // no leading zeros, please
    for (j = i; j < 400; j++) {
      printf("%d", result[j]);
    }
    break;
  }
}
printf("\n");

回答by Charles Bretana

One option, if your application logic will allow it is to change the units you are manipulating....

一种选择,如果您的应用程序逻辑允许它是更改您正在操作的单位....

If you are measuring the distance from New York to Paris in Angstroms, choose Miles or Kilometers instead.... Except for pure mathematical requirements, (like say factoring prime numbers for cryptology or, ... research into the Reimann Hypothesis), there is seldom any need to retain that many digits of accuracy.

如果你用埃来测量从纽约到巴黎的距离,请选择英里或公里......除了纯粹的数学要求,(比如为密码学分解素数或......研究 Reimann 假设),还有很少需要保留这么多位数的准确性。

On the other hand, if you are doing something that requires perfectly accurate integer values with that many digits, then you should probably get specialized software designed to handle large numbers... Such software is definitely available, although I'm not familiar with that area. (costs, vendors, capabilities etc.) If cost is an issue, and you're thinking of writing your own, I don't know enough about what's involved in to know if that approach is worth the effort...

另一方面,如果您正在做一些需要非常精确的整数值的事情,那么您可能应该使用专门设计来处理大数字的软件......这样的软件绝对可用,尽管我不熟悉区域。(成本、供应商、能力等)如果成本是一个问题,而您正在考虑自己编写,那么我对所涉及的内容知之甚少,无法知道这种方法是否值得付出努力……

回答by Charles Bretana

cout << fixed << your_number;

cout << 固定 << your_number;

But it won't probably show the whole number. As someone said before, you need to write a class.

但它可能不会显示整数。之前有人说过,你需要写一个类。

回答by tloach

You are getting as precise a number as the variable type can support. That number is on the order of 1 followed by 301 zeroes. To get a precise number you'll have to work with a library that supports large numbers, or work with a language that is made for that kind of math (maple, matlab, etc)

您获得的数字与变量类型所能支持的一样精确。该数字的数量级为 1 后跟 301 个零。要获得精确的数字,您必须使用支持大数字的库,或者使用专为此类数学设计的语言(maple、matlab 等)

回答by C AJAY KARTHIKEYAN

Include the header limits.hand cmath.h

包括标题limits.hcmath.h

cout.precision(0);
cout<< fixed<< pow(2,31);               //OR ANY NUMBER HERE

Use cout.precision to set the precision.

使用 cout.precision 设置精度。