在 C++ 中大于 2^32 的整数使用什么类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/934358/
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 18:02:55  来源:igfitidea点击:

What type to use for integers larger than 2^32 in C++?

c++types

提问by Igor Oks

I have an integer variable, that can get a value larger than 4294967295.

我有一个整数变量,它可以得到一个大于 4294967295 的值。

What type should I use for it (long long, or double, or something else)?

我应该使用什么类型(long long、double 或其他)?

采纳答案by user9876

There is no portable way of doing this in C++, as the language does not specify the size of integer types (except sizeof char is 1). You need to consult your compiler documentation.

在 C++ 中没有可移植的方法来做到这一点,因为该语言没有指定整数类型的大小(除了 sizeof char 是 1)。您需要查阅编译器文档。

回答by sharptooth

Use long longand if possible add a compile-time assertion that this type is wide enough (smth like sizeof( long long ) >= 8).

使用long long并在可能的情况下添加一个编译时断言,该断言该类型足够宽(类似于sizeof( long long ) >= 8)。

doubleis for floating-point, not integer.

double用于浮点数,而不是整数。

回答by Pod

Try:

尝试:

http://gmplib.org/big num.

http://gmplib.org/大号码。

http://mattmccutchen.net/bigint/big int.

http://mattmccutchen.net/bigint/大整数。

I've used neither, but I've used similiar things in Java.

我没有使用过,但我在 Java 中使用过类似的东西。

回答by user9876

I'm going to assume your numbers will fit in 64 bits. If not, then you need an arbitrary-precision arithmetic librarysuch as GMP.

我将假设您的数字适合 64 位。如果没有,那么您需要一个任意精度的算术库,例如GMP

In theory, there's no easy, portable way to do 64-bit maths in C++. In practise, most C++ compilers also support the "old fashioned" C headers, and C99 has a nice header called stdint.h.

理论上,在 C++ 中进行 64 位数学运算没有简单、可移植的方法。实际上,大多数 C++ 编译器也支持“老式”C 头文件,C99 有一个很好的头文件,称为stdint.h

So first do:

所以首先做:

#include <stdint.h>

Then use types int64_t(signed) and uint64_t(unsigned).

然后使用类型int64_t(有符号)和uint64_t(无符号)。

EDIT TO ADD:After I wrote this answer, C++11 added the <cstdint> headerwhich normally* defines std::int64_t and std::uint64_t , so if you have a modern compiler it's best to use those.

编辑添加:在我写下这个答案之后,C++11 添加了<cstdint> 标头,它通常*定义 std::int64_t 和 std::uint64_t ,所以如果你有一个现代编译器,最好使用它们。

(* In theory the system might not support a 64-bit type at all. But in practise it will, on any system you're likely to come across).

(* 理论上,系统可能根本不支持 64 位类型。但实际上,在您可能遇到的任何系统上,它会支持)。

回答by user9876

Don't use double, because:

不要使用double,因为:

cout.setf(ios::fixed);
cout << LONG_LONG_MAX << endl;
cout << double(LONG_LONG_MAX) << endl;

cout << LONG_LONG_MAX-100 << endl;
cout << double(LONG_LONG_MAX-100) << endl;

Output:

输出:

9223372036854775807
9223372036854775808.000000
9223372036854775707
9223372036854775808.000000

回答by Igor Oks

Both proposals aren't good because long long is not a standard C++ data type, and double is a floating-point.

这两个提议都不好,因为 long long 不是标准的 C++ 数据类型,而 double 是浮点数。

Since my program has to be portable, I am going to #define my own types, that suit all the compilers that I use (visual studio and gcc) :

由于我的程序必须是可移植的,我将#define 我自己的类型,这适合我使用的所有编译器(visual studio 和 gcc):

#ifdef WIN32
  #define unsigned_long_long unsigned __int64
  #define long_long __int64
#else // gcc. Might not work on other compilers!
  #define unsigned_long_long unsigned long long
  #define long_long long long
#endif

回答by Ben Reeves

I use

我用

uint64_t

But it's not standard.

但这不是标准的。

回答by PowerApp101

Try TTMath. All you need to do is include a single header and then declare a bignum type such as:

试试TTMath。您需要做的就是包含一个标头,然后声明一个 bignum 类型,例如:

typedef ttmath::UInt<100> BigInt;

which creates a type that can hold unsigned integers between 0 and 2 ^ (32*100)-1. Then just use BigIntwherever you would use int.

它创建了一种可以保存 0 到 2 ^ (32*100)-1 之间的无符号整数的类型。然后就BigInt在任何你会使用的地方使用int

Of course you can choose whatever size you like for the template parameter. 100 might be overkill ;-)

当然,您可以为模板参数选择任何您喜欢的大小。100 可能有点矫枉过正;-)

Just realised, the lib only works on x86 and x64, but is OS cross-platform on those processors.

刚刚意识到,该库仅适用于 x86 和 x64,但在这些处理器上是跨平台的。

回答by Slartibartfast

if you don't need negative numbers, unsigned long long sounds like most you can get.

如果您不需要负数,那么 unsigned long long 听起来就像您可以获得的大多数。

回答by lispmachine

A lot of current C/C++ compilers have either stdint.hor inttypes.h header.

许多当前的 C/C++ 编译器都有stdint.h或 inttypes.h 头文件。

int_fast64_tor int64_tmay be an option (IMHO the most portable).

int_fast64_t或者int64_t可能是一个选项(恕我直言最便携)。