Numerical Robustness for Numerical Robustness for Geometric Calculations Geometric Calculations Christer Ericson Christer Ericson Sony Computer Sony Computer Entertainment Entertainment Slides @ Slides @ http://realtimecollisiondetection.net/pubs/ http://realtimecollisiondetection.net/pubs/
54
Embed
Numerical Robustness for Geometric Calculations Christer Ericson Sony Computer Entertainment Slides @
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Numerical Robustness for Numerical Robustness for Geometric CalculationsGeometric Calculations
Christer EricsonChrister Ericson
Sony Computer Sony Computer EntertainmentEntertainment
The contents of this presentation is The contents of this presentation is discussed in further detail in my discussed in further detail in my book:book:
If we could work in real arithmetic, I If we could work in real arithmetic, I wouldn’t be having this talk!wouldn’t be having this talk!
Floating-point numbersFloating-point numbers IEEE-754 single precisionIEEE-754 single precision
1 bit sign1 bit sign 8 bit exponent (biased)8 bit exponent (biased) 23 bits fraction (24 bits mantissa, including hidden 23 bits fraction (24 bits mantissa, including hidden
bit)bit)
ss Exponent (e)Exponent (e) Fraction (f)Fraction (f)
This is a This is a normalizednormalized format format
––Inf, +Inf, NaNInf, +Inf, NaN Some examples:Some examples:
a/0 = +Inf, if a > 0a/0 = +Inf, if a > 0 a/0 = –Inf, if a < 0a/0 = –Inf, if a < 0 0/0 = Inf – Inf = ±Inf · 0 = NaN0/0 = Inf – Inf = ±Inf · 0 = NaN
Known as Known as Infinity ArithmeticInfinity Arithmetic ( (IAIA))
Floating-point numbersFloating-point numbers IA is a potential source of robustness IA is a potential source of robustness
errors!errors! +Inf and –Inf compare as normal+Inf and –Inf compare as normal But NaN compares as unorderedBut NaN compares as unordered
NaN != NaN is trueNaN != NaN is true All other comparisons involving NaNs are falseAll other comparisons involving NaNs are false
These expressions are not equivalent:These expressions are not equivalent: if (a > b) X(); else Y();
if (a <= b) Y(); else X();
Floating-point numbersFloating-point numbers But IA provides a nice feature tooBut IA provides a nice feature too Allows not having to test for div-by-Allows not having to test for div-by-
zerozero Removes test branch from inner loopRemoves test branch from inner loop Useful for SIMD codeUseful for SIMD code
(Although same approach usually (Although same approach usually works for non-IEEE CPUs too.)works for non-IEEE CPUs too.)
Floating-point numbersFloating-point numbers Irregular number lineIrregular number line
Spacing increases the farther away Spacing increases the farther away from zero a number is locatedfrom zero a number is located
Number range for exponent Number range for exponent k+1k+1 has has twice the spacing of the one for twice the spacing of the one for exponent exponent kk
Equally many representable numbers Equally many representable numbers from one exponent to anotherfrom one exponent to another
0
Floating-point numbersFloating-point numbers Consequence of irregular spacing:Consequence of irregular spacing:
#define EPSILON 0.000001#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]).../* if determinant is near zero, ray lies in plane of triangle */det = DOT(edge1, pvec);...if (det > -EPSILON && det < EPSILON) // Abs(det) < EPSILON return 0;
Written using doubles.Written using doubles. Change to float without changing epsilon?Change to float without changing epsilon? DOT({10,10,10},{10,10,10}) breaks test!DOT({10,10,10},{10,10,10}) breaks test!
Relative toleranceRelative tolerance
Comparing two floats for equality:Comparing two floats for equality:
Epsilon scaled Epsilon scaled byby magnitude of inputs magnitude of inputs But consider Abs(x)<1.0, Abs(y)<1.0But consider Abs(x)<1.0, Abs(y)<1.0
if (Abs(x – y) <= EPSILON * Max(Abs(x), Abs(y)) …
Combined toleranceCombined tolerance
Comparing two floats for equality:Comparing two floats for equality:
Absolute test for Abs(x)≤1.0, Abs(y)≤1.0Absolute test for Abs(x)≤1.0, Abs(y)≤1.0 Relative test otherwise!Relative test otherwise!
bool AlmostEqual2sComplement(float A, float B, int maxUlps){ // Make sure maxUlps is non-negative and small enough that the // default NAN won't compare as equal to anything. assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024); int aInt = *(int*)&A; // Make aInt lexicographically ordered as a twos-complement int if (aInt < 0) aInt = 0x80000000 - aInt; // Make bInt lexicographically ordered as a twos-complement int int bInt = *(int*)&B; if (bInt < 0) bInt = 0x80000000 - bInt; int intDiff = abs(aInt - bInt); if (intDiff <= maxUlps) return true; return false;}
Also suggested, an integer-based test:Also suggested, an integer-based test:
Floating-point numbersFloating-point numbers
Caveat: Intel uses 80-bit format Caveat: Intel uses 80-bit format internallyinternally Unless told otherwise.Unless told otherwise. Errors dependent on what code Errors dependent on what code
generated.generated. Gives different results in debug and Gives different results in debug and
release.release.
EXACTEXACTARITHMETICARITHMETIC
(and semi-exact ditto)(and semi-exact ditto)
Exact arithmeticExact arithmetic Hey! Integer arithmetic is exactHey! Integer arithmetic is exact
As long as there is no overflowAs long as there is no overflow Closed under +, –, and *Closed under +, –, and * Not closed under / but can often remove Not closed under / but can often remove
divisions through cross multiplicationdivisions through cross multiplication
Exact arithmeticExact arithmetic Example: Does Example: Does CC project onto project onto AB AB ??
A B
C
D
float t = Dot(AC, AB) / Dot(AB, AB);if (t >= 0.0f && t <= 1.0f) ... /* do something */
Exact arithmeticExact arithmetic Another example:Another example:
A
B
C
D
Exact arithmeticExact arithmetic TestsTests
Boolean, can be evaluated exactlyBoolean, can be evaluated exactly ConstructionsConstructions
Non-Boolean, cannot be done exactlyNon-Boolean, cannot be done exactly
Exact arithmeticExact arithmetic Tests, often expressed as determinant Tests, often expressed as determinant
predicates. E.g.predicates. E.g.
Shewchuk's predicates well-known exampleShewchuk's predicates well-known example Evaluates using extended-precision arithmetic Evaluates using extended-precision arithmetic
(EPA)(EPA) EPA is expensive to evaluateEPA is expensive to evaluate
Limit EPA use through “floating-point filter”Limit EPA use through “floating-point filter” Common filter is interval arithmeticCommon filter is interval arithmetic
Intervals must be rounded up/down to Intervals must be rounded up/down to nearest machine-representable numbernearest machine-representable number
Is a reliable calculationIs a reliable calculation
ReferencesReferences Ericson, Christer. Ericson, Christer. Real-Time Collision DetectionReal-Time Collision Detection. Morgan Kaufmann, . Morgan Kaufmann,
2005. 2005. http://http://realtimecollisiondetection.net/realtimecollisiondetection.net/ Hoffmann, Christoph. Hoffmann, Christoph. Geometric and Solid Modeling: An IntroductionGeometric and Solid Modeling: An Introduction. .
Morgan Kaufmann, 1989.Morgan Kaufmann, 1989. Ratschek, Helmut. Jon Rokne. Ratschek, Helmut. Jon Rokne. Geometric Computations with Interval Geometric Computations with Interval
and New Robust Methodsand New Robust Methods. Horwood Publishing, 2003.. Horwood Publishing, 2003.
Hoffmann, Christoph. “Hoffmann, Christoph. “Robustness in Geometric ComputationsRobustness in Geometric Computations.” JCISE 1, 2001, .” JCISE 1, 2001, pp. 143-156. pp. 143-156. http://www.cs.purdue.edu/homes/cmh/distribution/papers/Robustness/robust4.pdfhttp://www.cs.purdue.edu/homes/cmh/distribution/papers/Robustness/robust4.pdf
Santisteve, Francisco. “Santisteve, Francisco. “Robust Geometric Computation (RGC), State of the Robust Geometric Computation (RGC), State of the ArtArt.” Technical report, 1999. .” Technical report, 1999. http://www.lsi.upc.es/dept/techreps/ps/R99-19.ps.gzhttp://www.lsi.upc.es/dept/techreps/ps/R99-19.ps.gz
Schirra, Stefan. “Schirra, Stefan. “Robustness and precision issues in geometric computation.Robustness and precision issues in geometric computation.” ” Research Report MPI-I-98-004, Max Planck Institute for Computer Science, 1998. Research Report MPI-I-98-004, Max Planck Institute for Computer Science, 1998. httphttp://domino.mpi-sb.mpg.de/internet/reports.nsf/NumberView/1998-1-004://domino.mpi-sb.mpg.de/internet/reports.nsf/NumberView/1998-1-004
Shewchuk, Jonathan. “Shewchuk, Jonathan. “Adaptive Precision Floating-Point Arithmetic and Fast Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates.Robust Geometric Predicates.” Discrete & Computational Geometry 18(3):305-” Discrete & Computational Geometry 18(3):305-363, October 1997. 363, October 1997. http://www.cs.cmu.eduhttp://www.cs.cmu.edu/~quake-papers/robust-arithmetic.ps/~quake-papers/robust-arithmetic.ps