C ++:整数常量对其类型而言太大 [英] C++ : integer constant is too large for its type

查看:266
本文介绍了C ++:整数常量对其类型而言太大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要粗暴训练一年.编译器不断抛出此错误:

I need to bruteforce a year for an exercise. The compiler keep throwing this error:

bruteforceJS12.cpp:8:28:警告:整数常量对于其类型而言太大(默认情况下启用)

bruteforceJS12.cpp:8:28: warning: integer constant is too large for its type [enabled by default]

我的代码是:

#include <iostream>

using namespace std;

int main(){

    unsigned long long year(0);
    unsigned long long result(318338237039211050000);
    unsigned long long pass(1337);

    while (pass != result)
    {
    for (unsigned long long i = 1; i<= year; i++)
        {

        pass += year * i * year;

        }

        cout << "pass not cracked with year = " << year << endl;
        ++year;

    }

        cout << "pass cracked with year = " << year << endl;
}

请注意,我已经尝试使用unsigned long long result(318338237039211050000ULL);

Note that I already tried with unsigned long long result(318338237039211050000ULL);

我正在使用gcc版本4.8.1

I'm using gcc version 4.8.1

以下是使用InfInt库的更正版本 http://code.google.com/p/infint /

Here is the corrected version using InfInt library http://code.google.com/p/infint/

#include <iostream>
#include "InfInt.h"

using namespace std;

int main(){

    InfInt year = "113";
    InfInt result = "318338237039211050000";
    InfInt pass= "1337";

    while (pass != result)
    {
    for (InfInt i = 1; i<= year; i++)
        {

        pass += year * i * year;

        }

        cout << "year = " << year << "  pass = " << pass <<  endl;
        ++year;

    }

        cout << "pass cracked with year = " << year << endl;
}

推荐答案

找出系统上数字限制的快速方法是使用

A quick way to figure out the numeric limits on your system would be to use std::numeric_limits. The output on my system when I run the following code:

#include <iostream>
#include <limits>

int main()
{   
     std::cout << "ull\t"
              << std::numeric_limits<unsigned long long>::lowest() << '\t'
              << std::numeric_limits<unsigned long long>::max() << std::endl ;
}

是:

ull 0   18446744073709551615

我们可以看到最大值肯定小于您的字面值:

we can see the max value is definitely smaller than your literal value:

 18446744073709551615
 318338237039211050000

因此,对于 unsigned long long 类型,您的整数文字太大.

So your integer literal is just too large for unsigned long long type.

这篇关于C ++:整数常量对其类型而言太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