SlideShare une entreprise Scribd logo
1  sur  43
InfoQ.com: News & Community Site
• 750,000 unique visitors/month
• Published in 4 languages (English, Chinese, Japanese and Brazilian
Portuguese)
• Post content from our QCon conferences
• News 15-20 / week
• Articles 3-4 / week
• Presentations (videos) 12-15 / week
• Interviews 2-3 / week
• Books 1 / month
Watch the video with slide
synchronization on InfoQ.com!
http://www.infoq.com/presentations
/IEEE754
http://www.infoq.com/presentati
ons/nasa-big-data
http://www.infoq.com/presentati
ons/nasa-big-data
Presented at QCon London
www.qconlondon.com
Purpose of QCon
- to empower software development by facilitating the spread of
knowledge and innovation
Strategy
- practitioner-driven conference designed for YOU: influencers of
change and innovation in your teams
- speakers and topics driving the evolution and innovation
- connecting and catalyzing the influencers and innovators
Highlights
- attended by more than 12,000 delegates since 2007
- held in 9 cities worldwide
Catastrophic cancellation: the
pitfalls of floating point arithmetic
(and how to avoid them!)
Graham Markall
Quantitative Developer, OpenGamma
Intro/Disclaimers
• Aims:
• Get a rough “feel” of how floating point works
• Know when to dig deeper
• Cover basics, testing, and optimisation
• Not an exhaustive trudge through algorithms + details
• This talk: IEEE754 – mostly, but not quite ubiquitous
• Mostly C/Python examples, Linux/x86_64
• No complex numbers
• Code samples available!
A problem (C)
#include <values.h>
float a = MAXINT; // 2147483648
float b = MAXLONG; // 9223372036854775808
float f = a + b;
f == MAXLONG; // True or false?
Code ex. 1
A problem (C)
#include <values.h>
float a = MAXINT; // 2147483648
float b = MAXLONG; // 9223372036854775808
float f = a + b;
f == MAXLONG; // True or false?
// True!
// IEEE754 only approximates real arithmetic
Code ex. 1
How is arithmetic on reals
approximated?
// float gives about 7 digits of accuracy
*******---.------
MAXINT: 2147483648.000000
MAXLONG: 9223372036854775808.000000
*******------------.------
// ^ ^
// | |
// Represented “Lost” beneath
// unit of
// least precision
Floating point representation (1)
Sign – exponent – mantissa
s mantissa * 2exponent
Sign bit: 0 = positive, 1 = negative
Mantissa: 1.xxxxxxxxxx…
FP representation (2)
S|EEEEEEEE|1|MMMMMMMMMMMMMMMMMMMMMMM
// MAXINT: S = 0, M = 1.0, E = 158
0|10011110|1|00000000000000000000000
+ 1.0 * 2158-127 = 2147483648.0
A
A
A
a
Mantissa has:
implied leading 1
Exponent has:
bias (-127 for float)
FP representation (2)
S|EEEEEEEE|1|MMMMMMMMMMMMMMMMMMMMMMM
// MAXINT: S = 0, M = 1.0, E = 158
0|10011110|1|00000000000000000000000
+ 1.0 * 2158-127 = 2147483648.0
// MAXLONG: S = 0, M = 1.0, E = 190
0|10111110|1|00000000000000000000000
+ 1.0 * 2190-127 = 9223372036854775808.0
Mantissa has:
implied leading 1
Exponent has:
bias (-127 for float)
MAXLONG smallest increment
// MAXLONG: S = 0, M = 1.0, E = 190
0|10111110|1|00000000000000000000000
+ 1.0 * 2190-127 = 9223372036854775808.0
0|10111110|1|00000000000000000000001
+ 1.0000001192092896 * 2190-127 =
9223373136366403584.0
Code ex. 2
On the number line
MAXLONG
(9223372036854775808.0)
MAXLONG_NEXT
(9223373136366403584.0)
MAXLONG + MAXINT
(0.19% towards MAXLONG_NEXT)
Precision and range summary
• Precision: Mantissa length
• Range: Exponent length
• Float, 4 bytes:
23 bit mantissa, 8 bit exponent
Precision: ~7.2 digits
Range: 1.17549e-38, 3.40282e+38
• Double, 8 bytes:
52 bit mantissa, 11 bit exponent
Precision: ~15.9 digits
Range: 2.22507e-308, 1.79769e+308
Code ex. 3
Special cases
When is a number not a number?
Floating point closed arithmetic
• Integer arithmetic:
• 1/0 // Arithmetic exception
• Floating point arithmetic is closed:
• Domain (double):
• 2.22507e-308 <-> 1.79769e+308
• 4.94066e-324 <-> just beneath 2.22507e-308
• +0, -0
• Inf
• NaN
• Exceptions are exceptional – traps are
exceptions
Code ex. 4, 5
A few exceptional values
1/0 = Inf // Limit
-1/0 = -Inf // Limit
0/0 = NaN // 0/x = 0, x/0 = Inf
Inf/Inf = NaN // Magnitudes unknown
Inf + (-Inf) = NaN // Magnitudes unknown
0 * Inf = NaN // 0*x = 0, Inf*x = Inf
sqrt(x), x<0 = NaN // No complex
Code ex. 6
Consequences
// Inf, NaN propagation:
double n = 1000.0;
for(double i = 0.0; i < 100.0; i += 1.0)
n = n / i;
printf(“%f”, n); // “Inf”
Code ex. 7
Trapping exceptions (Linux, GNU)
• feenableexcept(int __excepts)
• FE_INXACT – Inexact result
• FE_DIVBYZERO - Division by zero
• FE_UNDERFLOW - Underflow
• FE_OVERFLOW - Overflow
• FE_INVALID - Invalid operand
• SIGFPE Not exclusive to floating point:
• int i = 0; int j = 1; j/i // Receives SIGFPE!
Code ex. 8-12
Back in the normal range
Some exceptional inputs
to some math library functions
result in normal-range results:
x = tanh(Inf) // x is 1.0
y = atan(Inf) // y is pi/2
(ISO C / IEEE Std 1003.1-2001)
Code ex. 13
Denormals
• x – y == 0 implies x == y ?
• Without denormals, this is not true:
• X = 2.2250738585072014e-308
• Y = 2.2250738585072019e-308 // (5e-324)
• Y – X = 0
• With denormals:
• 4.9406564584124654e-324
• Denormal implementation e = 0:
• Implied leading 1 is not a 1 anymore
• Performance: revisited later
Code ex. 14
Testing
Getting getting right right
Assumptions
• Code that does floating-point computation
• Needs tests to ensure:
• Correct results
• Handling of exceptional cases
• A function to compare floating point numbers is
required
Exact equality (danger)
def equal_exact(a, b):
return a == b
equal_exact(1.0+2.0, 3.0) # True
equal_exact(2.0, sqrt(2.0)**2.0) # False
sqrt(2.0)**2 # 2.0000000000000004
Code ex. 15
Absolute tolerance
def equal_abs(a, b, eps=1.0e-7):
return fabs(a - b) < eps
equal_abs(1.0+2.0, 3.0) # True
equal_abs(2.0, sqrt(2.0)**2.0) # True
Code ex. 16
Absolute tolerance eps choice
equal_abs(2.0, sqrt(2)**2, 1.0e-16) # False
equal_abs(1.0e-8, 2.0e-8) # True!
Code ex. 17
Relative tolerance
def equal_rel(a, b, eps=1.0e-7):
m = min(fabs(a), fabs(b))
return (fabs(a – b) / m) < eps
equal_rel(1.0+2.0, 3.0) # True
equal_rel(2.0, sqrt(2.0)**2.0) # True
equal_rel(1.0e-8, 2.0e-8) # False
Code ex. 18
Relative tolerance correct digits
eps Correct digits
1.0e-1 ~1
1.0e-2 ~2
1.0e-3 ~3
…
1.0e-16 ~16
Code ex. 19
Relative tolerance near zero
equal_rel(1.0e-50, 0)
ZeroDivisionError: float division by zero
Code ex. 20
Summary guidelines:
When to use:
• Exact equality: Never
• Absolute tolerance: Expected ~ 0.0
• Relative tolerance: Elsewhere
• Tolerance choice:
• No universal “correct” tolerance
• Implementation/application specific
• Appropriate range: application specific
Checking special cases
-0 == 0 // True
Inf == Inf // True
-Inf == -Inf // True
NaN == NaN // False
Inf == NaN // False
NaN < 1.0 // False
NaN > 1.0 // False
NaN == 1.0 // False
isnan(NaN) // True
Code ex. 21
Performance
optimisation
Manual and automated.
Division vs Reciprocal multiply
// Slower (generally)
a = x/y; // Divide instruction
// Faster (generally)
y1 = 1.0/y; // x86: RCPSS instruction
a = x*y1; // Multiply instruction
// May lose precision.
// GCC: -freciprocal-math
Code ex. 22
Non-associativity
float a = 1.0e23;
float b = -1.0e23;
float c = 1.0;
printf("(a + b) + c = %fn", (a + b) + c);
printf("a + (b + c) = %fn", a + (b + c));
(a + b) + c = 1.000000
a + (b + c) = 0.000000
Code ex. 23
Non-associativity (2)
• Re-ordering is “unsafe”
• Turned off in compilers by default
• Enable (gcc):
-fassociative-math
• Turns on –fno-trapping, also –fno-
signed-zeros (may affect -0 == 0, flip sign
of -0*x)
Finite math only
• Assume that no Infs or NaNs are ever produced.
• Saves execution time: no code for checking/dealing
with them need be generated.
• GCC: -ffinite-math-only
• Any code that uses an Inf or NaN value will
probably behave incorrectly
• This can affect your tests! Inf == Inf may not be
true anymore.
-ffast-math
• Turns on all the optimisations we’ve just discussed.
• Also sets flush-to-zero/denormals-are-zero
• Avoids overhead of dealing with denormals
• x – y == 0 -> x == y may not hold
• For well-tested code:
• Turn on –ffast-math
• Do tests pass?
• If not, break into individual flags and test again.
-ffast-math linkage
• Also causes non-standard code to be linked in and
called
• e.g. crtfastmath.c set_fast_math()
• This can cause havoc when linking with other code.
• E.g. Java requires option to deal with this:
• -XX:RestoreMXCSROnJNICalls
Summary guidelines
• Refactoring and reordering of floating point can
increase performance
• Can also be unsafe
• Some transformations can be enabled by compiler
• Manual implementation also possible
• Make sure code well-tested
• Be prepared for trouble!
Wrap up
Floating point
• Finite approximation to real arithmetic
• Some “corner” cases:
• Denormals, +/- 0
• Inf, NaN
• Testing requires appropriate choice of:
• Comparison algorithm
• Expected tolerance and range
• Optimisation:
• For well-tested code
• Reciprocal, associativity, disable “edge case” handling
• FP can be a useful approximation to real arithmetic
Code samples/examples:
https://github.com/gmarkall/PitfallsFP
Watch the video with slide synchronization on
InfoQ.com!
http://www.infoq.com/presentations/IEEE754

