简体   繁体   English

在 C++ 中存储和打印 10+ 位整数

[英]Storing and printing 10+ digit integer in c++

I'm using cout to print digits to the console.我正在使用 cout 将数字打印到控制台。 I am also storing values of up to 13+billion as a digit and doing computations on it.我还将高达 13+0 亿的值存储为一个数字并对其进行计算。 What data type should I use?我应该使用什么数据类型?

When I do the following:当我执行以下操作时:

int a = 6800000000;
cout << a;

It prints -1789934592.它打印 -1789934592。

thanks.谢谢。

long long最多可以容纳 9223372036854775807。如果需要更大的,请使用gmp东西。

Use int64_t to guarantee you won't overflow.使用int64_t来保证你不会溢出。 It is available from stdint.h .它可以从stdint.h 获得

Just a note that both int64_t and long long are included in C99 and in C++ 0x, but not in the current version of C++.请注意, int64_tlong long都包含在 C99 和 C++ 0x 中,但不在当前版本的 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.然而,实际上,这种风险可能已经很低了——以至于当/如果您移植代码时,可能会出现更大的问题。

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 FP type.与流行的看法相反,浮点类型可以精确地表示达到某个限制的整数——该限制(本质上)由 FP 类型中尾数的大小设置。 The typical implementation of a double has a 53-bit mantissa, so you can represent 53-bit integers with absolute precision. double 的典型实现具有 53 位尾数,因此您可以以绝对精度表示 53 位整数。 That supports numbers up to 9,007,199,254,740,992 (which is substantially more than 13 of either of the popular meanings of "billion").支持数字高达9,007,199,254,740,992(其基本超过任一的“十亿”的流行含义13)。

Your data type (int) is too small to hold such large numbers.您的数据类型 (int) 太小,无法容纳如此大的数字。 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).您应该使用更大的数据类型或另一个答案中给出的固定大小数据类型之一(尽管如果您不使用负数,您应该真正使用 uint64_t )。

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

只需在声明语句中使用 double

unsigned long long无符号长长

can be used可以使用

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

See: http://forums.guru3d.com/showthread.php?t=131678见: http : //forums.guru3d.com/showthread.php?t=131678

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM