Problem 934 - 8-point G4Trap construction test does not allow for round off error
Summary: 8-point G4Trap construction test does not allow for round off error
Status: RESOLVED FIXED
Alias: None
Product: Geant4
Classification: Unclassified
Component: geometry/solids (show other problems)
Version: 8.1
Hardware: All All
: P5 normal
Assignee: Vladimir.Grichine
URL:
Depends on:
Blocks:
 
Reported: 2007-03-28 16:12 CEST by Scott Zelakiewicz
Modified: 2008-04-18 15:07 CEST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this problem.
Description Scott Zelakiewicz 2007-03-28 16:12:26 CEST
G4Trap construction was occasionally failing when using the 8-point construction method.

When constructing a new G4Trap using 8 points, a test is performed to check the the points conform to the expected order and the object is in the correct orientation.  To do this, a number of comparisons are made using the sums of the x,y and z components.  The tests check for equality to zero using '==', but this kind of comparison is generally a bad idea for floating point numbers due to round-off errors.  A better solution is to test against a tolerance limit.

The patch is attached below, applied from the 'source' directory, for 8.1.p02:



--- geometry/solids/CSG/src/G4Trap.cc.old       2007-03-28 08:13:57.000000000 -0
400
+++ geometry/solids/CSG/src/G4Trap.cc   2007-03-28 08:21:26.000000000 -0400
@@ -135,17 +135,23 @@
 {
   // Start with check of centering - the center of gravity trap line
   // should cross the origin of frame
-
+  double tolerance = 1e-9;
   if (   pt[0].z() < 0
-      && pt[0].z() == pt[1].z() && pt[0].z() == pt[2].z() && pt[0].z() == pt[3]
.z()
+      && fabs( pt[0].z() - pt[1].z() ) < tolerance
+      && fabs( pt[0].z() -pt[2].z() ) < tolerance
+      && fabs( pt[0].z() - pt[3].z() ) < tolerance
       && pt[4].z() > 0
-      && pt[4].z() == pt[5].z() && pt[4].z() == pt[6].z() && pt[4].z() == pt[7]
.z()
-      && ( pt[0].z() + pt[4].z() ) == 0
-      && pt[0].y() == pt[1].y() && pt[2].y() == pt[3].y()
-      && pt[4].y() == pt[5].y() && pt[6].y() == pt[7].y()
-      && ( pt[0].y() + pt[2].y() + pt[4].y() + pt[6].y() ) == 0
-      && ( pt[0].x() + pt[1].x() + pt[4].x() + pt[5].x() +
-           pt[2].x() + pt[3].x() + pt[6].x() + pt[7].x() ) == 0 )
+      && fabs( pt[4].z() - pt[5].z() ) < tolerance
+      && fabs( pt[4].z() - pt[6].z() ) < tolerance
+      && fabs( pt[4].z() - pt[7].z() ) < tolerance
+      && fabs( pt[0].z() + pt[4].z() ) < tolerance
+      && fabs( pt[0].y() - pt[1].y() ) < tolerance
+      && fabs( pt[2].y() - pt[3].y() ) < tolerance
+      && fabs( pt[4].y() - pt[5].y() ) < tolerance
+      && fabs( pt[6].y() - pt[7].y() ) < tolerance
+      && fabs( pt[0].y() + pt[2].y() + pt[4].y() + pt[6].y() ) < tolerance
+      && fabs( pt[0].x() + pt[1].x() + pt[4].x() + pt[5].x() +
+           pt[2].x() + pt[3].x() + pt[6].x() + pt[7].x() ) < tolerance )
   {
     G4bool good;
Comment 1 Gabriele Cosmo 2007-04-04 13:29:05 CEST
To be verified by the author.
Comment 2 Vladimir.Grichine 2008-04-18 15:07:32 CEST
The severity of conditions inside 8-point constructor for G4Trap was changed 
for the cases where there are arithmetic actions on the point coordinates.