在 C++ 中对于非常大的数字使用哪种数据类型?
我必须将号码 600851475143
存储在我的程序中。我尝试将其存储在 long long int
变量和 long double
中,但在编译时显示错误,
integer constant is too large for "long" type.
我也尝试过 unsigned long long int
> 也是。我正在使用 MinGW 5.1.6 在 Windows 上运行 g++。
我应该使用什么数据类型来存储数字?
I have to store the number 600851475143
in my program. I tried to store it in long long int
variable and long double
as well but on compiling it shows the error
integer constant is too large for "long" type.
I have also tried unsigned long long int
too. I am using MinGW 5.1.6 for running g++ on windows.
What datatype should I use to store the number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
long long
可以,但是您必须在文字上使用后缀。如果您将
ll
保留在文字末尾,则编译器会假定您希望它是int
,在大多数情况下是 32 位有符号数。 32 位不足以存储这么大的值,因此出现警告。通过添加ll
,您向编译器表明该文字应被解释为long long
,它足够大以存储该值。后缀对于指定为函数调用哪个重载也很有用。例如:
long long
is fine, but you have to use a suffix on the literal.If you leave the
ll
off the end of the literal, then the compiler assumes that you want it to be anint
, which in most cases is a 32-bit signed number. 32-bits aren't enough to store that large value, hence the warning. By adding thell
, you signify to the compiler that the literal should be interpreted as along long
, which is big enough to store the value.The suffix is also useful for specifying which overload to call for a function. For example:
您对
long long int
(或unsigned long long int
)的想法是正确的,但为了防止出现警告,您需要告诉编译器该常量是一个long long int
:这些“L”可以是小写,但我建议不要这样做 - 根据字体,小写“L”通常看起来很像一位数字( “1”)代替。
You had the right idea with
long long int
(orunsigned long long int
), but to prevent the warning, you need to tell the compiler that the constant is along long int
:Those "L"s can be lower-case, but I'd advise against it -- depending on the font, a lower-case "L" often looks a lot like a one digit ("1") instead.
查看 GNU MP Bignum 库 http://gmplib.org/
Have a look at the GNU MP Bignum library http://gmplib.org/