Examples

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#

1. public static void PredicateExample()
2. {
3.         // Test if a point lies on a plane -----------------------------------
4.         // Create a plane
5.         Point p1 = new Point(0, 0, 0);
6.         Point p2 = new Point(1, 1, 1);
7.         Point p3 = new Point(-0.5, 0.5, 0);
8.         Plane pl = new Plane(p1, p2, p3);
9.
10.         // This point definitely lies on the plane:
11.         Point pt = new Point(0.6666666666666666,
12.                              0.6666666666666666,
13.                              0.6666666666666666);
14.
15.         // Test if the plane contains the point using floating point arithmetic
16.         // Outputs -1.38777878078145E-17 which is not 0, so this test failes!
17.         Console.WriteLine("Result using floating point arithmetic: " +
18.                           Predicate.Orient3d(p1, p2, p3, pt));
19.
20.         // Test if the plane contains the point using eaxt arithmetic
21.         // Outputs 0. This test works!
22.         Console.WriteLine("Result using exact arithmetic:          " +
23.                           Predicate.Orient3d_exact(p1, p2, p3, pt));
24.
25. }

<<back