Contenu connexe

Tendances

Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in cBUBT
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examplesmua99
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CBUBT
 
Mdp plus 2.1
Mdp plus 2.1Mdp plus 2.1
Mdp plus 2.1boedax
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)yap_raiza
 
Scala Under the Hood / ScalaSwarm
Scala Under the Hood / ScalaSwarmScala Under the Hood / ScalaSwarm
Scala Under the Hood / ScalaSwarmTzofia Shiftan
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C ProgrammingKamal Acharya
 
0.my book draft chap 1
0.my book draft chap 10.my book draft chap 1
0.my book draft chap 1manhduc1811
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in cBUBT
 
Why you-dont-need-design-patterns-in-python
Why you-dont-need-design-patterns-in-pythonWhy you-dont-need-design-patterns-in-python
Why you-dont-need-design-patterns-in-pythonSivanagaraju Pachipulusu
 
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);Joel Porquet
 
Things to avoid in JavaScript
Things to avoid in JavaScriptThings to avoid in JavaScript
Things to avoid in JavaScriptBrian Moschel
 
Coordenadas jugador
Coordenadas jugadorCoordenadas jugador
Coordenadas jugadorOscar Torres
 
Booth's algorithm part 4
Booth's algorithm part 4Booth's algorithm part 4
Booth's algorithm part 4babuece
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 

