C++ is giving different wrong results for same arithmetic expression

  addition, c++, integer, integer-overflow

I have a very simple calculation where I calculate the distance between each individual cell to center in a 2D space. I know that the O(n) solution is redundant and I derived the formula for O(1) solution. But what I am trying to understand is: Why do these two trivial calculations give me two different results?

Here is the expected (correct) result in Python (Both versions give the same result):

result = 0;
n = 499993;
center = (n+1)//2;
for ii in range(1,center):
        result += (ii*ii*8);

print(result);

which outputs:

41664916690999888

And here are two versions in C++ with two completely different wrong results:

1)

#include <iostream>
using namespace std;
int main()
{
    unsigned long long result = 0;
    int n = 499993;
    int center = (n+1)/2;
    for(int ii = 1; ii < center; ++ii)
    {
        result += (ii*ii);
    }
    cout << result*8 << endl;
}

Output:

154435732281936
#include <iostream>
using namespace std;
int main()
{
    unsigned long long result = 0;
    int n = 499993;
    int center = (n+1)/2;
    for(int ii = 1; ii < center; ++ii)
    {
        result += (ii*ii*8);
    }
    cout << result << endl;
}

Output:

6229295798864 

What is the reason of this behavior?

For compiler I am using GCC with only -g flag

Online compiler for CPP that produces the same result: https://www.onlinegdb.com/online_c++_compiler

Thanks in advance!

Source: Windows Questions C++

LEAVE A COMMENT