Floating point Numbers

Similar documents
Descriptive Statistics (And a little bit on rounding and significant digits)

Please bring the task to your first physics lesson and hand it to the teacher.

Number Systems III MA1S1. Tristan McLoughlin. December 4, 2013

2 Systems of Linear Equations

( )( b + c) = ab + ac, but it can also be ( )( a) = ba + ca. Let s use the distributive property on a couple of

15 Skepticism of quantum computing

Finite Mathematics : A Business Approach

Modern Algebra Prof. Manindra Agrawal Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

Algebra. Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Quadratic Equations Part I

Hypothesis testing I. - In particular, we are talking about statistical hypotheses. [get everyone s finger length!] n =

- measures the center of our distribution. In the case of a sample, it s given by: y i. y = where n = sample size.

ASTRO 114 Lecture Okay. We re now gonna continue discussing and conclude discussing the entire

The Gram-Schmidt Process

1 Normal Distribution.

You separate binary numbers into columns in a similar fashion. 2 5 = 32

Lies My Calculator and Computer Told Me

ter. on Can we get a still better result? Yes, by making the rectangles still smaller. As we make the rectangles smaller and smaller, the

Q: How can quantum computers break ecryption?

Chapter 3. Estimation of p. 3.1 Point and Interval Estimates of p

Discrete Structures Proofwriting Checklist

The force of gravity holds us on Earth and helps objects in space stay

CH 66 COMPLETE FACTORING

Math 31 Lesson Plan. Day 2: Sets; Binary Operations. Elizabeth Gillaspy. September 23, 2011

What Every Programmer Should Know About Floating-Point Arithmetic DRAFT. Last updated: November 3, Abstract

AN ALGEBRA PRIMER WITH A VIEW TOWARD CURVES OVER FINITE FIELDS

Study skills for mathematicians

TheFourierTransformAndItsApplications-Lecture28

EQ: How do I convert between standard form and scientific notation?

Binary floating point

Week 2: Defining Computation

Chapter 1 Review of Equations and Inequalities

base 2 4 The EXPONENT tells you how many times to write the base as a factor. Evaluate the following expressions in standard notation.

CHAPTER 9 THE ARROW OF TIME

5 + 9(10) + 3(100) + 0(1000) + 2(10000) =

Solving Equations. Lesson Fifteen. Aims. Context. The aim of this lesson is to enable you to: solve linear equations

Appendix C: Accuracy, Precision, and Uncertainty

CSE 331 Winter 2018 Reasoning About Code I

Joint Probability Distributions and Random Samples (Devore Chapter Five)

Final Review Sheet. B = (1, 1 + 3x, 1 + x 2 ) then 2 + 3x + 6x 2

Astronomy 102 Math Review

Regression, part II. I. What does it all mean? A) Notice that so far all we ve done is math.

ACCESS TO SCIENCE, ENGINEERING AND AGRICULTURE: MATHEMATICS 1 MATH00030 SEMESTER /2018

Part I Electrostatics. 1: Charge and Coulomb s Law July 6, 2008

Calculus II. Calculus II tends to be a very difficult course for many students. There are many reasons for this.

2. Limits at Infinity

The Basics COPYRIGHTED MATERIAL. chapter. Algebra is a very logical way to solve

Chapter 2. Mathematical Reasoning. 2.1 Mathematical Models

Instructor (Brad Osgood)

THE SIMPLE PROOF OF GOLDBACH'S CONJECTURE. by Miles Mathis

ACCESS TO SCIENCE, ENGINEERING AND AGRICULTURE: MATHEMATICS 1 MATH00030 SEMESTER / Functions

1.20 Formulas, Equations, Expressions and Identities

Linear Programming and its Extensions Prof. Prabha Shrama Department of Mathematics and Statistics Indian Institute of Technology, Kanpur

Deep Algebra Projects: Algebra 1 / Algebra 2 Go with the Flow

Introduction to Algebra: The First Week

Uncertainty. Michael Peters December 27, 2013

Lecture 4: Constructing the Integers, Rationals and Reals

Coulomb s Law. 1 Equipment. 2 Introduction

Alex s Guide to Word Problems and Linear Equations Following Glencoe Algebra 1

PEANO AXIOMS FOR THE NATURAL NUMBERS AND PROOFS BY INDUCTION. The Peano axioms

Natural Language Processing Prof. Pawan Goyal Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

But, there is always a certain amount of mystery that hangs around it. People scratch their heads and can't figure