Tendances (20)

Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 
ICT Flowchart and Pseudo codes
ICT Flowchart and Pseudo codes ICT Flowchart and Pseudo codes
ICT Flowchart and Pseudo codes
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
Mdp plus 2.1
Mdp plus 2.1Mdp plus 2.1
Mdp plus 2.1
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
Scala Under the Hood / ScalaSwarm
Scala Under the Hood / ScalaSwarmScala Under the Hood / ScalaSwarm
Scala Under the Hood / ScalaSwarm
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
0.my book draft chap 1
0.my book draft chap 10.my book draft chap 1
0.my book draft chap 1
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
Why you-dont-need-design-patterns-in-python
Why you-dont-need-design-patterns-in-pythonWhy you-dont-need-design-patterns-in-python
Why you-dont-need-design-patterns-in-python
 
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
printf("%s from %c to Z, in %d minutes!\n", "printf", 'A', 45);
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Things to avoid in JavaScript
Things to avoid in JavaScriptThings to avoid in JavaScript
Things to avoid in JavaScript
 
input
inputinput
input
 
Coordenadas jugador
Coordenadas jugadorCoordenadas jugador
Coordenadas jugador
 
Booth's algorithm part 4
Booth's algorithm part 4Booth's algorithm part 4
Booth's algorithm part 4
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 

