Informatik I (D-ITET) Übungsstunde 5, Hossein Shafagh

Similar documents
Informatik I (D-ITET) Übungsstunde 6, Hossein Shafagh

Floating Point Numbers

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

Informatik I (D-ITET) Übungsstunde 11, Hossein Shafagh

FLOATING POINT NUMBERS

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

8. Floating-point Numbers II

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

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

COMP2611: Computer Organization. Data Representation

ECE232: Hardware Organization and Design

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

Floating Point Arithmetic

15213 Recitation 2: Floating Point

Floating Point Arithmetic

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

Number Systems. Both numbers are positive

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

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

Numbers. John Perry. Spring 2017

Finite arithmetic and error analysis

Informatik I (D-ITET) Übungsstunde 12,

Data Representation Floating Point

Exercise: Using Numbers

CS242 COMPUTER PROGRAMMING

C++ Programming Lecture 11 Functions Part I

Data Representation Floating Point

Floating-point numbers. Phys 420/580 Lecture 6


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

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

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

Floating Point January 24, 2008

CS429: Computer Organization and Architecture

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

IEEE Standard for Floating-Point Arithmetic: 754

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

CS Computer Architecture. 1. Explain Carry Look Ahead adders in detail

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

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

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

System Programming CISC 360. Floating Point September 16, 2008

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

Today: Floating Point. Floating Point. Fractional Binary Numbers. Fractional binary numbers. bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j

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

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

Numeric Encodings Prof. James L. Frankel Harvard University

Foundations of Computer Systems

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

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

2. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Chapter 3 - Functions

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

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

Floating-point representations

Floating-point representations

Giving credit where credit is due

6. Control Statements II

Floating Point Numbers

Floating Point Numbers

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Giving credit where credit is due

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

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

3.5 Floating Point: Overview

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

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

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

CS2141 Software Development using C/C++ C++ Basics

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

Thomas Polzer Institut für Technische Informatik

COMP Overview of Tutorial #2

Chapter Three. Arithmetic

MATH 353 Engineering mathematics III

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

Data Representation Floating Point

FLOATING POINT NUMBERS

CHAPTER 3 Expressions, Functions, Output

Inf2C - Computer Systems Lecture 2 Data Representation

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Signed umbers. Sign/Magnitude otation

CMPSCI 145 MIDTERM #2 SPRING 2017 April 7, 2017 Professor William T. Verts NAME PROBLEM SCORE POINTS GRAND TOTAL 100

mith College Computer Science Fixed & Floating Point Formats CSC231 Fall 2017 Week #15 Dominique Thiébaut

Computer Systems Programming. Practice Midterm. Name:

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

Chapter 2. Outline. Simple C++ Programs

Functions and Recursion

Floating Point Arithmetic. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

Computer Systems C S Cynthia Lee

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Floating Point Representation. CS Summer 2008 Jonathan Kaldor

Up next. Midterm. Today s lecture. To follow

ECE331: Hardware Organization and Design

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

Transcription:

Informatik I (D-ITET) Übungsstunde 5, 23.10.2017 Hossein Shafagh shafagh@inf.ethz.ch

Übungsblatt 4 Problem 4.1. Loop mix-up // Fakultät unsigned int n; std::cin >> n; unsigned int f = 1; if(n!= 0) { do { f = f * n; --n; } while(n > 0); } std::cout << f << std::endl; unsigned int n; std::cin >> n; unsigned int f; for(f = 1; n > 0; --n) f = f * n; std::cout << f << "\n";

Übungsblatt 4 Problem 4.1. Loop mix-up // Compute the sum while(true) { int i1, i2; std::cin >> i1 >> i2; std::cout << i1+i2 << "\n ; } int again; std::cout << "Again? (0/1)\n"; std::cin >> again; if(!again) break; int again; do { int i1, i2; std::cin >> i1 >> i2; std::cout << i1 + i2 << "\n ; std::cout << "Again? (0/1)\n"; std::cin >> again; } while(again);

Übungsblatt 4 Problem 4.1. Loop mix-up // Compute Modulo unsigned int z; unsigned int d; for(std::cin >> z >> d ; z >= d ; z = z-d); std::cout << z << std::endl; unsigned int z, d; std::cin >> z >> d; while(z >= d) { z = z d; } std::cout << z << "\n";

Übungsblatt 4 Problem 4.2. Loop Analysis a) x = 2 n b) {0,.., 31}, 2 32 à overflow! c) For n = 0 the program terminates after not entering the if-block. For n N [1, 31], k is strictly monotonic increasing from start value 0, eventually fulfilling k = n and setting the termination condition. This is independent of the value of x. unsigned int x = 1; if (n > 0) { unsigned int k = 0 bool e = true; do { if (++k == n) { e = false; } x *= 2; } while(e); } std::cout << x << std::endl; unsigned int x = 1; for (unsigned int i=0; i<n; ++i) { x *= 2; } std::cout << x << std::endl;

