Floating Point. CSC207 Fall 2017

Similar documents
3.1 DATA REPRESENTATION (PART C)

CPS 101 Introduction to Computational Science

Floating Point. CENG331 - Computer Organization. Instructors: Murat Manguoglu (Section 1)

Scientific Computing. Error Analysis

Representing and Manipulating Floating Points. Jo, Heeseung

Representing and Manipulating Floating Points

Representing and Manipulating Floating Points

CS429: Computer Organization and Architecture

ROUNDING ERRORS LAB 1. OBJECTIVE 2. INTRODUCTION

Representing and Manipulating Floating Points

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Floating Point Arithmetic

fractional quantities are typically represented in computers using floating point format this approach is very much similar to scientific notation

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Giving credit where credit is due

Numerical Methods for Eng [ENGR 391] [Lyes KADEM 2007]

Giving credit where credit is due

Data Representation Floating Point

Floating-point Error

Data Representation Floating Point

Variables and Data Representation

18-642: Floating Point Blues

Floating Point January 24, 2008

Numerical computing. How computers store real numbers and the problems that result

What Every Programmer Should Know About Floating-Point Arithmetic

CS61C : Machine Structures

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

Divide: Paper & Pencil

Up next. Midterm. Today s lecture. To follow

Natural Numbers and Integers. Big Ideas in Numerical Methods. Overflow. Real Numbers 29/07/2011. Taking some ideas from NM course a little further

3.5 Floating Point: Overview

Characters, Strings, and Floats

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Floating Point Numbers. Lecture 9 CAP

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

Floating-Point Arithmetic

Foundations of Computer Systems

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

System Programming CISC 360. Floating Point September 16, 2008

Floating Point Numbers

Module 2: Computer Arithmetic

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

CS61C : Machine Structures

Floating-Point Arithmetic

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

Number Systems and Computer Arithmetic

95% of the folks out there are completely clueless about floating-point.

UCB CS61C : Machine Structures

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Computer Organization: A Programmer's Perspective

ME 261: Numerical Analysis. ME 261: Numerical Analysis

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

COMP2611: Computer Organization. Data Representation

The Perils of Floating Point

UC Berkeley CS61C : Machine Structures

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

Floating Point Numbers. Luca Heltai

Computational Methods. Sources of Errors

CS61C : Machine Structures


95% of the folks out there are completely clueless about floating-point.

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Floating-Point Arithmetic

Arithmetic for Computers. Hwansoo Han

ecture 25 Floating Point Friedland and Weaver Computer Science 61C Spring 2017 March 17th, 2017

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

Floating Point Representation. CS Summer 2008 Jonathan Kaldor

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Ints and Floating Point

02 - Numerical Representations

±M R ±E, S M CHARACTERISTIC MANTISSA 1 k j

CS61C : Machine Structures

CS321 Introduction To Numerical Methods

Floating Point Arithmetic

Learning from Math Library Testng for C Marcel Beemster Solid Sands

Number Representations

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

EE 109 Unit 19. IEEE 754 Floating Point Representation Floating Point Arithmetic

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Precision and Accuracy

CS 61C: Great Ideas in Computer Architecture Floating Point Arithmetic

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Floating Point Numbers

Floating Point Numbers

Data Representation 1

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Number Systems. Both numbers are positive

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Numerical Precision. Or, why my numbers aren t numbering right. 1 of 15

Chapter 2. Data Representation in Computer Systems

CMPSCI 145 MIDTERM #2 Solution Key SPRING 2018 April 13, 2018 Professor William T. Verts

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Transcription:

Floating Point CSC207 Fall 2017

Ariane 5 Rocket Launch

Ariane 5 rocket explosion In 1996, the European Space Agency s Ariane 5 rocket exploded 40 seconds after launch. During conversion of a 64-bit to a 16-bit format, overflow occurred: the number was too big to store in 16 bits. This hadn t been expected because the data (acceleration reported by sensors) had never been this large before. But this new rocket was faster than its predecessor. $7 billion of R&D had been invested in this rocket. Reference: http://www.around.com/ariane.html

Example 1 Perform some simple arithmetic, and check that the laws of mathematics hold. Code: Adding.java

Is Java broken? It s not only Java. Check this out in Python: >>> x = 0.1 >>> sum = x + x + x >>> sum == 0.3 False >>> sum 0.30000000000000004 >>> bigger = 1.0 >>> s = 1.0e-6 >>> sum1 = s + s + s + s + s + s + s + s + s + s + bigger >>> sum2 = bigger + s + s + s + s + s + s + s + s + s + s >>> sum1 == sum2 False >>> sum1 1.00001 >>> sum2 1.0000099999999992

Representing numbers It all makes sense if you understand how real numbers are represented. First, consider an int like 42. Hardware doesn t directly represent 4s or 2s everything is binary. 42 = 1 x 2 5 + 0 x 2 4 + 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 0 x 2 0 So 42 can be represented by 101010 (base 2).

Representing fractions Fractions can be handled using the same approach. Example: 0.4375 = 0 x 2-1 + 1 x 2-2 + 1 x 2-3 + 1 x 2-4 = 0/2 + 1/4 + 1/8 + 1/16 = 0.25 + 0.125 + 0.0625 = 0.4375 So we can represent 0.4375 using 0.0111 (base 2). Another example: 0.1 = 0.000110011001100110011001100... 0.1 does not have a finite binary representation

