Floating point issue

Is it me, or the Watch window in Visual Studio rounds the floating point values

I'm asking this, because I'm having a very strange behavior regarding floats.

I have this example. Values shown in the Watch Window while debuging:

plane.c = -1.0000000
pp->z = -8.8235264
plane.c * pp->z = 8.8235263824462891

Are the values in the watch window rounded Can I disable that Why does this happen

Thank you





Answer this question

Floating point issue

  • sroche

    You can use double precision by adding D3DCREATE_FPU_PRESERVE when creating the device or maybe make up a function to test upto a certain accuracy like:

    bool epsilonEqual(float lhs, float rhs, float Epsilon = 0.001f)
    {
    return (fabsf(lhs - rhs) < Epsilon) true : false;
    }


  • empty mind

    Torpedo wrote:
    What I think is the stranger, is that the watch window in the VS doesn't show the correct values.

    It's showing you the correct values rounded to the number of decimal digits of accuracy that 32-bit floating point values can represent. No other 32-bit floating point value can round to same number that you see in the watch window, so there's no point in showing you more decimal digits.

    The only thing strange thing in your watch window is the last number. Since it's the result of multiplying two values together, it follows C rules for promotion. In this case it means that the result is a 64-bit floating point value, a double instead of a float, so the watch window displays the result with number of decimal digits that 64-bit floating point values are accurate to. Unfortunately this misleads you into thinking that the number is more accurate than it really is. Since it's the result of multiplying two 32-bit floating pointer numbers, it really only has 32-bit floating point accuracy.


  • JoelT3

    I had already tried to add the D3DCREATE_FPU_PRESERVE but the problem persisted. Besides, I don't want to change the default D3D fp behavior.

    I had already solved the problem in a similar way that u suggested using a threshold.

    What I think is the stranger, is that the watch window in the VS doesn't show the correct values.

    Thank you

  • Floating point issue