Alex Rivera | Logout

Detecting precision loss when converting from double to float

Asked 2010-07-13T17:53:18.260
12

I am writing a piece of code in which i have to convert from double to float values. I am using boost::numeric_cast to do this conversion which will alert me of any overflow/underflow. However i am also interested in knowing if that conversion resulted in some precision loss or not.

For example

    double source =  1988.1012;
    float dest = numeric_cast<float>(source);

Produces dest which has value 1988.1

Is there any way available in which i can detect this kind of precision loss/rounding

Edit
Report

1 Answer

1

You're going to have a certain level of precision loss, as per Dave's answer. If, however, you want to focus on quantifying it and raising an exception when it exceeds a certain number, you will have to open up the floating point number itself and parse out the mantissa & exponent, then do some analysis to determine if you've exceeded your tolerance.

But, the good news, its usually the standard IEEE floating-point float. :-)

answered 2010-07-13T18:18:25.403

Your Answer