Physics for Games Programmers: Numerical Robustness (for Geometric Calculations) Christer Ericson Sony Computer Entertainment

Slides:



Advertisements
Similar presentations
Numerical Robustness for Geometric Calculations
Advertisements

Fixed-point and floating-point numbers CS370 Fall 2003.
COMP2130 Winter 2015 Storing signed numbers in memory.
Computer Engineering FloatingPoint page 1 Floating Point Number system corresponding to the decimal notation 1,837 * 10 significand exponent A great number.
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved Floating-point Numbers.
Floating Point (FLP) Representation A Floating Point value: f = m*r**e Where: m – mantissa or fractional r – base or radix, usually r = 2 e - exponent.
Floating Point Numbers. CMPE12cGabriel Hugh Elkaim 2 Floating Point Numbers Registers for real numbers usually contain 32 or 64 bits, allowing 2 32 or.
Floating Point Numbers. CMPE12cCyrus Bazeghi 2 Floating Point Numbers Registers for real numbers usually contain 32 or 64 bits, allowing 2 32 or 2 64.
Major Numeric Data Types Unsigned Integers Signed Integer Alphanumeric Data – ASCII & UNICODE Floating Point Numbers.
Chapter 5 Floating Point Numbers. Real Numbers l Floating point representation is used whenever the number to be represented is outside the range of integer.
Floating Point Numbers
Computer Science 210 Computer Organization Floating Point Representation.
Lecture 8 Floating point format
Binary Representation and Computer Arithmetic
Factional Values What is 0.75 in binary?. How could we represent fractions? In decimal: – As fractions : 1/5.
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Chapter Contents.
Numeric precision in SAS. Two aspects of numeric data in SAS The first is how numeric data are stored (how a number is represented in the computer). –
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Number Systems So far we have studied the following integer number systems in computer Unsigned numbers Sign/magnitude numbers Two’s complement numbers.
Introduction to Numerical Analysis I
Computer Architecture
Data Representation - Part II. Characters A variable may not be a non-numerical type Character is the most common non- numerical type in a programming.
Computer Science Engineering B.E.(4 th sem) c omputer system organization Topic-Floating and decimal arithmetic S ubmitted to– Prof. Shweta Agrawal Submitted.
The Teacher CP4 Binary and all that… CP4 Revision.
1 Lecture 2  Complement  Floating Point Number  Character Encoding.
The Teacher CP4 Binary and all that… CP4 Revision.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Floating Point Representation.
1 Number Systems Lecture 10 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Computer Engineering FloatingPoint page 1 Floating Point Number system corresponding to the decimal notation 1,837 * 10 significand exponent A great number.
Data Representation: Floating Point for Real Numbers Computer Organization and Assembly Language: Module 11.
IT11004: Data Representation and Organization Floating Point Representation.
Representation of Data (Part II) Computer Studies Notes: chapter 19 Ma King Man.
Lecture 6: Floating Point Number Representation Information Representation: Floating Point Number Representation Lecture # 7.
Fixed-point and floating-point numbers Ellen Spertus MCS 111 October 4, 2001.
Floating Point (FLP) Representation
Floating Point Arithmetic – Part I
FLOATING-POINT NUMBER REPRESENTATION
CSCI206 - Computer Organization & Programming
Introduction to Numerical Analysis I
Fundamentals of Computer Science
Introduction To Computer Science
Floating Point Representations
CSCI206 - Computer Organization & Programming
Floating Point Number system corresponding to the decimal notation
IEEE floating point format
PRESENTED BY J.SARAVANAN. Introduction: Objective: To provide hardware support for floating point arithmetic. To understand how to represent floating.
Chapter 6 Floating Point
CSCI206 - Computer Organization & Programming
Number Representations
BCD = Binary Coded Decimal
Systems Architecture I
Floating Point Representation
Computer Science 210 Computer Organization
Floating Point Arithmetic
How to represent real numbers
Approximations and Round-Off Errors Chapter 3
Numbers representations
Systems Architecture I
COMS 161 Introduction to Computing
CS 286 Computer Architecture & Organization
IT11004: Data Representation and Organization
Numbers with fractions Could be done in pure binary
Number Representations
Floating Point Arithmetic
Lecture 9: Shift, Mult, Div Fixed & Floating Point
CS 105 “Tour of the Black Holes of Computing!”
Presentation transcript:

Physics for Games Programmers: Numerical Robustness (for Geometric Calculations) Christer Ericson Sony Computer Entertainment

Physics for Games Programmers: Numerical Robustness (for Geometric Calculations) EPSILON is NOT ! Christer Ericson Sony Computer Entertainment

Takeaway An appreciation of the pitfalls inherent in working with floating-point arithmetic. Tools for addressing the robustness of floating-point based code. Probably something else too.

THE PROBLEM Floating-point arithmetic

Floating-point numbers Real numbers must be approximated Floating-point numbers Fixed-point numbers (integers) Rational numbers Homogeneous representation If we could work in real arithmetic, I wouldnt be having this talk!

Floating-point numbers IEEE-754 single precision 1 bit sign 8 bit exponent (biased) 23 bits fraction (24 bits mantissa w/ hidden bit) s Exponent (e) Fraction (f) This is a normalized format

Floating-point numbers IEEE-754 representable numbers: ExponentFractionSignValue 0<e<255 e=0f=0s=0 e=0f=0s=1 e=0f0 e=255f=0s=0 e=255f=0s=1 e=255f0

Floating-point numbers In IEEE-754, domain extended with: –Inf, +Inf, NaN Some examples: a/0 = +Inf, if a > 0 a/0 = –Inf, if a < 0 0/0 = Inf – Inf = ±Inf · 0 = NaN Known as Infinity Arithmetic (IA)

Floating-point numbers IA is a potential source of robustness errors! +Inf and –Inf compare as normal But NaN compares as unordered NaN != NaN is true All other comparisons involving NaNs are false These expressions are not equivalent: if (a > b) X(); else Y(); if (a <= b) Y(); else X();

Floating-point numbers But IA provides a nice feature too Allows not having to test for div-by- zero Removes test branch from inner loop Useful for SIMD code (Although same approach usually works for non-IEEE CPUs too.)

Floating-point numbers Irregular number line Spacing increases the farther away from zero a number is located Number range for exponent k+1 has twice the spacing of the one for exponent k Equally many representable numbers from one exponent to another 0

0 Floating-point numbers Consequence of irregular spacing: – ( ) = 0 (– ) + 1 = 1 Thus, not associative (in general): (a + b) + c != a + (b + c) Source of endless errors!

Floating-point numbers All discrete representations have non- representable points A BC D Q P

The floating-point grid In floating-point, behavior changes based on position, due to the irregular spacing!

EXAMPLE Polygon splitting

Sutherland-Hodgman clipping algorithm A BB C I D C AD J

Polygon splitting Enter floating-point errors! I P Q P Q IFIF

Polygon splitting ABCD split against a plane A B D C A B D J I C

Polygon splitting Thick planes to the rescue! P Q Desired invariant: OnPlane(I, plane) = true where: I = IntersectionPoint(PQ, plane)

Polygon splitting Thick planes also help bound the error PQP'Q' PQ e e P'Q'

A B D C Polygon splitting ABCD split against a thick plane A B D C I

Polygon splitting Cracks introduced by inconsistent ordering A B C D A B C D

EXAMPLE BSP-tree robustness

Robustness problems for: Insertion of primitives Querying (collision detection) Same problems apply to: All spatial partitioning schemes! (k-d trees, grids, octrees, quadtrees, …)

BSP-tree robustness Query robustness 12 I P Q IFIF C A B

BSP-tree robustness Insertion robustness 12 C A B 12 C A B I IFIF

BSP-tree robustness How to achieve robustness? Insert primitives conservatively Accounting for errors in querying and insertion Can then ignore problem for queries

EXAMPLE Ray-triangle test

Common approach: Compute intersection point P of ray R with plane of triangle T Test if P lies inside boundaries of T Alas, this is not robust!

Ray-triangle test A problem configuration R P A B C D

Ray-triangle test Intersecting R against one plane R P

