why this two call to toBinary function compute the same output (at least under VS2010) ?
#include <iostream>
#include <bitset>
#include <limits>
using namespace std;
template<class T> bitset<sizeof(T)*CHAR_BIT> toBinary(const T num)
{
bitset<sizeof(T)*CHAR_BIT> mybits;
const char * const p = reinterpret_cast<const char*>(&num);
for (int i = sizeof(T)*CHAR_BIT-1 ; i >= 0 ; --i)
mybits.set(i, (*(p)&(1<<i) ));
return mybits;
}
int main()
{
cout << toBinary(8.9).to_string() << "\n";
cout << toBinary( 8.9 + std::numeric_limits<double>::epsilon() ).to_string() << "\n";
cin.get();
}
That epsilon is relative to 1; here, instead, you are summing it to 8.9, which is more than 8 (2^3) times bigger than 1. This means that that epsilon would change a binary digit that is three digits to the right of the rightest digit stored in that double.
If you want to notice something change, you have to add at about 8.9*epsilon.