Some problem numbers You already know from math that some numbers do not have a finite representation. Even worse, some numbers that have a finite representation in decimal do not in binary! (Thought question: is the reverse true?) Computer systems have finite memory. But we need to represent numbers that take an infinite number of bits. Solution?

IEEE-754 Floating Point Like a binary version of scientific notation 32 bits for a float (64 bits for a double) as follows: 1 bit for the sign 8 bits for the exponent e 23 bits for the mantissa (significand) M

Allocation of 32 bits 1 bit for the sign: 1 for negative and 0 for positive 8 bits for the exponent e To allow for negative exponents, 127 is added to that exponent to get the representation. We say that the exponent is biased by 127. So the range of possible exponents is not 0 to 2 8-1 = 0 to 255, but (0-127) to (255-127) = -127 to 128. 23 bits for the mantissa M Since the first bit must be 1, we don t waste space storing it!

IEEE-754 Floating Point 31 30 23 22 0 sign exponent mantissa s e M (-1) s * (1 + M) * 2 e-127

Rounding If we have to lose some digits, we don t just truncate, we round. In rounding a decimal to a whole number, an issue arises: If we have a 0.5, do we round up or down? If we always round up, we are biasing towards higher values. Proper rounding: round to the nearest even number. E.g., 17.5 is rounded up to 18 but 16.5 is rounded down to 16. The IEEE standard uses proper rounding also.

Historical aside 30 years ago, computer manufacturers each had their own standard for floating point. Problem? Writing portable software! Advantage to manufacturers? Customers got locked in to their particular computers. In the late 1980s, the IEEE produced the standard that now virtually all follow. William Kahan spearheaded the effort, and won the 1989 Turing Award for it.

Back to the example (Adding.java) As we saw, 0.1 cannot be represented exactly in binary, leading to the unexpected result. And adding a very small quantity to a very large quantity can mean the smaller quantity falls off the end of the mantissa. But if we add small quantities to each other, this doesn t happen. And if they accumulate into a larger quantity, they may not be lost when we finally add the big quantity in.

Examples 2 and 3 This seems contrived, but consider some value that accumulates in a loop. Code: Totalling.java Or consider adding up a list of doubles, what should you do? Code: ArrayTotal.java

Lessons When adding floating point numbers, add the smallest first. More generally, try to avoid adding dissimilar quantities. Specific scenario: When adding a list of floating point numbers, sort them first.

Example 4 Repeat a task for values in a particular range with an increment of 0.1. For example, for values 0.1 to 0.5 with an increment of 0.1. For example, for values 1.1 to 1.5 with an increment of 0.1. Code: FunctionValues.java

Lessons Don t use floating point variables to control what is essentially a counted loop. Also: Notice that we wrote x = 1.0 + i * 0.1; instead of initializing x to 1.0 and then repeatedly adding 0.1. Why? Fewer total arithmetic operations means fewer rounding errors are introduced. Use fewer arithmetic operations where possible.

Example 5 A very simple program that just prints the same variable using different formats. Code: Examine.java

What happened? We shouldn t be surprised by now to find out that 4/5 can t be represented exactly in a float. Lots of things can t. But the represented value should be off by a tiny bit. What are all these extra digits?? bit 24 4/5 = 1.1001100110011001100110011001100... x 2-1 It gets rounded to 1.10011001100110011001101 x 2-1 When we ask to print it as a decimal number, it gets converted. The exact equivalent is 0.800000011920928955078125000000 But only 7 of those digits are significant.

Lesson Don t print more precision in your output than you are holding.

Why does this matter?

Patriot missile accident In 1991, an American missile failed to track and destroy an incoming missile. Instead it hit a US Army barracks, killing 28. The system tracked time in tenths of seconds. The error in approximating 0.1 with 24 bits was magnified in its calculations. At the time of the accident, the error corresponded to 0.34 seconds. A Patriot missile travels about half a km in that time. Reference: http://www.ima.umn.edu/~arnold/disasters/ patriot.html

Sinking of an oil rig In 1992, the Sleipner A oil and gas platform sank in the North Sea near Norway. Numerical issues in modelling the structure caused shear stresses to be underestimated by 47%. As a result, concrete walls were not built thick enough. Cost: $700 million Reference: http://www.ima.umn.edu/~arnold/disasters/sleipner.html

What should you do? 95% of folks out there are completely clueless about floating-point. James Gosling

Follow the lessons Use double instead of float. When adding floating point numbers, add the smallest first. More generally, try to avoid adding dissimilar quantities. Specific scenario: When adding a list of floating point numbers, sort them first. Don t use floating point variables to control what is essentially a counted loop. Use fewer arithmetic operations where possible. Don t print more precision in your output than you are holding.

CSC336 Numerical Methods The study of computational methods for solving problems in linear algebra, non-linear equations, and approximation. The aim is to give students a basic understanding of both floating-point arithmetic and the implementation of algorithms used to solve numerical problems, as well as a familiarity with current numerical computing environments.