Why do I get a "constant too large" error? - c++

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

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!-)

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.

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?

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

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:
What is the difference between an int and a long in C++?

Related

What's the exact maximum value of `long long int` in C++?

I've been using c++ for over 3 months and I can't figure out the exact maximum value of numbers in long long int.
Does anyone know the value?
It depends on the system. The C++ standard only guarantees that the minimum size for long long int will be 64-bits. This is also by far the most common size.
With a 64-bit size, the maximum number that can be represented will be 2^63 - 1, which equals 9223372036854775807. The reason for this exact size, is that we need half of the bit combinations for the negative numbers, then one for 0 and the rest for the positive numbers.
The max value on a specific system can also be checked programmatically with:
#include <iostream>
#include <limits>
int main() {
std::cout << std::numeric_limits<long long int>::max();
}
Output:
9223372036854775807
long long int doesn't have a fixed maximum value specified by C++ language - it depends on the platform.
Use std::numeric_limits<long long>::max() (from <limits> header) to get its maximum value.
The guarantee by the C++ Standard for the long long modifier is that it will have a width of at least 64 bits. (see here).
The exact width of the type is, however, dependant on the particular platform, so you might get more thatn 64 bits for your types.
To check the maximum and minimum number a long long inttype can hold on your machine and implementation, use the <limits> library with std::numeric_limits<long long>. You can read more about that here.

C++ integer limits

So I think I'm a bit confused. I'm searching information about the limits of the differents types of integers. I've seen that the limit for unsigned long int is 4294967295 but when I do:
cout << numeric_limits<unsigned long int>::max() << endl;
I'm getting:
18446744073709551615
And if I'm not wrong this number is the limit of unsigned long long, isn'it? So what is happening?
Thank you
I've seen that the limit for unsigned long int is 4294967295
Whoever told you that was wrong.
The limit for unsigned long int will usually be that on systems for which the type is 32-bit.
But yours is evidently 64-bit, so you have a different limit.
this number is the limit of unsigned long long, isn'it?
Again, you're making assumptions about type width.
The width of types varies across compilers/platforms.
If you want to use types with a fixed size, then those do exist.
The standard only defines lower bounds for the limits of integers. For example, the lower bound for the maximum that an unsigned long can represent is 4294967295.
std::numeric_limits<unsigned long>::max() gives the implementation-defined maximum value an unsigned long can represent (i.e. what the current implementation aka compiler/linker/etc actually supports).
This means it is required that
std::numeric_limits<unsigned long>::max() gives a value that is 4294967295 or more. There is nothing preventing it giving a larger result. However, an implementation that gives a smaller result is non-compliant with the standard.
Note that, when moving between compilers, the only guarantee is "4294967295 or more". If one implementation gives a larger value, there is no guarantee that another implementation will.
For the most part, the standard actually says nothing whatsoever about the number of actual bits used to represent the basic integral types, like unsigned long.
The value 18446744073709551615 is consistent with a 64-bit unsigned long, in practice.
Similar stories, albeit with different values, for other integral types (int, char, short, long, etc).

