C++ : integer constant is too large for its type

后端 未结 2 1032
误落风尘
误落风尘 2021-01-16 09:48

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

bruteforceJS12.cpp:8:28: warning: integer constant is too large fo

相关标签:
2条回答
  • 2021-01-16 10:03

    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 ;
    }
    

    is:

    ull 0   18446744073709551615
    

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

     18446744073709551615
     318338237039211050000
    

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

    0 讨论(0)
  • 2021-01-16 10:04

    Your problem is simply that 318338237039211050000ULL is out of range.

    Assuming a 64 bit type, a 64 bit value is good for log10( 264-1) decimal digits (i.e. all 19 digit values and some lower 20 digit values), 318338237039211050000ull has 21 digits. 264-1 (18446744073709551615ull) is the maximum.

    The value 318338237039211050000 requires at least log10(318338237039211050000)/log10(2) bits. That's 69 bits.

    0 讨论(0)
提交回复
热议问题