How can I write a C++ function returning true if a real number is exactly representable with a double?
bool isRepresentable( const char* realNumber ) { bool answer = false; // what goes here? return answer; }
Simple tests:
assert( true==isRepresentable( '0.5' ) ); assert( false==isRepresentable( '0.1' ) );
Parse the number into the form a + N / (10^k), where a and N are integers, and k is the number of decimal places you have.
Example: 12.0345 -> 12 + 345 / 10^4, a = 12, N = 345, k = 4
Now, 10^k = (2 * 5) ^ k = 2^k * 5^k
You can represent your number as exact binary fraction if and only if you get rid of the 5^k term in the denominator.
The result would check (N mod 5^k) == 0