Int types in C/C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are C++ int and long types both 4 bytes?
In C/C++, what is the difference between:
u_int64 myNum;
and:
unsigned long myNum;
As far as I can tell, both ate just unsigned integers, with 64bits of memory.
unsigned long does not have to be 64 bits, whereas uint64_t does. There is a kind of hierarchy of integer types where each type has to be at least large as the preceding type: signed char, short, int, long, long long, and similarly for their unsigned counterparts. There are some anchor points, stating that char is one byte (a byte does not have to be 8 bits, as far as I can recall short is at least 2 bytes 16 bits. In C++11, long long is at least 64 bits. But none of these types is exactly a given number of bits.
See fixed width integer types for more information (thanks to #chris for the link).
unsigned long is dependent on the machine like every int in C/C++. Many library especially libraries that allow for two machines to interact will type def most int like numbers to make sure both are the same size. u_int64 is basically type def to an unsigned integer of 64 bits, to allow for use on any machine. In theory unsigned long could be 128, 64, 256 or pretty much any size.
The C language itself doesn't specify how big an int is
long only has to be at least as long as int
long long only has to be at least as long as long
also u_int64_t is a c99 type, and wouldn't be available in ANSI c89.
also among 64 bit architectures that are differences LP64 indicates long's and pointers are 64 bit, LLP64 means that long long's and pointers are 64 bit.

What's the 'long' data type used for?

I've been programming in C++ for quite a while now and I am pretty familiar with most of the stuff. One thing that I've never understood though is the 'long' data type.
I googled it but I still don't know what it is for. I've found pages that say it is the same size and has the same range as an int. So what would be the point in using it?
I found another stack overflow question regarding this here:
Difference between long and int data types
And it seems that the only difference between the two is that sometimes the size is different on different systems. Does that mean that an application that uses long on a 64bit machine won't work on a 32bit machine? If so then wouldn't it be better to not use them at all?
Also I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
It is compiler dependent. My standards-fu is a bit rusty but I believe it is defined as follows:
char <= short <= int <= long <= long long
where:
char >= 8 bits
short >= 16 bits
int >= 16 bits
long >= 32 bits
long long >= 64 bits
Which means that it is perfectly valid to have char = short = int = long = long long = 64bits and in fact compilers of some DSPs are designed that way.
This underscores the importance of actually reading your compiler documentation.
I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
long int is the same as long (just as short int is the same as short).
long long is a distinct data type introduced by several compilers and adopted by C++0x.
Note that there is no such thing as long long long:
error: 'long long long' is too long for GCC
From one of the answers in the question you linked:
The long must be at least the same size as an int, and possibly, but not necessarily, longer.
I can't think of a better way to explain it.
long is guaranteed (at least) 32 bits. int is only guaranteed (at least) 16 bits. on 16- and 8-bit systems long provided range at a cost of efficiency.
cheers & hth.,
This is what the C++03 standard says (3.9.1/2) :
There are four signed integer types:
“signed char”, “short int”, “int”, and
“long int.” In this list, each type
provides at least as much storage as
those preceding it in the list.
So : sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
This is what the C++0x (3.9.1/2) and C99 (6.2.5/4) standards say :
There are five standard signed integer
types, designated as signed char,
short int, int, long int, and long
long int.
long is synonym of long int
long long doesn't exist in C++03, but will in C++0x.
I googled it but I still don't know
what its for. I've found pages that
say its the same size and has the same
range as an int. So what would be the
point in using it?
I've wondered the same thing. And concluded that long is now useless.
Prior to the rise of 64-bit systems, the de facto standard for C integer types was:
char = (u)int8_t (Note that C predates Unicode.)
short = int16_t
int = intptr_t [until 64-bit], int_fast16_t
long = int32_t [until 64-bit], intmax_t [until 1999]
long long = int64_t or intmax_t
Today, however, long has no consistent semantics.
If you want an integer that is guaranteed to be 32 bit or 64 bit, there are such types, e.g. int64_t. If you really want to ensure your int are of such a size, use these types instead of long, long long, etc. You'll need to include cstdint for these (stdint.h in C).

C++ long overflowing prematurely

I'm having a bizarre problem with C++ where the long data type is overflowing long before it should. What I'm doing (with success so far) is to have integers behave like floats, so that the range [-32767,32767] is mapped to [-1.0,1.0]. Where it stumbles is with larger arguments representing floats greater than 1.0:
inline long times(long a, long b) {
printf("a=%ld b=%ld ",a,b);
a *= b;
printf("a*b=%ld ",a);
a /= 32767l;
printf("a*b/32767=%ld\n",a);
return a;
}
int main(void) {
printf("%ld\n",times(98301l,32767l));
}
What I get as output is:
a=98301 b=32767 a*b=-1073938429 a*b/32767=-32775
-32775
So times(98301,32767) is analogous to 3.0*1.0. This code works perfectly when the arguments to times are less than 32767 (1.0), but none of the intermediate steps with the arguments above should overflow the 64 bits of long.
Any ideas?
long is not necessarily 64 bits. try 'long long' instead.
The type long is not necessarily 64 bits. If you are on the 32 bit architecture (at least on MS Visual c++), the long type is 32 bits. Check it out with sizeof (long). There is also the long long data type that may help.
You probably have 32-bit longs. Try using long long instead.
98301 * 32767 = 3221028867, while a 32-bit long overflows at 2147483648
The C standard only guarantees that long will have at least 32 bit (which is actually the case on most 32bit platforms).
If you need 64 bit, use long long. It's guaranteed to hold at least 64 bit.