Übungsblatt 4 Problem 4.3. Floats and Conversions Determine type and value for each of the following expressions: (a) 2e2 3e3f > 23.0 false (boolean) (b) 7 + 7.5 0.5 (double) (c) 1.0f / 2 + 1/3 + 1/4 0.5 (float) (d) 1u 2u < 0 false (boolean) (e) 1 + 2 3 + 4 11 (int) (f) int(8.5) int(7.6) 1 (int)

Übungsblatt 4 Problem 4.4. Approximation of π double sum = 0.0; double a = 1.0; double b = 1.0; for (unsigned int count = 1; n >= count; ++count) { sum += (a / b); a *= (-1); b += 2; } cout << "sum = " << 4 * sum << endl; // initialize auxiliary variables for first term of sum double numer = 2.0; // numerator i-th term double denom = 1.0; // denominator i-th term // pi: value after term i (i=0 initially, then i=1,2,...,n-1) double pi = 2.0; for (int i = 1; i < n; ++i) { numer *= i; denom *= (2 * i + 1) pi += numer/denom; }

Übungsblatt 4 Problem 4.4. Approximation of π For n = 1000000, the output of sum1 is correct to five digits after the decimal point: 3.14159. Sum2 already gives the result 3.14159 for n = 17, so this version is obviously preferable. Caveat: sum2 quickly results in (on our platform after n = 172) terms that are so small that they cannot be represented as 64-bit (double) floating point number (NaN: not a number)

Binary Representation Binary representation of the decimal number 1.9? 2x x b i x b i 1.9 1 0.9 1.8 1 0.8 1.6 1 0.6 1.2 1 0.2 0.4 0 0.4 0.8 0 0.8 1.6 1 0.6 1.11100

Exercise 1) Binary Representation a) 0.25 x b i x b i 0.25 0 0.25 0.5 0 0.5 1.0 1 0.0 0.01 a) 11.1 x b i x b i 1.1 1 0.1 0.2 0 0.2 0.4 0 0.4 0.8 0 0.8 1.6 1 0.6 1.2 1 0.2 0.4 0 0.8 1011.00011

A 10 bit floating point type scientific notation: 2.73 10 12 1 digit for decimal point 5 for after decimal point 4 for exponent 1.00110 2 15 or 0.00001 2 0 F(β, p, e min, e max ), β 2, p 1 So for our 10 bit example, β = 2 and p = 6, but what are e min and e max?

A 10 bit floating point type F(2, 6, e min, e max ) IEEE 754 standard unsigned int - bias Bias: 8 à e min = 0 8 = 8 and e max = 15 8 = 7 F (2, 6, 8, 7) smallest representable number: 0.00001 2 8 largest representable number: 1.11111 2 7

A 10 bit floating point type F(β, p, e min, e max ) F (2, 6, 8, 7) smallest representable number: 0.00001 2 8 largest representable number: 1.11111 2 7 Two important changes One bit for sign to represent negative values There is always exactly one non-zero digit in front of the decimal point! (scientific notation) à don t store this bit à hidden bit F (2, 6, 8, 7) +1.11111 2 7 (biggest number) 1.11111 2 7 (smallest number) +1.00000 2 8 (smallest positive number)

A 10 bit floating point type F (2, 6, 8, 7) +1.11111 2 7 (biggest number) 1.11111 2 7 (smallest number) +1.00000 2 8 (smallest positive number) How to represent 0? Exceptional values: exponent 8 (0000) is the signal that we have an exceptional value 5 bits after the decimal point: 00000 means 0 00001 means 00010 means 00011 means NaN (not a number) This leaves us with F (2, 6, 7, 7)

The floating point system F (2, 24, 126, 127) float type s internal representation for double it is F (2, 53, 1022, 1023) 24: 23 digits + 1 sign bit 8 for exponent: Bias of 127 0, 255 for special values: 0,, and NaN (not a number)

Exercise 2) Largest and smallest positive numbers What are the largest and smallest positive normalized single and double precision floating point numbers, according to the IEEE 754 standard? 2 e min Single precision: 2-126 Double precision: 2-1022 Largest normalized number: +1.11111111111111111111111 2 127

Exercise 2) Largest and smallest positive numbers What are the largest and smallest positive normalized single and double precision floating point numbers, according to the IEEE 754 standard? Largest normalized number: +1.11111111111111111111111 2 127 More elegant: (1 (1/β) p ) β emax+1 Single: (1 - (1/2) 24 ) 2 128 = 2 128 2 104 Double: (1 - (1/2) 53 ) 2 1024 = 2 1024 2 971

Floating Point Guidelines

Function Definition and Declaration // PRE: Value representing an angle expressed in radians. // One radian is equivalent to 180/PI degrees. // POST: Cosine of x in radians. double cos (double x); // available in #include <cmath> // PRE: y!= 0 // POST: returns quotient x / y double quotient (double x, double y) { assert (y!= 0); return x / y; // possible PRE condition violation here }

Function Exercises

Übungsblatt 5 Problem 5.1. Floating-Point Number Representation Problem 5.2. Point on Line? Problem 5.3. Rounding Problem 5.4. Binary Expansion Problem 5.5. Fixing Functions