BRIDGE CIRCUITS EXPERIMENT 5: DC AND AC BRIDGE CIRCUITS 10/2/13

CS1800: Sequences & Sums. Professor Kevin Gold

Systematic Uncertainty Max Bean John Jay College of Criminal Justice, Physics Program

of 8 28/11/ :25

Gravity Well Demo - 1 of 9. Gravity Well Demo

ASTRO 114 Lecture Okay. What we re going to discuss today are what we call radiation laws. We ve

Slope Fields: Graphing Solutions Without the Solutions

Introductory Quantum Chemistry Prof. K. L. Sebastian Department of Inorganic and Physical Chemistry Indian Institute of Science, Bangalore

Quantum Entanglement. Chapter Introduction. 8.2 Entangled Two-Particle States

9.4 Radical Expressions

2. FUNCTIONS AND ALGEBRA

Ordinary Differential Equations Prof. A. K. Nandakumaran Department of Mathematics Indian Institute of Science Bangalore

Knots, Coloring and Applications

Liquid-in-glass thermometer

Circuits for Analog System Design Prof. Gunashekaran M K Center for Electronics Design and Technology Indian Institute of Science, Bangalore

ABE Math Review Package

Addition & Subtraction of Polynomials

Real Analysis Prof. S.H. Kulkarni Department of Mathematics Indian Institute of Technology, Madras. Lecture - 13 Conditional Convergence

Properties of Sequences

COLLEGE ALGEBRA. Paul Dawkins

DIFFERENTIAL EQUATIONS

Appendix B: Accuracy, Precision and Uncertainty

#29: Logarithm review May 16, 2009

Understanding The Law of Attraction

Getting Started with Communications Engineering

Basics of Proofs. 1 The Basics. 2 Proof Strategies. 2.1 Understand What s Going On

(Refer Time Slide: 1:35)

CHAPTER 10 Zeros of Functions

Solving with Absolute Value

Figure 1: Doing work on a block by pushing it across the floor.

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

Sums of Squares (FNS 195-S) Fall 2014

CONSTRUCTION OF THE REAL NUMBERS.

Introducing Proof 1. hsn.uk.net. Contents

Math Lecture 4 Limit Laws

- a value calculated or derived from the data.

Atomic Theory. Introducing the Atomic Theory:

Chapter 5 Simplifying Formulas and Solving Equations

Transcription:

Floating point Numbers William Gould President StataCorp LP July 2014, Boston W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 1 / 62

Apologia No Report to Users this year I have something more interesting. I think you ll find it useful. The Report to Users will return next year. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 2 / 62

Instead... Instead, I m going to talk about... Floating Point Numbers which I subtitle A Visit Through The Looking Glass W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 3 / 62

Motivation Motivation Computers implement Floating Point (FP) numbers. FP numbers are similar to, but different from, Real numbers. This is not unlike the Nutrimatic drinks dispenser, The Nutrimatic drinks dispenser produces a concoction that is almost, but not quite, entirely unlike tea. Hitchhiker s Guide to the Galaxy W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 4 / 62

Motivation Motivation Most researchers (not you) use FP numbers as if they were Real numbers. Most researchers (not you) have little understanding of FP numbers. Many (not you) couldn t even tell us one way FP and Real numbers differ, much less the most important way. The purpose of this talk is to... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 5 / 62

Purpose Purpose Without jargon, Without even mentioning the word binary, But using the language with which you are familiar, Develop your understanding of FP numbers, and Develop your intuition about FP numbers so that you can predict when calculations might go awry, and know how to think about the problem, and determine how to fix it. (Warning: most talks do not live up to the author s billing.) W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 6 / 62

Introduction 1 Introduction FP stands for Floating Point. FP numbers are one of a variety of methods that could have been chosen by computer engineers to represent real numbers. FP numbers are strange...... and that strangeness has nothing to do with computers being binary. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 7 / 62

No real numbers Computers don t implement real numbers Computers don t implement real numbers...... computers simulate them As with all simulations Some features are preserved..... and others are discarded Among the discarded features is the defining characteristic of real numbers... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 8 / 62

No real numbers Missing is the defining characteristic of real numbers Between any two real numbers, there is another real number. That s not true for FP numbers. Worse, the number of FPs varies between any two FP numbers that are equally far apart! That s what makes FP numbers strange. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 9 / 62

