Here is my code in C++. I'm using g++-11 with gcc version 11.2.0.
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
int main()
{
string t="90071992547409929007199254740993";
//separateNumbers(t);
unsigned long long stringNum=0;
for (int j=16;j<32;j++)
{
stringNum += (t[j]-'0')*pow(10,32-j-1);
}
cout << stringNum;
return 0;
}
This simple code, which I expected to get the last 16 digits as a number, gives 9929007199254740992, not 9929007199254740993.
However, the change of code
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
int main()
{
string t="90071992547409929007199254740993";
//separateNumbers(t);
unsigned long long stringNum=0;
for (int j=16;j<32;j++)
{
unsigned long long temp = (t[j]-'0')*pow(10,32-j-1);
stringNum += temp;
}
cout << stringNum;
return 0;
}
gives the desired result, 9007199254740993. How can we explain such a difference between two codes?