tags:

views:

150

answers:

5

I'm new to Windows development and I'm pretty confused.

When I compile this code with Visual C++ 2010, I get an error "constant too large." Why do I get this error, and how do I fix it?

Thanks!

int _tmain(int argc, _TCHAR* argv[])
{
    unsigned long long foo = 142385141589604466688ULL;
    return 0;
}
+9  A: 

The digit sequence you're expressing would take about 67 bits -- maybe your "unsigned long long" type takes only (!) 64 bits, your digit sequence won't fit in its, etc, etc.

If you regularly need to deal with integers that won't fit in 64 bits you might want to look at languages that smoothly support them, such as Python (maybe with gmpy;-). Or, give up on language support and go for suitable libraries, such as GMP and MPIR!-)

Alex Martelli
+4  A: 

A long long is 64 bits and thus holds a maximum value of 2^64, which is 9223372036854775807 as a signed value and 18446744073709551615 as an unsigned value. Your value is bigger, hence it's a constant value that's too large.

Pick a different data type to hold your value.

Andrew
Be careful how you word that. A long long appears to be 64 bits on the OP hardware. C++ does not define a max size limit for long long.
Martin York
Quite true. In my defence: my understanding is that Visual C++ defines a long long as 64 bits, and the OP specifically tagged the question as Visual C++.
Andrew
+2  A: 

You get the error because your constant is too large.

From Wikipedia: An unsigned long long's max value is at least 18,446,744,073,709,551,615

Here is the max value and your value:

 18,446,744,073,709,551,615  // Max value
142,385,141,589,604,466,688  // Your value

See why your value is too long?

abelenky
+1 Please change "...is 18,..." to "...is at least 18,...". long long has *at least* 64 bits and in the case of the OP probably exactly 64 bits.
sellibitze
long long is not limited to 64 by the language. I use several systems where it is 128 bits. It is a limit of the hardware used by the OP.
Martin York
A: 

According to http://msdn.microsoft.com/en-us/library/s3f49ktz%28VS.100%29.aspx, the range of unsigned long long is 0 to 18,446,744,073,709,551,615.

142385141589604466688 > 18446744073709551615

sdtom
A: 

You have reached the limit of your hardware to represent integers directly.

It seems that beyond 64 bits (on your hardware) requires the integer to be simulated by software constructs. There are several projects out there that help.

See BigInt
http://sourceforge.net/projects/cpp-bigint/

Note: Others have misconstrued that long long has a limit of 64 bits.
This is not accurate. The only limitation placed by the language are:
(Also Note: Currently C++ does not support long long (But C does) It is an extension by your compiler (coming in the next version of the standard))

sizeof(long) <= sizeof(long long)
sizeof(long long) * CHAR_BITS >= 64   // Not defined explicitly but deducible from
                                      // The values defined in limits.h

For more details See:
http://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c

Martin York