The FP number line The FP (simulated Real) Number Line The number line looks like this: x x x x xxxxx x x x x x 0 FP numbers important properties are They have finite precision. Only the points shown exist. The distance between points increases as you move away from zero. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 10 / 62

The FP number line x x x x xxxxx x x x x x 0 This number line is not multiplicative or exponential. It is linear, punctuated by exponential jumps. Distance between Range adjacent numbers [1, 10) ǫ [10, 10 2 ) 10ǫ [10 2,10 3 ) 10 2 ǫ [10 1,1) 10 1 ǫ [10 2,10 1 ) 10 2 ǫ W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 11 / 62

The FP number line Let s imagine what the universe would be like if it were based on FP numbers... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 12 / 62

The FP Universe Mark I If the Universe were based on floating point numbers...... it would have an identifiable center! Y X W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 13 / 62

The FP Universe The FP Universe has its highest resolution at its center. My pictures don t show a scale. Does the decreasing resolution matter? Let s measure resolution as the minimum distance that two objects can be separated, assuming the separation is greater than 0. If the Universe had a resolution of Planck s distance (1.616199e-35 m) at the center (Earth, of course), then... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 14 / 62

The FP Universe If the Universe had a resolution of Planck s distance (1.616199e-35 m) at the center (Earth), then the resolution on the Moon and Sun would be... Decimal digits of Resolution Resolution accuracy of Universe at Moon at Sun 1 161,620 km 16,161,990 km 2 16,162 km 1,616,199 km 7 162 m 16,162 m 8 16 m 1,616 m 16 1.616e-07 m 1.616e-05 m W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 15 / 62

The FP Universe The FP Universe behaves oddly as objects move Simply moving an object outward from the center would cause the object to collapse in on itself, or cause the object to grow in size, or perhaps we d have a new uncertainty principle and it would do a little of both. Which depends on what happens to a particle that occupies a position that disappears. Does the particle move inward or outward? Or does it vanish? That would violate conservation laws and produce a very different universe than the one we observe. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 16 / 62

The FP Universe The Best Answer is The Universe simply wouldn t work if it were implemented in FP numbers. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 17 / 62

Taking stock Taking Stock Where are we? Computer engineers created FP numbers as a substitute for real numbers. We use computers to perform real-world simulations, simulations based on the Real Universe. FP numbers cannot simulate the real universe. Houston, we have a problem. Or we might have. It will turn out that we do have a problem. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 18 / 62

Taking stock Why FP Numbers? Computer engineers chose FP numbers because The other alternatives are worse, FP numbers have some useful properties, and the bad properties can be avoided if you are just a little cautious. So why didn t anybody ever tell you about this? W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 19 / 62

FP Numbers FP Numbers What s an FP Number? Real numbers are in no sense natural for digital computers. Instead, we simulate real numbers using facilities that are natural for digital computers. The simulation is called FP. Integers are natural for computers. So we must implement FP numbers using integers only! Here s how... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 20 / 62

Simulating Real Numbers Simulating Real Numbers Consider real number z. We will write it z = a 10 b where a and b are (finite precision) integers. An FP number is the integer pair (a,b). Real computers use z = a 2 b, but it will be easier to understand FPs if we use base 10. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 21 / 62

Implementing FP Numbers Implementing FP Numbers An FP number is the integer pair (a,b) interpreted to mean z = a 10 b We set limits for a and b, 9999 a 9999 99 b 99 and we agree on a convention (implied decimal point), (a,b) means z = (a/1000) 10 b which will make the slides easier to read. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 22 / 62

Examples of FP Numbers Examples of FP Numbers (1234,0) = 1.234 (1234,1) = 12.34 (1234, 1) = 0.1234 ( 1234, 0) = 1.234 ( 1234, 1) = 12.34 ( 1234, 1) = 0.1234 (FP integer pair on the left; our interpretation of it on the right.) W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 23 / 62

Normalization of FP Numbers Normalization of FP Numbers There are three ways we could write 1.2: (1200, 0) = 1.2 (0120, 1) = 1.2 (0012, 2) = 1.2 If a 1000 we say the FP number is normalized. If a < 1000 we say the FP number is denormalized. Numbers are stored in normalized form. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 24 / 62

Adding of FP Numbers Adding FP Numbers Let s add 2.0 + 3.0: 2.0 = (2000, 0) + 3.0 = (3000, 0) ------------------ (5000, 0) = 5.0 What made that possible is that the powers were equal. We just added as integers 2000 + 3000 to obtain integer 5000. The result is (5000,0). We humans interpret (5000,0) as meaning 5.000 10 0 = 5.000. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 25 / 62