En vedette

Matrices in computer applications
Matrices in computer applicationsMatrices in computer applications
Matrices in computer applicationsRayyan777
 
Profitable growth via adjacency - Guest lecture on Zook by Peter Spung
Profitable growth via adjacency - Guest lecture on Zook by Peter SpungProfitable growth via adjacency - Guest lecture on Zook by Peter Spung
Profitable growth via adjacency - Guest lecture on Zook by Peter SpungPeter Spung
 
Multiplication of matrices and its application in biology
Multiplication of matrices and its application in biologyMultiplication of matrices and its application in biology
Multiplication of matrices and its application in biologynayanika bhalla
 
Appilation of matrices in real life
Appilation of matrices in real lifeAppilation of matrices in real life
Appilation of matrices in real lifeStudent
 
MATRICES
MATRICESMATRICES
MATRICESfaijmsk
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theoryChuckie Balbuena
 
Inversion of Control in MVC
Inversion of Control in MVCInversion of Control in MVC
Inversion of Control in MVCSunny Sharma
 
Real Time Data Visualization using asp.net / SignalR + D3.js
Real Time Data Visualization using asp.net / SignalR + D3.jsReal Time Data Visualization using asp.net / SignalR + D3.js
Real Time Data Visualization using asp.net / SignalR + D3.jsSunny Sharma
 
Computational Social Science, Lecture 05: Networks, Part I
Computational Social Science, Lecture 05: Networks, Part IComputational Social Science, Lecture 05: Networks, Part I
Computational Social Science, Lecture 05: Networks, Part Ijakehofman
 
Matrices And Application Of Matrices
Matrices And Application Of MatricesMatrices And Application Of Matrices
Matrices And Application Of Matricesmailrenuka
 
Fast pair-wise and node-wise algorithms for commute times and Katz scores
Fast pair-wise and node-wise algorithms for commute times and Katz scoresFast pair-wise and node-wise algorithms for commute times and Katz scores
Fast pair-wise and node-wise algorithms for commute times and Katz scoresDavid Gleich
 

En vedette (14)

Design lesson not taught in schools
Design lesson not taught in schoolsDesign lesson not taught in schools
Design lesson not taught in schools
 
Matrices in computer applications
Matrices in computer applicationsMatrices in computer applications
Matrices in computer applications
 
Profitable growth via adjacency - Guest lecture on Zook by Peter Spung
Profitable growth via adjacency - Guest lecture on Zook by Peter SpungProfitable growth via adjacency - Guest lecture on Zook by Peter Spung
Profitable growth via adjacency - Guest lecture on Zook by Peter Spung
 
Multiplication of matrices and its application in biology
Multiplication of matrices and its application in biologyMultiplication of matrices and its application in biology
Multiplication of matrices and its application in biology
 
Adjacency pair
Adjacency pairAdjacency pair
Adjacency pair
 
Appilation of matrices in real life
Appilation of matrices in real lifeAppilation of matrices in real life
Appilation of matrices in real life
 
MATRICES
MATRICESMATRICES
MATRICES
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
Inversion of Control in MVC
Inversion of Control in MVCInversion of Control in MVC
Inversion of Control in MVC
 
Real Time Data Visualization using asp.net / SignalR + D3.js
Real Time Data Visualization using asp.net / SignalR + D3.jsReal Time Data Visualization using asp.net / SignalR + D3.js
Real Time Data Visualization using asp.net / SignalR + D3.js
 
Computational Social Science, Lecture 05: Networks, Part I
Computational Social Science, Lecture 05: Networks, Part IComputational Social Science, Lecture 05: Networks, Part I
Computational Social Science, Lecture 05: Networks, Part I
 
Matrices And Application Of Matrices
Matrices And Application Of MatricesMatrices And Application Of Matrices
Matrices And Application Of Matrices
 
