Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Long Long INT - maxes out at 999,999,999 ?  (Read 4655 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Long Long INT - maxes out at 999,999,999 ?
« on: January 25, 2016, 08:34:00 AM »
Found an interesting problem with a program I wrote. If I use a value greater than 999,999,999 for a long long int it acts like an overflow to the int and the program freaks out. long long int has/(should have) a range of:   –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ..... 19 digit places, whereas the program is failing as if there is an overflow with more than 9 digits.

Was curious if this is a bug with the IDE or some other issue that I am not aware of. I'm using Bloodshed Dev C++ 4.9.9.2. More info on the ranges for variables listed at the link.

https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

The program has all integers that are to interact with the long long int set to long long so that I dont overflow a standard int. So I know its not a mistake of passing a long long int value to a standard int with only a range of –2,147,483,648 to 2,147,483,647 by which even a standard int shouldn't be exceeded either as for I am 1,147,483,648 away from the standard signed int limit at 999,999,999

The code is in C++ and has a while loop and the value of the long long is passed to srand(seed). So maybe 999,999,999 is the limit for seeded random? 

However looking here it shows that it should go beyond the 999,999,999 limit that I am hitting. If I pass 1,000,000,000 to it the program malfunctions as if its an overflow. http://en.cppreference.com/w/cpp/numeric/random/rand

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Long Long INT - maxes out at 999,999,999 ?
« Reply #1 on: January 25, 2016, 09:40:51 AM »
It is a compiler issue. The workaround is to make your own compiler.
Or do it in assembly.
Wait... did you conform to this?
https://gcc.gnu.org/onlinedocs/gcc/Long-Long.html
Quote

Next: Complex, Previous: __int128, Up: C Extensions
6.9 Double-Word Integers

ISO C99 supports data types for integers that are at least 64 bits wide, and as an extension GCC supports them in C90 mode and in C++. Simply write long long int for a signed integer, or unsigned long long int for an unsigned integer. To make an integer constant of type long long int, add the suffix ‘LL’ to the integer. To make an integer constant of type unsigned long long int, add the suffix ‘ULL’ to the integer.

You can use these types in arithmetic like any other integer types. Addition, subtraction, and bitwise boolean operations on these types are open-coded on all types of machines. Multiplication is open-coded if the machine supports a fullword-to-doubleword widening multiply instruction. Division and shifts are open-coded only on machines that provide special support. The operations that are not open-coded use special library routines that come with GCC.

There may be pitfalls when you use long long types for function arguments without function prototypes. If a function expects type int for its argument, and you pass a value of type long long int, confusion results because the caller and the subroutine disagree about the number of bytes for the argument. Likewise, if the function expects long long int and you pass int. The best way to avoid such problems is to use prototypes.


DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Long Long INT - maxes out at 999,999,999 ?
« Reply #2 on: January 25, 2016, 10:22:55 AM »
Geek thanks for sharing what you shared, it pointed me to LL suffix which helped point out the issue.

http://stackoverflow.com/questions/1458923/long-long-in-c-c

Now its going to be interesting with a counter++; as to if I need to concatenate the LL suffix before use. The counter starts at 0 and at 1,000,000,000 was when the program would fail going past the last value of 999,999,999 which was ok without the LL suffix. The while loop might be ok with it since its looking for a constant before exiting, but value passed to SRAND(seed) I wonder if I will need to concatenate and then cast it to seed to retain the LL suffix as for obviously the language itself doesnt do this automatically at 1,000,000,000, and I would need an IF statement that when the counter value reached 1,000,000,000 it is required that it add the LL suffix. WOW What a mess!  ::)

Quote
counter++;  //This works until 1,000,000,000 is reached

Might need to add code to add the LL suffix for a dynamic value...  :o

Quote
IF(counter>=1000000000LL){
// concatenate incremented value with LL suffix
}
else{
// do nothing until 1,000,000,000 is reached
}

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Long Long INT - maxes out at 999,999,999 ?
« Reply #3 on: January 25, 2016, 11:49:17 AM »
Ok found my problem.... Typo in code. You dont need to add suffix to counters dynamic value. It works beyond 999,999,999 now with the LL used for while loop constant that its looking to reach

Code: [Select]
long long int = 999999999;

    while(counter<=1000000010LL){
counter++;
cout<<"Counter = "<<counter;
}

And this warning below is seen if tying to set the while loop to 1,000,000,000 or larger without the LL suffix and compiling.

 
Quote
  13:20 C:\cpp\run1.cpp [Warning] integer constant is too large for its type

Quote
counter = 999999999
counter = 1000000000
counter = 1000000001
counter = 1000000002
counter = 1000000003
counter = 1000000004
counter = 1000000005
counter = 1000000006
counter = 1000000007
counter = 1000000008
counter = 1000000009
counter = 1000000010