Adding of FP Numbers We are still adding numbers with equal powers Just as easily, we can add 2.981 + 3.425: 2.981 = (2981, 0) + 3.425 = (3422, 0) -------------------- (6403, 0) = 6.403 We just added as integers 2981 + 3422 to obtain integer 6403. The result is (6403,0). We humans interpret (6403,0) as meaning 6.403. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 26 / 62

Adding of FP Numbers We are still adding numbers with equal powers Try adding 2.981 + 8.425, for which the answer is 11.406: 2.981 = (2981, 0) + 8.425 = (8425, 0) -------------------- (?406, 0) <- How can we squeeze 11 into one slot? Adding integers 2981 + 8425 results in 11406. We can t fit a 5-digit result in a 4-digit accumulator. Obviously, we need an extra digit in our accumulator. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 27 / 62

Adding of FP Numbers We need an extra digit in our accumulator... So assume it: 2.981 = 2981, 0) + 8.425 = 3422, 0) -------------------- (11406, 0) -> (1140, 1) / \ accumulator has extra digit The true answer is 11.406, so we lost a digit of accuracy. we shift away the extra digit (in this case, w/o rounding) (we could have rounded) We lost accuracy when we normalized the result (removed the extra digit). A one-digit loss is the worst case. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 28 / 62

Adding FP Numbers of Different Powers Adding FP Numbers of Different Powers We now know how to add (a 1,b 1 ) + (a 2,b 2 ) when b 1 = b 2. How do we add when b 1 b 2? Answer: We denormalize the number with the smaller power so that the powers are equal. Then we proceed just as we did previously. Let s add 10.0 + 2.0: powers now equal 10.0 = (1000, 1) -> (1000, 1) + 2.0 = (2000, 0) -> (0200, 1) <- (we shifted right) ---------------------------------- (1200, 1) = 12.00 W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 29 / 62

Adding FP Numbers of Different Powers Adding FP Numbers of Different Powers Let s add 10 + 2.005: powers now equal 10.00 = (1000, 1) -> (1000, 1) + 2.005 = (2005, 0) -> (0200, 1) <- (we shifted right) ---------------------------------- (1200, 1) = 12.00 When we shift right to denormalize, we lose digits. Requested to add 10 + 2.005, the computer added 10 + 2.00, and thus it obtained 12.00, not 12.005. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 30 / 62

Adding FP Numbers of Different Powers Adding FP Numbers of Different Powers Just one more example. Let s add 10 + 0.0005: 10.0 = (1000, 1) -> (1000, 1) + 0.005 = (5000,-3) -> (0000, 1) <- (we lose all digits!) ---------------------------------- (1000, 1) = 10.0 When we shift right, we lose digits. In this case, we lost all the digits. 0.005 became 0. And thus, 10 + 0.0005 = 10 W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 31 / 62

Adding FP Numbers: Summary Adding FP Numbers: Summary In calculating (a 1,b 1 ) + (a 2,b 2 ), the maximum digit loss is b 1 b 2 + 1 On binary computers, the same applies, except we lose binary digits. Binary digits contain less information than decimal digits, but we lose more of them. (It averages out.) Addition is to be feared if b 1 b 2 0 W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 32 / 62

Adding FP Numbers: Examples Adding FP Numbers: Example 1 You have an approximation formula S = C(1) + C(2) + C(3) +... where C( ) > 0 and C(i) > C(i + 1) for all i. Do not code, real scalar i, result result = 0 for (i=1; i<=10; i++) result = result + C(i) Do not add the terms from left to right,... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 33 / 62

Adding FP Numbers: Examples Adding FP Numbers: Example 1 We are coding the approximation formula S = C(1) + C(2) + C(3) +... where C( ) > 0 and C(i) > C(i + 1) for all i. Do code, real scalar i, result real vector term result = 0 term = J(1, 10,.) for (i=1; i<=10; i++) term[i] = C(i) for (i=10; i<=1; --i) result = result + term[i] That is, add terms from right to left. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 34 / 62

Adding FP Numbers: Examples Adding FP Numbers: Example 2 Imagine a large dataset with the property It is possible that x[1] x[2] + x[3] + + X[N] x[1] + x[2] + x[3] + + x[n] = x[1] If you are worried about this, sort before adding or use Stata s quad-precision facilities. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 35 / 62