Fast pair-wise and node-wise algorithms for commute times and Katz scores
Fast pair-wise and node-wise algorithms for commute times and Katz scoresFast pair-wise and node-wise algorithms for commute times and Katz scores
Fast pair-wise and node-wise algorithms for commute times and Katz scores
 
Models for hierarchical data
Models for hierarchical dataModels for hierarchical data
Models for hierarchical data
 

Similaire à Catastrophic Cancellation

MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdfXxUnnathxX
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12Rabia Khalid
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxSALU18
 
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...Hsien-Hsin Sean Lee, Ph.D.
 
Lesson 24. Phantom errors
Lesson 24. Phantom errorsLesson 24. Phantom errors
Lesson 24. Phantom errorsPVS-Studio
 
Robotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docx
Robotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docxRobotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docx
Robotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docxSUBHI7
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelizationAlbert DeFusco
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statementsCtOlaf
 
Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Béo Tú
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
Floating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfFloating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfinfo235816
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptskatiarrahaman
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate CompilersFunctional Thursday
 
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...zeeshanshanzy009
 
Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3Paul Brebner
 

Similaire à Catastrophic Cancellation (20)

MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
 
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
 
Lesson 24. Phantom errors
Lesson 24. Phantom errorsLesson 24. Phantom errors
Lesson 24. Phantom errors
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Robotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docx
Robotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docxRobotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docx
Robotics.DS_Store__MACOSXRobotics._.DS_StoreRobotics.docx
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelization
 
Verilogforlab
VerilogforlabVerilogforlab
Verilogforlab
 
Operators expressions-and-statements
Operators expressions-and-statementsOperators expressions-and-statements
Operators expressions-and-statements
 
Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014
 
Switch Control and Time Delay - Keypad
Switch Control and Time Delay - KeypadSwitch Control and Time Delay - Keypad
Switch Control and Time Delay - Keypad
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
functions
functionsfunctions
functions
 
Exploiting vectorization with ISPC
Exploiting vectorization with ISPCExploiting vectorization with ISPC
Exploiting vectorization with ISPC
 
Floating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfFloating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdf
 
Comp Arithmetic Basic.ppt
Comp Arithmetic Basic.pptComp Arithmetic Basic.ppt
Comp Arithmetic Basic.ppt
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
 
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
IEEE-754 standard format to handle Floating-Point calculations in RISC-V CPUs...
 
Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3
 

Plus de C4Media

Streaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoStreaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoC4Media
 
Next Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileNext Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileC4Media
 
Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020C4Media
 
Understand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsUnderstand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsC4Media
 
Kafka Needs No Keeper
Kafka Needs No KeeperKafka Needs No Keeper
Kafka Needs No KeeperC4Media
 
High Performing Teams Act Like Owners
High Performing Teams Act Like OwnersHigh Performing Teams Act Like Owners
High Performing Teams Act Like OwnersC4Media
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaC4Media
 
Service Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideService Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideC4Media
 
Shifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDShifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDC4Media
 
CI/CD for Machine Learning
CI/CD for Machine LearningCI/CD for Machine Learning
CI/CD for Machine LearningC4Media
 
Fault Tolerance at Speed
Fault Tolerance at SpeedFault Tolerance at Speed
Fault Tolerance at SpeedC4Media
 
Architectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsArchitectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsC4Media
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsC4Media
 
Build Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerBuild Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerC4Media
 
User & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleUser & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleC4Media
 
Scaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeScaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeC4Media
 
Make Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereMake Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereC4Media
 
The Talk You've Been Await-ing For
The Talk You've Been Await-ing ForThe Talk You've Been Await-ing For
The Talk You've Been Await-ing ForC4Media
 
Future of Data Engineering
Future of Data EngineeringFuture of Data Engineering
Future of Data EngineeringC4Media
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreAutomated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreC4Media
 

Plus de C4Media (20)

Streaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live VideoStreaming a Million Likes/Second: Real-Time Interactions on Live Video
Streaming a Million Likes/Second: Real-Time Interactions on Live Video
 
Next Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy MobileNext Generation Client APIs in Envoy Mobile
Next Generation Client APIs in Envoy Mobile
 
Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020Software Teams and Teamwork Trends Report Q1 2020
Software Teams and Teamwork Trends Report Q1 2020
 
Understand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java ApplicationsUnderstand the Trade-offs Using Compilers for Java Applications
Understand the Trade-offs Using Compilers for Java Applications
 
