The mantissa on a double
in IEEE-754 is 53 bits (52 bits and one hidden, being very technical). That means that if the highest bits in x
are above bit 52, and some of the lower bits are non-zero, the compare will fail.
You could find this out by writing a piece of code:
unsigned long long x = 0x1;
while(x > 0)
{
x <<= 1ULL;
if (!test(x+1))
{
cout << "x=" << hex << x << endl;
break;
}
}
Edit: fixed up code a little bit after actually testing it.
It does print x=20000000000000
as predicted.
Or, if you want to use <limits>
, you can achieve the same result with:
numeric_limits<double> n;
cout << "digits=" << dec << n.digits << " -> " << hex << (1ULL << n.digits) << endl;