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 analogous 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!
ii*iiwill be too large for a 32 bit int. Remember since its int * int the calculation is done as an int notunsigned long long int.result += (8LL*ii*ii);should fix things (forcing the calculation to be performed as long long). In your first case, you would need to declareiias a long long, or cast one of the operands of the*.