Kafka Needs No Keeper
Kafka Needs No KeeperKafka Needs No Keeper
Kafka Needs No Keeper
 
High Performing Teams Act Like Owners
High Performing Teams Act Like OwnersHigh Performing Teams Act Like Owners
High Performing Teams Act Like Owners
 
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to JavaDoes Java Need Inline Types? What Project Valhalla Can Bring to Java
Does Java Need Inline Types? What Project Valhalla Can Bring to Java
 
Service Meshes- The Ultimate Guide
Service Meshes- The Ultimate GuideService Meshes- The Ultimate Guide
Service Meshes- The Ultimate Guide
 
Shifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CDShifting Left with Cloud Native CI/CD
Shifting Left with Cloud Native CI/CD
 
CI/CD for Machine Learning
CI/CD for Machine LearningCI/CD for Machine Learning
CI/CD for Machine Learning
 
Fault Tolerance at Speed
Fault Tolerance at SpeedFault Tolerance at Speed
Fault Tolerance at Speed
 
Architectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep SystemsArchitectures That Scale Deep - Regaining Control in Deep Systems
Architectures That Scale Deep - Regaining Control in Deep Systems
 
ML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.jsML in the Browser: Interactive Experiences with Tensorflow.js
ML in the Browser: Interactive Experiences with Tensorflow.js
 
Build Your Own WebAssembly Compiler
Build Your Own WebAssembly CompilerBuild Your Own WebAssembly Compiler
Build Your Own WebAssembly Compiler
 
User & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix ScaleUser & Device Identity for Microservices @ Netflix Scale
User & Device Identity for Microservices @ Netflix Scale
 
Scaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's EdgeScaling Patterns for Netflix's Edge
Scaling Patterns for Netflix's Edge
 
Make Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home EverywhereMake Your Electron App Feel at Home Everywhere
Make Your Electron App Feel at Home Everywhere
 
The Talk You've Been Await-ing For
The Talk You've Been Await-ing ForThe Talk You've Been Await-ing For
The Talk You've Been Await-ing For
 
Future of Data Engineering
Future of Data EngineeringFuture of Data Engineering
Future of Data Engineering
 
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and MoreAutomated Testing for Terraform, Docker, Packer, Kubernetes, and More
Automated Testing for Terraform, Docker, Packer, Kubernetes, and More
 

Dernier

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 

