lord.didger
lord.didger

Reputation: 1407

miscalculation with long long integers

In last iteration of the loop result is wrong. I know that before subtraction numbers can be bigger than long. That is why I set power to long long. Result in last iteration should be 17888888888888888889. Why it is not?

  const int NR_LEVELS = 18;
  unsigned long levels[NR_LEVELS];
  unsigned long long power = 10;
  for(unsigned int i = 0; i < NR_LEVELS; i++) {
    levels[i] = ((i+1)*10*power-(i+2)*power+1)/9;
    cout << levels[i] << endl;
    power *= 10;
  }
  levels[17] = 17888888888888888889lu;
  for(unsigned int i = 0; i < NR_LEVELS; i++) {
    cout << levels[i] << endl;
  }

Upvotes: 0

Views: 65

Answers (1)

nhahtdh
nhahtdh

Reputation: 56809

The intermediate value (before dividing by 9) overflows 64-bit integer. That is the reason why you don't get the expected result.

To be more precise, the maximum value of 64-bit integer is:

18446744073709551615

Compared to the (smallest) intermediate value before division:

161000000000000000001

This answer is assuming that long type in your code translate to a 64-bit integral type (the standard mandates that long type is at least 32-bit, so you might also get 32-bit integral type depending on the environment). Depending on the OS, computer architecture and the compiler, the upper limit of long type may vary.

Upvotes: 3

Related Questions