C++ long overflowing prematurely - c++

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.

Related

int always short when defined?

Just a little question :
Today I got back to the value the variable types can hold and I wondered if an int defined without short or long is always short or long, like signed or unsigned!
int i ; //short or long ?
The answer is "neither". int without a modifier is int, short int is no larger than int and may be the same size. long int is no smaller than int, but may be the same size. This is all according to the C++ standard, which also says that short must be at least 16 bits, and long should be at least 32 bits. But it's entirely possible to have a machine where all are 32 or 64 bits.
neither. it's an integer type.
No. int is a distinct type from short and long. short, int, and long can have 3 different sizes. (Also, note that usually, instead of modifying int as short int or long int, it's generally preferred to just write short or long.)
It's neither. On a lot of systems, including the usual Linuxes, int will be 32 bits, short will be 16, and long 64. This isn't guaranteed by the C language however--the types need to be ordered by size but they don't have to be those specific sizes (e.g. int could be 64 bits on some systems, or 16).
Unless specified, int is always signed.
Just to know,
short is 16 bits, long is 32 bits, and int is either 16 or 32 bits depending on compiler.
Unless specified, all these are signed by default.

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 is the difference between long long and long int

I know the difference between long and int
But What is the difference between "long long" and "long int"
There are several shorthands for built-in types.
short is (signed) short int
long is (signed) long int
long long is (signed) long long int.
On many systems, short is 16-bit, long is 32-bit and long long is 64-bit. However, keep in mind that the standard only requires
sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
And a consequence of this is that on an exotic system, sizeof(long long) == 1 is possible.
According to the C standard the integral types are defined to provide at least the following ranges:
int -32767 to +32767 representable in 16 bits
long -2147483647 to +2147483647 representable in 32 bits
long long -9223372036854775807 to +9223372036854775807 representable in 64 bits
Each can be represented as to support a wider range. On common 32 bit systems int and long have the same 32 bit representation.
Note that negative bounds are symmetric to their positive counterparts to allow for sign and magnitude representations: the C language standard does not impose two's complement.
long long may be a bigger type than long int. For example on x86 32 bit long long would be a 64-bit type rather than 32 bit for long int.
On 64 bit systems it doesn't make any difference in their sizes. On 32 bit systems long long is guaranteed store values of 64 bit range.
Just to avoid all these confusions, it is always better to use the standard integral types: (u)int16_t, (u)int32_t and (u)int64_t available via stdint.h which provides transparency.
An int on 16 bit systems was 16 bits. A "long" was introduced as a 32 bit integer, but on 32 bit systems long and int mean the same thing (both are 32 bit.) So on 32 and 64 bit systems, long long and long int are both 64 bit. The exception is 64 bit UNIX where long is 64 bits.
See the integer Wikipedia article for a more detailed table.
long int is a synonym for long. long long int is a synonym for long long.
The only guarantee you have in standard C++ is that long long is at least as large as long but can be longer. This is specified in ยง3.9.1.2 in the most recent publicly available draft of the standard n3242.
The C standard doesn't make any specific width requirements for integral types other than minimal ranges of values that the type needs to be able to represent, and that the widths are non-decreasing: short <= int <= long int <= long long int (similarly for the unsigned types). long long only became part of the standard in C99 and C++0x, by the way. The minimum required ranges can be found in this Wikipedia article.
I think:
"long" doubles the number of bits allocated to the data type.
So long (32 bits?) becomes 64 bits.
Int (16 bits?) becomes 32 bits.

What's the difference between long long and long

What's the difference between long long and long? And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
#include <iostream>
using namespace std;
int main(){
long long a = 600851475143;
}
Going by the standard, all that's guaranteed is:
int must be at least 16 bits
long must be at least 32 bits
long long must be at least 64 bits
On major 32-bit platforms:
int is 32 bits
long is 32 bits as well
long long is 64 bits
On major 64-bit platforms:
int is 32 bits
long is either 32 or 64 bits
long long is 64 bits as well
If you need a specific integer size for a particular application, rather than trusting the compiler to pick the size you want, #include <stdint.h> (or <cstdint>) so you can use these types:
int8_t and uint8_t
int16_t and uint16_t
int32_t and uint32_t
int64_t and uint64_t
You may also be interested in #include <stddef.h> (or <cstddef>):
size_t
ptrdiff_t
long long does not exist in C++98/C++03, but does exist in C99 and c++0x.
long is guaranteed at least 32 bits.
long long is guaranteed at least 64 bits.
To elaborate on #ildjarn's comment:
And they both don't work with 12 digit numbers (600851475143), am I forgetting something?
The compiler looks at the literal value 600851475143 without considering the variable that you're assigning it to/initializing it with. You've written it as an int typed literal, and it won't fit in an int.
Use 600851475143LL to get a long long typed literal.
Your C++ compiler supports long long, that is guaranteed to be at least 64-bits in the C99 standard (that's a C standard, not a C++ standard). See Visual C++ header file to get the ranges on your system.
Recommendation
For new programs, it is recommended that one use only bool, char, int, and double, until circumstance arises that one of the other types is needed.
http://www.somacon.com/p111.php
Depends on your compiler.long long is 64 bits and should handle 12 digits.Looks like in your case it is just considering it long and hence not handling 12 digits.

Why do I get a "constant too large" error?

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++?