Subtracting FP Numbers Subtracting FP Numbers At this point, I don t need to lead you through as many examples. Let s calculate 10.0 0.5008, 10.0 = (1000, 1) -> (1000, 1) - 0.5 = (5008,-1) -> (0050, 1) <- (shift) ---------------------------------- (0950, 1) = (9500, 0) = 9.5 In a mechanical sense, subtraction is just like addition. Except... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 36 / 62

Subtracting FP Numbers Subtraction is just like addition, except Whereas adding nearly equal quantities is whoppingly accurate, Subtracting nearly equal quantities can be whoppingly inaccurate. Let me show you... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 37 / 62

Subtracting FP Numbers Let s calculate 10.01 + 10.00 and calculate 10.01 10.00: 10.01 = (1001, 1) + 10.00 = (1000, 1) ------------------- (2001, 1) = 20.01 (4 digits of accuracy) 10.01 = (1001, 1) - 10.00 = (1000, 1) ------------------- (0001, 1) = (1000, 3) (1 digit of accuracy) --- --- \ these digits were made up! this is a more honest answer we believe every one of these digits W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 38 / 62

Subtracting FP Numbers Subtraction is always scary Consider (a 1,b 1 ) (a 2,b 2 ), Either b 1 b 2, in which case we lose digits Or b 1 = b 2, in which case we make up digits Footnote: When I say subtraction, I mean subtraction of like-signed values. Subtraction of oppositely signed values is addition. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 39 / 62

Subtracting FP Numbers Think about addition and subtraction like this: When adding z 1 + z 2, we do not have to believe that z 1 and z 2 are perfectly accurately recorded. Say z 1 = (1001,1) and z 2 = (1000,1). We do not have to believe that z 1 = 10.01000 and z 2 = 10.00000. Whatever we believe, we obtain z 1 + z 2 to advertised accuracy. With subtraction, however... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 40 / 62

Subtracting FP Numbers Think about addition and subtraction like this: When subtracting z 1 z 2, if z 1 z 2, we must believe that z 1 and z 2 are perfectly accurately recorded. Say z 1 = (1001,1) and z 2 = (1000,1). We must believe that z 1 = 10.01000 and z 2 = 10.00000 even though all that s recorded is z 1 = 10.01 and z 2 = 10.00. That is, we must assume enough zeros to justify the made up digits! Or, if z 1 and z 2 are of different magnitudes, we run into the standard inaccuracies. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 41 / 62

Subtracting FP Numbers For subtraction, nothing beats having extra digits It is because of subtraction that modern computers carry extra digits in their FP numbers. If you would promise never to perform a single subtraction, we wouldn t need them. You cannot make that promise, and so we need extra digits in ALL the operators. This way, when you subtract two numbers that are nearly equal, the result contains at least some digits that are accurate. You use double precision in order to obtain single-precision accuracy! W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 42 / 62

Subtracting FP Numbers Sear this onto your brain When you subtract numbers that are roughly equal, double precision returns roughly single-precision accuracy if you are lucky. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 43 / 62

Subtracting FP Numbers: Examples Subtracting FP Numbers: Example: Reverse cumulatives Comparison of 1 F(z) and G(z) = F( z) F( ) cumulative normal z # of digits in common 1 16 3 13 5 9 7 5 8 1 9 0 Regardless of distribution, never calculate 1 F(). Calculate G(). (Formulas vary.) W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 44 / 62

Subtracting FP Numbers: Examples Inverse Mills Ratio lambda 0 2 4 6 8 10 0 2 4 6 8 10 z lambda based on F( z) lambda based on 1 F(z) W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 45 / 62

Subtracting FP Numbers: Examples Subtracting FP Numbers: Example 2: Probabilities The above was not just a cute example. For 0 p 1, The simulated real numbers that computers provide simply cannot calculate 1 p accurately. Restate your formulas so that they do not involve 1 p, or restrict the range to p 0.99997. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 46 / 62

Subtracting FP Numbers: Examples Subtracting FP Numbers: Cox Survival Model Cox concerns risk pools. Subjects enter the risk pools, and leave them. We have lots of things we must calculate about the current risk pool. When subjects enter, we add values to the risk pools. When subjects leave, we subtract those values back out. Run the whole process from start to finish, and the values ought to be zero. They are not. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 47 / 62

