在 C++ 中存储和打印 10+ 位整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2231525/
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
Storing and printing 10+ digit integer in c++
提问by Shawn Mclean
I'm using cout to print digits to the console. I am also storing values of up to 13+billion as a digit and doing computations on it. What data type should I use?
我正在使用 cout 将数字打印到控制台。我还将高达 13+0 亿的值存储为一个数字并对其进行计算。我应该使用什么数据类型?
When I do the following:
当我执行以下操作时:
int a = 6800000000;
cout << a;
It prints -1789934592.
它打印 -1789934592。
thanks.
谢谢。
回答by Ignacio Vazquez-Abrams
回答by Matthew Flaschen
Use int64_tto guarantee you won't overflow. It is available from stdint.h.
使用int64_t来保证你不会溢出。它可以从stdint.h 获得。
回答by Jerry Coffin
Just a note that both int64_t
and long long
are included in C99 and in C++ 0x, but notin the current version of C++. As such, using either does put your code at some risk of being non-portable. Realistically, however, that risk is probably already pretty low -- to the point that when/if you port your code, there are likely to be much bigger problems.
请注意,int64_t
和long long
都包含在 C99 和 C++ 0x 中,但不在当前版本的 C++ 中。因此,使用任何一种都会使您的代码面临不可移植的风险。然而,实际上,这种风险可能已经很低了——以至于当/如果您移植代码时,可能会出现更大的问题。
If, however, you really want to assure against that possibility, you might consider using a double precision floating point. Contrary to popular belief, floating point types can represent integers exactly up to a certain limit -- that limit being set (in essence) by the size of the mantissa in the F.P. type. The typical implementation of a double has a 53-bit mantissa, so you can represent 53-bit integers with absolute precision. That supports numbers up to 9,007,199,254,740,992 (which is substantially more than 13 of eitherof the popular meanings of "billion").
但是,如果您真的想确保避免这种可能性,您可以考虑使用双精度浮点数。与流行的看法相反,浮点类型可以精确地表示达到某个限制的整数——该限制(本质上)由 FP 类型中尾数的大小设置。double 的典型实现具有 53 位尾数,因此您可以以绝对精度表示 53 位整数。这支持高达 9,007,199,254,740,992 的数字(大大超过“十亿”这两种流行含义中的13 个)。
回答by R Samuel Klatchko
It's a good idea to understand the range limits of different sized types.
了解不同大小类型的范围限制是个好主意。
A 32 bit type (on most 32 bit platforms, both int and long are 32 bit) have the following ranges:
32 位类型(在大多数 32 位平台上,int 和 long 都是 32 位)具有以下范围:
signed: -2,147,483,648 to 2,147,483,647
unsigned: 0 to 4,294,967,295
While 64 bit types (typically long long's are 64 bit, on most Unix 64 bit platforms a long is also 64) have the following range:
虽然 64 位类型(通常 long long 是 64 位,在大多数 Unix 64 位平台上 long 也是 64)具有以下范围:
signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned: 0 to 18,446,744,073,709,551,615
回答by Stephen Cross
Your data type (int) is too small to hold such large numbers. You should use a larger data type or one of the fixed size data types as given in the other answer (though you should really use uint64_t if you're not using negative numbers).
您的数据类型 (int) 太小,无法容纳如此大的数字。您应该使用更大的数据类型或另一个答案中给出的固定大小数据类型之一(尽管如果您不使用负数,您应该真正使用 uint64_t )。
回答by Nishadh
just use double in the declaration statement
只需在声明语句中使用 double
回答by user3320035
unsigned long long
无符号长长
can be used
可以使用
回答by akiller
You could use a long int:
你可以使用一个长整数:
long int a
Or if it's always going to be positive, an unsigned long int:
或者,如果它总是为正数,则为 unsigned long int:
unsigned long int a