Ray-triangle test Intersecting R against the other plane R P

Robust test must share calculations for shared edge AB Perform test directly in 3D! Let ray be Then, sign of says whether is left or right of AB If R left of all edges, R intersects CCW triangle Only then compute P Still errors, but managable Ray-triangle test

Fat tests are also robust! P

EXAMPLES SUMMARY Achieve robustness through… (Correct) use of tolerances Sharing of calculations Use of fat primitives

TOLERANCES

Tolerance comparisons Absolute tolerance Relative tolerance Combined tolerance (Integer test)

Absolute tolerance Almost never used correctly! What should EPSILON be? Typically arbitrary small number used! OMFG!! if (Abs(x – y) <= EPSILON) … Comparing two floats for equality:

Absolute tolerances Delta step to next representable number: DecimalHexNext representable number 10.00x x x42C80000x ,000.00x447A0000x ,000.00x461C4000x ,000.00x47C35000x ,000,000.00x x ,000,000.00x4B189680x + 1.0

Absolute tolerances Möller-Trumbore ray-triangle code: #define EPSILON #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. Change to float without changing epsilon? DOT({10,10,10},{10,10,10}) breaks test!

Relative tolerance Comparing two floats for equality: Epsilon scaled by magnitude of inputs But consider Abs(x)<1.0, Abs(y)<1.0 if (Abs(x – y) <= EPSILON * Max(Abs(x), Abs(y)) …

Combined tolerance Comparing two floats for equality: Absolute test for Abs(x)1.0, Abs(y)1.0 Relative test otherwise! if (Abs(x – y) <= EPSILON * Max(1.0f, Abs(x), Abs(y)) …

Floating-point numbers Caveat: Intel uses 80-bit format internally Unless told otherwise. Errors dependent on what code generated. Gives different results in debug and release.

EXACT ARITHMETIC (and semi-exact ditto)

Exact arithmetic Hey! Integer arithmetic is exact As long as there is no overflow Closed under +, –, and * Not closed under / but can often remove divisions through cross multiplication

Exact arithmetic Example: Does C project onto AB ? AB C D float t = Dot(AC, AB) / Dot(AB, AB); if (t >= 0.0f && t <= 1.0f)... /* do something */ int tnum = Dot(AC, AB), tdenom = Dot(AB, AB); if (tnum >= 0 && tnum <= tdenom)... /* do something */ Floats: Integers:

Exact arithmetic Another example: A B C D

Exact arithmetic Tests Boolean, can be evaluated exactly Constructions Non-Boolean, cannot be done exactly

Exact arithmetic Tests, often expressed as determinant predicates. E.g. Shewchuk's predicates well-known example Evaluates using extended-precision arithmetic (EPA) EPA is expensive to evaluate Limit EPA use through floating-point filter Common filter is interval arithmetic

Exact arithmetic Interval arithmetic x = [1,3] = { x R | 1 x 3 } Rules: [a,b] + [c,d] = [a+c,b+d] [a,b] – [c,d] = [a–d,b–c] [a,b] * [c,d] = [min(ac,ad,bc,bd), max(ac,ad,bc,bd)] [a,b] / [c,d] = [a,b] * [1/d,1/c] for 0 [c,d] E.g. [100,101] + [10,12] = [110,113]

Exact arithmetic Interval arithmetic Intervals must be rounded up/down to nearest machine-representable number Is a reliable calculation

References Ericson, Christer. Real-Time Collision Detection. Morgan Kaufmann, Hoffmann, Christoph. Geometric and Solid Modeling: An Introduction. Morgan Kaufmann, Ratschek, Helmut. Jon Rokne. Geometric Computations with Interval and New Robust Methods. Horwood Publishing, Hoffmann, Christoph. Robustness in Geometric Computations. JCISE 1, 2001, pp Santisteve, Francisco. Robust Geometric Computation (RGC), State of the Art. Technical report, Schirra, Stefan. Robustness and precision issues in geometric computation. Research Report MPI-I , Max Planck Institute for Computer Science, Shewchuk, Jonathan. Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates. Discrete & Computational Geometry 18(3): , October BOOKS PAPERS