I'm trying to calculate large integers with the long long
datatype but when it gets large enough (2^55
), the arithmetic behavior is unpredictable. I am working in Microsoft Visual Studio 2017.
In this first case, I am subtracting 2
from the long long
variable m
in the initialization. This works fine for all n
until I try 54
, then m
will simply not be subtracted by 2
.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define LL long long
int main()
{
LL n;
cin >> n;
LL m = pow(2, n + 1) - 2;
cout << m;
return 0;
}
However, using this code m
does get subtracted by 2
and is working as I would expect.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define LL long long
int main()
{
LL n;
cin >> n;
LL m = pow(2, n + 1);
m -= 2;
cout << m;
return 0;
}
I expect both codes to be equivalent, why is this not the case?