Problem: Floating point arithmetic errors
Finite precision and roundoff errors of floating point arithmetic may produce incorrect results for near-degenerate cases. Following example first generates a point on a plane and then tests if the point lies on the plane. A floating point arithmetic point-on-plane test failes. An exact arithmetic point-on-plane test returns the correct result.
Solution using G#
- public static void PredicateExample()
- {
- // Test if a point lies on a plane -----------------------------------
- // Create a plane
- // This point definitely lies on the plane:
- 0.6666666666666666,
- 0.6666666666666666);
- // Test if the plane contains the point using floating point arithmetic
- // Outputs -1.38777878078145E-17 which is not 0, so this test failes!
- Console.WriteLine("Result using floating point arithmetic: " +
- Predicate.Orient3d(p1, p2, p3, pt));
- // Test if the plane contains the point using eaxt arithmetic
- // Outputs 0. This test works!
- Console.WriteLine("Result using exact arithmetic: " +
- Predicate.Orient3d_exact(p1, p2, p3, pt));
- }
