Detect precision loss

Hello.
Is there any way to detect if there would be precission loss when converting a double to float without manually process the variable bits?

Somethink like:
double a = 123.456;
double b = 12345.6789;
std::cout <<precisionLoss(a) <<"\n";
std::cout <<precisionLoss(b) <<"\n";

should print:
0
1

Thanks in advance.
Nevermind, I've investigated a little and found that:
1.2 != (double)((float)1.2)
and I think that means that the precision loss is very probable even for numbers with few digits.
Precision loss is probable for numbers with only 1 digit. Even without converting between types. 1.0 does not necessarily equal 1.0.

Read this: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16
Last edited on
Topic archived. No new replies allowed.