Subtracting FP Numbers: Examples We are adding to and subtracting from risk pools, We evaluated lots of clever normalization and formulae for improving the accuracy of this add-and-subtract process. They improved accuracy, but not enough. Problem is, despite all of the double precision code, because of the subtractions, we are carrying only single-precision accuracy. Concerning subtraction, nothing beats adding more FP digits. We went to quad precision (and less sophisticated code). Accuracy improved. Run times improved. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 48 / 62

Subtracting FP Numbers: Examples Cox Model: Resolution We start the problem using double-precision code, yielding single-precision accuracy. Drive that to convergence. Then we start again, using quad-precision code yielding double-precision accuracy. Drive that to convergence. We have a fast and accurate result. If you have an accuracy problem due to subtraction,...... consider quad precision. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 49 / 62

Subtracting FP Numbers: Examples The one case when subtraction is safe Subtraction x c is perfectly safe when x is data and not the result of calculation on data, c is anything, data or the result of (imperfect) calculation, x c is closer to the center ( x c < x ). As a programmer, I take the data as given. Thus I assume x = (1001,2) = 10.01 really means x = 10.01000... That s how I get accurate results conditioned on the data. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 50 / 62

Subtracting FP Numbers: Examples The one case when subtraction is safe Subtraction to move data toward the center is not only safe, it s a good idea The FP number line is denser at the center. And because the points are denser, subsequent additions and subtractions performed on x i c produce more accurate results than those performed on x.. summarize x. generate centered_x = x - r(mean) \ not just safe, but recommended W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 51 / 62

Subtracting FP Numbers: Examples The FP number line is denser at the center Move in and lose nothing. You can move back out without loss.. generate x = 5000.01. generate closer = x - 5000. generate original = closer + 5000. display x-original 0 W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 52 / 62

Subtracting FP Numbers: Examples The FP number line is denser at the center Move out and lose precision. Move back in and the loss is permanent.. generate x = 5000.01. generate farther = x + 5000. generate original = farther - 5000. display x-original -.00390625 W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 53 / 62

Multiplication and Division Multiplication and Division I wish I had time to tell you about multiplication and division, It s interesting, but it s okay to skip it, because everything I would say is reassuring. Multiplication and division lose at most 1/2 digit. FP was designed for multiplication and division. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 54 / 62

Summary of operators Summary of Operators Addition is a red flag. Subtraction is a red flag with flashing lights. FP numbers produce a difference that is almost, but not quite, entirely unlike subtraction. Multiplication and Division are always accurate. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 55 / 62

What this means for Statistical Applications What this means for Statistical Applications 1. Subtraction is scary. Be scared. 2. But don t ignore addition. Statistics is about data reduction. Statisticians favorite data-reduction operator is addition. We sum data and then divide to get a mean. We sum likelihood contributions to fit models. The list goes on and on. And when addition is used, it s used a lot. Addition is major contributor to accuracy problems in statistical calculations. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 56 / 62

What this means for Statistical Applications The best way to sum When you have to calculate S = n i=1 x i and you re worried about precision, Use Mata s quad-precision routines Quad precision works best in my experience. There s a clever, double-precision alternative. I ll show you... W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 57 / 62

What this means for Statistical Applications To calculate S = n i=1 x i, use m i = m i 1 i 1 i + x i i Start it off the m 1 = x 1. Then S = m n n. Code it like this, real vector x real scalar S, m, i,n n = length(m) m = x[1] for (i=2; i<=n; i++) m = m*((i-1)/i) + x[i]/i S = m*n W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 58 / 62

Final Advice Final Advice When using programs where great care has not been exercised in numerically making the calculations when when using your own programs or those written by other users, Scale your data so that variables are roughly between 0 and 1000 (or 1000 and 1000). Scaled data considerably reduces computational stress. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 59 / 62

Final Advice Final Advice Even though multiplication is safe, if you use Mata, remember All matrix operators are problematic, even multiplication. Matrix multiplication involves scalar multiplication and scalar addition and, if any of the matrix elements are negative, scalar subtraction, too. Matrix routines, coded without thought and used on unscaled data, are an excellent way of quickly producing inaccurate results. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 60 / 62

Conclusion Conclusion The problem is FP numbers mostly work like Real numbers. That makes everyone complacent; even us at StataCorp. And then they bite because, sometimes, FP numbers are not at all like Real numbers. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 61 / 62

Conclusion Conclusion I wish I had more time I could show you lots more examples I could reassure you about many calculations I could show you workarounds for others but now you know how to think about the issues Thank you for you attention. W. Gould (StataCorp) Floating point Numbers: A visit through the looking glass July 2014 62 / 62