Dernier (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 

Catastrophic Cancellation

  • 1.
  • 2. InfoQ.com: News & Community Site • 750,000 unique visitors/month • Published in 4 languages (English, Chinese, Japanese and Brazilian Portuguese) • Post content from our QCon conferences • News 15-20 / week • Articles 3-4 / week • Presentations (videos) 12-15 / week • Interviews 2-3 / week • Books 1 / month Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations /IEEE754 http://www.infoq.com/presentati ons/nasa-big-data http://www.infoq.com/presentati ons/nasa-big-data
  • 3. Presented at QCon London www.qconlondon.com Purpose of QCon - to empower software development by facilitating the spread of knowledge and innovation Strategy - practitioner-driven conference designed for YOU: influencers of change and innovation in your teams - speakers and topics driving the evolution and innovation - connecting and catalyzing the influencers and innovators Highlights - attended by more than 12,000 delegates since 2007 - held in 9 cities worldwide
  • 4. Catastrophic cancellation: the pitfalls of floating point arithmetic (and how to avoid them!) Graham Markall Quantitative Developer, OpenGamma
  • 5. Intro/Disclaimers • Aims: • Get a rough “feel” of how floating point works • Know when to dig deeper • Cover basics, testing, and optimisation • Not an exhaustive trudge through algorithms + details • This talk: IEEE754 – mostly, but not quite ubiquitous • Mostly C/Python examples, Linux/x86_64 • No complex numbers • Code samples available!
  • 6. A problem (C) #include <values.h> float a = MAXINT; // 2147483648 float b = MAXLONG; // 9223372036854775808 float f = a + b; f == MAXLONG; // True or false? Code ex. 1
  • 7. A problem (C) #include <values.h> float a = MAXINT; // 2147483648 float b = MAXLONG; // 9223372036854775808 float f = a + b; f == MAXLONG; // True or false? // True! // IEEE754 only approximates real arithmetic Code ex. 1
  • 8. How is arithmetic on reals approximated? // float gives about 7 digits of accuracy *******---.------ MAXINT: 2147483648.000000 MAXLONG: 9223372036854775808.000000 *******------------.------ // ^ ^ // | | // Represented “Lost” beneath // unit of // least precision
  • 9. Floating point representation (1) Sign – exponent – mantissa s mantissa * 2exponent Sign bit: 0 = positive, 1 = negative Mantissa: 1.xxxxxxxxxx…
  • 10. FP representation (2) S|EEEEEEEE|1|MMMMMMMMMMMMMMMMMMMMMMM // MAXINT: S = 0, M = 1.0, E = 158 0|10011110|1|00000000000000000000000 + 1.0 * 2158-127 = 2147483648.0 A A A a Mantissa has: implied leading 1 Exponent has: bias (-127 for float)
  • 11. FP representation (2) S|EEEEEEEE|1|MMMMMMMMMMMMMMMMMMMMMMM // MAXINT: S = 0, M = 1.0, E = 158 0|10011110|1|00000000000000000000000 + 1.0 * 2158-127 = 2147483648.0 // MAXLONG: S = 0, M = 1.0, E = 190 0|10111110|1|00000000000000000000000 + 1.0 * 2190-127 = 9223372036854775808.0 Mantissa has: implied leading 1 Exponent has: bias (-127 for float)
  • 12. MAXLONG smallest increment // MAXLONG: S = 0, M = 1.0, E = 190 0|10111110|1|00000000000000000000000 + 1.0 * 2190-127 = 9223372036854775808.0 0|10111110|1|00000000000000000000001 + 1.0000001192092896 * 2190-127 = 9223373136366403584.0 Code ex. 2
  • 13. On the number line MAXLONG (9223372036854775808.0) MAXLONG_NEXT (9223373136366403584.0) MAXLONG + MAXINT (0.19% towards MAXLONG_NEXT)
  • 14. Precision and range summary • Precision: Mantissa length • Range: Exponent length • Float, 4 bytes: 23 bit mantissa, 8 bit exponent Precision: ~7.2 digits Range: 1.17549e-38, 3.40282e+38 • Double, 8 bytes: 52 bit mantissa, 11 bit exponent Precision: ~15.9 digits Range: 2.22507e-308, 1.79769e+308 Code ex. 3
  • 15. Special cases When is a number not a number?
  • 16. Floating point closed arithmetic • Integer arithmetic: • 1/0 // Arithmetic exception • Floating point arithmetic is closed: • Domain (double): • 2.22507e-308 <-> 1.79769e+308 • 4.94066e-324 <-> just beneath 2.22507e-308 • +0, -0 • Inf • NaN • Exceptions are exceptional – traps are exceptions Code ex. 4, 5
  • 17. A few exceptional values 1/0 = Inf // Limit -1/0 = -Inf // Limit 0/0 = NaN // 0/x = 0, x/0 = Inf Inf/Inf = NaN // Magnitudes unknown Inf + (-Inf) = NaN // Magnitudes unknown 0 * Inf = NaN // 0*x = 0, Inf*x = Inf sqrt(x), x<0 = NaN // No complex Code ex. 6
  • 18. Consequences // Inf, NaN propagation: double n = 1000.0; for(double i = 0.0; i < 100.0; i += 1.0) n = n / i; printf(“%f”, n); // “Inf” Code ex. 7
  • 19. Trapping exceptions (Linux, GNU) • feenableexcept(int __excepts) • FE_INXACT – Inexact result • FE_DIVBYZERO - Division by zero • FE_UNDERFLOW - Underflow • FE_OVERFLOW - Overflow • FE_INVALID - Invalid operand • SIGFPE Not exclusive to floating point: • int i = 0; int j = 1; j/i // Receives SIGFPE! Code ex. 8-12
  • 20. Back in the normal range Some exceptional inputs to some math library functions result in normal-range results: x = tanh(Inf) // x is 1.0 y = atan(Inf) // y is pi/2 (ISO C / IEEE Std 1003.1-2001) Code ex. 13
  • 21. Denormals • x – y == 0 implies x == y ? • Without denormals, this is not true: • X = 2.2250738585072014e-308 • Y = 2.2250738585072019e-308 // (5e-324) • Y – X = 0 • With denormals: • 4.9406564584124654e-324 • Denormal implementation e = 0: • Implied leading 1 is not a 1 anymore • Performance: revisited later Code ex. 14
  • 23. Assumptions • Code that does floating-point computation • Needs tests to ensure: • Correct results • Handling of exceptional cases • A function to compare floating point numbers is required
  • 24. Exact equality (danger) def equal_exact(a, b): return a == b equal_exact(1.0+2.0, 3.0) # True equal_exact(2.0, sqrt(2.0)**2.0) # False sqrt(2.0)**2 # 2.0000000000000004 Code ex. 15
  • 25. Absolute tolerance def equal_abs(a, b, eps=1.0e-7): return fabs(a - b) < eps equal_abs(1.0+2.0, 3.0) # True equal_abs(2.0, sqrt(2.0)**2.0) # True Code ex. 16
  • 26. Absolute tolerance eps choice equal_abs(2.0, sqrt(2)**2, 1.0e-16) # False equal_abs(1.0e-8, 2.0e-8) # True! Code ex. 17
  • 27. Relative tolerance def equal_rel(a, b, eps=1.0e-7): m = min(fabs(a), fabs(b)) return (fabs(a – b) / m) < eps equal_rel(1.0+2.0, 3.0) # True equal_rel(2.0, sqrt(2.0)**2.0) # True equal_rel(1.0e-8, 2.0e-8) # False Code ex. 18
  • 28. Relative tolerance correct digits eps Correct digits 1.0e-1 ~1 1.0e-2 ~2 1.0e-3 ~3 … 1.0e-16 ~16 Code ex. 19
  • 29. Relative tolerance near zero equal_rel(1.0e-50, 0) ZeroDivisionError: float division by zero Code ex. 20
  • 30. Summary guidelines: When to use: • Exact equality: Never • Absolute tolerance: Expected ~ 0.0 • Relative tolerance: Elsewhere • Tolerance choice: • No universal “correct” tolerance • Implementation/application specific • Appropriate range: application specific
  • 31. Checking special cases -0 == 0 // True Inf == Inf // True -Inf == -Inf // True NaN == NaN // False Inf == NaN // False NaN < 1.0 // False NaN > 1.0 // False NaN == 1.0 // False isnan(NaN) // True Code ex. 21
  • 33. Division vs Reciprocal multiply // Slower (generally) a = x/y; // Divide instruction // Faster (generally) y1 = 1.0/y; // x86: RCPSS instruction a = x*y1; // Multiply instruction // May lose precision. // GCC: -freciprocal-math Code ex. 22
  • 34. Non-associativity float a = 1.0e23; float b = -1.0e23; float c = 1.0; printf("(a + b) + c = %fn", (a + b) + c); printf("a + (b + c) = %fn", a + (b + c)); (a + b) + c = 1.000000 a + (b + c) = 0.000000 Code ex. 23
  • 35. Non-associativity (2) • Re-ordering is “unsafe” • Turned off in compilers by default • Enable (gcc): -fassociative-math • Turns on –fno-trapping, also –fno- signed-zeros (may affect -0 == 0, flip sign of -0*x)
  • 36. Finite math only • Assume that no Infs or NaNs are ever produced. • Saves execution time: no code for checking/dealing with them need be generated. • GCC: -ffinite-math-only • Any code that uses an Inf or NaN value will probably behave incorrectly • This can affect your tests! Inf == Inf may not be true anymore.
  • 37. -ffast-math • Turns on all the optimisations we’ve just discussed. • Also sets flush-to-zero/denormals-are-zero • Avoids overhead of dealing with denormals • x – y == 0 -> x == y may not hold • For well-tested code: • Turn on –ffast-math • Do tests pass? • If not, break into individual flags and test again.
  • 38. -ffast-math linkage • Also causes non-standard code to be linked in and called • e.g. crtfastmath.c set_fast_math() • This can cause havoc when linking with other code. • E.g. Java requires option to deal with this: • -XX:RestoreMXCSROnJNICalls
  • 39. Summary guidelines • Refactoring and reordering of floating point can increase performance • Can also be unsafe • Some transformations can be enabled by compiler • Manual implementation also possible • Make sure code well-tested • Be prepared for trouble!
  • 41. Floating point • Finite approximation to real arithmetic • Some “corner” cases: • Denormals, +/- 0 • Inf, NaN • Testing requires appropriate choice of: • Comparison algorithm • Expected tolerance and range • Optimisation: • For well-tested code • Reciprocal, associativity, disable “edge case” handling • FP can be a useful approximation to real arithmetic
  • 43. Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations/IEEE754