Floating point strikes back

I'm starting to realy hate this floating point hell.
I have another problem with floats, and this time I can't see the solution.
I have a simple mesh (just a square with two faces with size 100).
The vertice list in the x file is as follows:

-50.000000;0.000000;-50.000000;
50.000000;0.000000;-50.000000;
-50.000000;0.000000;50.000000;
50.000000;0.000000;50.000000;


Now, when I access the vertice buffer after loading the mesh, the values are not these. they are for instance:
-49.999996, 0, 49.999996

Wich causes a colision test with a ray that passes on the edge to miss the test.
I've tried once again to use the D3DCREATE_FPU_PRESERVE flag but the problem persisted.






Answer this question

Floating point strikes back

  • liu1323

    To answer your questions:

    "In general you wouldn't worry about this."

    Well, I want my spaceship to explose if the center of it is not above any ground. So I'm using D3DXIntersect() to check this. Sometimes the test fails even when my spaceship is in the center of a squared mesh (that's because of the rounding I described above).

    "How are you creating the vertex buffer "

    I'm using D3DXLoadMeshFromX().


    "Do you use a binary or text based X file "
    I'm using text based X files. I'm going to try binary.




  • John Locke

    As 50.0 can store as 32 bit floating point value (as ZMan already mentioned) I am think the problem is inside the x file loader. Do you use a binary or text based X file



  • Arun SB

    I've tried the binary format but is no good (with and whithout FPU_PRESEVER).
    Maybe I'll just use a thin bounding box instead of a ray...

  • ybkang

    In general you wouldn't worry about this. Accuracy at that level is sub pixel in real world terms and would not be visible.

    However if its vitally important you just have to build some tolerance in. e.g. you don't do a ray/triangle intersect you do a thin cylinder/triangle intercept or call it an intercept if the distance is above some tolerance level.

    I'm surprised 50 can't be represented butter by a single (and especially by a double when you set the FPU flag) but at the end of the day floating point is an approximation.



  • Jimmyusa

    How are you creating the vertex buffer. Its odd that its not really storing 50 becuase 50 can be stored accuratly in a single according to the floating point calculator.

    (I just made a floating point FAQ entry on thezbuffer to cover some of these issues)



  • Floating point strikes back