Bonus Puzzle #1 Random Doubles [0,5] Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Similar documents
Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Analysis of Algorithms Chapter 11. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Graphs Chapter 24. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

15213 Recitation 2: Floating Point

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Inheritance Chapter 8. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Arrays Chapter 7. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Searching and Sorting Chapter 18. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Divide: Paper & Pencil

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

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

Welcome to CMPT 125/126! Introduction to Computer Science and Programming II

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Physics 331 Introduction to Numerical Techniques in Physics

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

unused unused unused unused unused unused

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

MA 1128: Lecture 02 1/22/2018

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

IEEE Floating Point Numbers Overview

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

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

Scientific Computing. Error Analysis

Variables and Data Representation

Programming Using C Homework 4

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

Introduction to Computers and Programming. Numeric Values

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions

Floating Point Arithmetic

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

Floating-Point Arithmetic

Introduction to Scientific Computing Lecture 1

COMP Overview of Tutorial #2

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

Outline. What is Performance? Restating Performance Equation Time = Seconds. CPU Performance Factors

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

Floating Point. EE 109 Unit 20. Floating Point Representation. Fixed Point

Floating Point Numbers. Lecture 9 CAP

The type of all data used in a C (or C++) program must be specified

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

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

Module 2: Computer Arithmetic

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

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

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

Floating Point Numbers

Introduction to Computer Systems Recitation 2 May 29, Marjorie Carlson Aditya Gupta Shailin Desai

Chapter 3: Arithmetic for Computers

Chapter Three. Arithmetic

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

Thijs Dorssers, Pieter van den Hombergh, Richard van den Ham. May 24, 2018

Floating Point Arithmetic

Learning from Math Library Testng for C Marcel Beemster Solid Sands

CS61C : Machine Structures

COMP-202: Foundations of Programming

Floating Point Representation. CS Summer 2008 Jonathan Kaldor

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

Handling Numeric Representation SAS Errors Caused by Simple Floating-Point Arithmetic Computation Fuad J. Foty, U.S. Census Bureau, Washington, DC

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

ECE232: Hardware Organization and Design

Computer Systems C S Cynthia Lee

Number Systems and Computer Arithmetic

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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

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

CHAPTER 5: Representing Numerical Data

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

Slide 1 CS 170 Java Programming 1 Real Numbers Duration: 00:00:54 Advance mode: Auto

Representing numbers on the computer. Computer memory/processors consist of items that exist in one of two possible states (binary states).

ME 261: Numerical Analysis. ME 261: Numerical Analysis

Review for Midterm. Instructor: Scott Kristjanson CMPT 135 SFU Surrey, Spring 2016

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

Floating Point. CSC207 Fall 2017

A Level Computing. Contents. For the Exam:

What we need to know about error: Class Outline. Computational Methods CMSC/AMSC/MAPL 460. Errors in data and computation

Computational Methods CMSC/AMSC/MAPL 460. Representing numbers in floating point and associated issues. Ramani Duraiswami, Dept. of Computer Science

COMP2611: Computer Organization. Data Representation

Machine Arithmetic 8/31/2007

2.1.1 Fixed-Point (or Integer) Arithmetic

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

Number Systems. Both numbers are positive

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

Number Systems. The Computer Works in Binary, or how I learned to think like a computer. The computer s natural number system is binary not decimal.

ESc101 : Fundamental of Computing

MATH 353 Engineering mathematics III

2 Computation with Floating-Point Numbers

Organisasi Sistem Komputer

Topic Notes: Bits and Bytes and Numbers

Data Representation Floating Point

Transcription:

Bonus Puzzle #1 Random Doubles [0,5] Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

2 Bonus Puzzle #1 Write an expression to calculate double random numbers from [0, 5.000000000] inclusive Note: Generating the range: [0, 5.0) is easy: generator.nextfloat()*5 Wk04.1 Slide 2

3 Honourable Mentions Some good tries by: Amritpaul Gill Gavin Haynes Adam Tuck A working solution by Raymond Zeng But slow convergence, not guaranteed to ever complete Wk04.1 Slide 3

4 And the Winner is. Serena Read: Simple elegant well commented solution Returns entire range with [0,5] inclusive Fast convergence, but still may never terminate public static float rannum() { Random ran = new Random(); int a; float f, r; do{ a = ran.nextint(6); f = ran.nextfloat(); r = (float)a - f; } while(r<0); } return r; Wk04.1 Slide 4

5 Scott s solution Can we improve on Serena s and Raymond s solution? Want guaranteed convergence Want to eliminate loops Want to cover the whole range uniformly (this last bullet is much trickier than you think!) Wk04.1 Slide 5

BUT DO WE WANT TO SOLVE THIS? 6 What is the practical difference between [0,5) and [0,5]? Remember when you compare floats, we usetolerances That means 4.999999 equals 5.000000 within a tolerance How many real numbers are in between 4.999999 and 5.000000? How many java floating point numbers are in this gap? ONE : 4.9999995 That is because floats only have 32 bits to represent the number There is limited precision, and thus limited resolution. Do we WANT to put all this effort into something whose probability of occurring approaches zero in the limit? It took over 2 million tries for Serena s algorithm to return 5.000000 Wk04.1 Slide 6

Representing Real numbers in Java [2] 7 Java uses the IEEE 754 standard: This uses 1-plus form of the binary normalized fraction (rounded). The fraction part is called the mantissa. 1-plus normalized scientific notation base two is then: N = ± (1.b1b2b3b4...)2 x 2+E float double For a great overview of this standard, see [2] at: http://people.uncw.edu/tompkinsj/133/numbers/reals.htm Wk04.1 Slide 7

8 Ok, so you really want to solve this Only missing a single finite range with [0,5) If we know the size of this gap, We can add it in with 50/50 probability to close the gap uniformly generator.nextfloat()*5 + ((generator.nextint() >= 0)? epsilon : 0); What s epsilon? It is based on the resolution (# bits in real number) plus the exponent (smaller magnitude numbers have finer resolution). epsilon = (.5) 23 = 0.00000000000000000000001 B Wk04.1 Slide 8

But this assumes nextfloat works as expected 9 Yes, nextfloat works, but is does not cover the entire [0,1) space uniformly. nextfloat produces a random number based on 2 24 possible floats within this range (see [3] for details). However there are really 2 30 possible floats in the range [0,1) using the IEEE 754 standard (see [2] for details). That s only about 1.6% of all possible floats that get returned. If you really want to get them all, you will need to add in a 6 bit random number to the end of what nextfloat returns. Instead of using a constant Epsilon as in previous slide, it needs to become a 6-bit random variable.

10 References: 1. J. Lewis, P. DePasquale, and J. Chase., Java Foundations: Introduction to Program Design & Data Structures. Addison-Wesley, Boston, Massachusetts, 3rd edition, 2014, ISBN 978-0-13-337046-1 2. Tompkins, J.A., Java Primitive Data Types - Reals - IEEE754 http://people.uncw.edu/tompkinsj/133/numbers/reals.htm 3. Oracle Reference Pages on the Random Class http://docs.oracle.com/javase/7/docs/api/java/util/random.html#nextfloat%28%29 Wk04.1 Slide 10