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

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

Inf2C - Computer Systems Lecture 2 Data Representation

COMP2611: Computer Organization. Data Representation

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

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

Module 2: Computer Arithmetic

Chapter 3: Arithmetic for Computers

Divide: Paper & Pencil

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

ECE232: Hardware Organization and Design

Signed Multiplication Multiply the positives Negate result if signs of operand are different

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

FLOATING POINT NUMBERS

Number Systems and Computer Arithmetic

CO212 Lecture 10: Arithmetic & Logical Unit

COMP Overview of Tutorial #2

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Introduction to Computers and Programming. Numeric Values

CS101 Lecture 04: Binary Arithmetic

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


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

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

Organisasi Sistem Komputer

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Floating Point Numbers. Lecture 9 CAP

Chapter Three. Arithmetic

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Floating Point Numbers

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

Programming Using C Homework 4

Computer Systems C S Cynthia Lee

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

Systems Programming and Computer Architecture ( )

Data Representation Floating Point

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

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Zheng-Liang Lu Java Programming 45 / 79

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

Chapter 4. Operations on Data

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

3.5 Floating Point: Overview

CS 101: Computer Programming and Utilization

MACHINE LEVEL REPRESENTATION OF DATA

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Numeric Encodings Prof. James L. Frankel Harvard University

FLOATING POINT NUMBERS

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

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

Number Systems CHAPTER Positional Number Systems

A Level Computing. Contents. For the Exam:

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

Data Representation Floating Point

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Chapter 2 Data Representations

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Binary Adders: Half Adders and Full Adders

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

ROUNDING ERRORS LAB 1. OBJECTIVE 2. INTRODUCTION

Number Representations

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

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

Floating Point Arithmetic

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Data Representations & Arithmetic Operations

UNIT 7A Data Representation: Numbers and Text. Digital Data

Computer Architecture and IC Design Lab. Chapter 3 Part 2 Arithmetic for Computers Floating Point

IEEE Standard for Floating-Point Arithmetic: 754

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

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

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

Floating Point Arithmetic

Foundations of Computer Systems

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

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

JAVA OPERATORS GENERAL

1.3b Type Conversion

CHAPTER 5: Representing Numerical Data

Number System. Introduction. Decimal Numbers

Computer Arithmetic Floating Point

Computer System and programming in C

15213 Recitation 2: Floating Point

These are reserved words of the C language. For example int, float, if, else, for, while etc.

4. Number Representations

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

Floating Point Numbers

Floating Point Numbers

Chapter 5 : Computer Arithmetic

Signed umbers. Sign/Magnitude otation

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Number Systems. Both numbers are positive

CS61C : Machine Structures

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

CIS133J. Working with Numbers in Java

Experimental Methods I

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

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

Floating-point representations

Floating-point representations

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

Transcription:

COSC 243 Data Representation 3 Lecture 3 - Data Representation 3 1

Data Representation Test Material Lectures 1, 2, and 3 Tutorials 1b, 2a, and 2b During Tutorial a Next Week 12 th and 13 th March If you cannot attend your tutorial Let me know and we ll re-schedule Into another tutorial next week Lecture 3 - Data Representation 3 2

Overview Last Lecture Data representation (integers) This Lecture Data representation (real numbers) Source: lecture & lecture notes Source: chapter 10 (10 th edition) Next Lecture Digital logic Gates Boolean algebra Lecture 3 - Data Representation 3 3

What Time Is It? 11 12 10 9 8 1 2 4 3 7 6 5 Lecture 3 - Data Representation 3 4

What Time Is It? What time will it be two hours from now? 11 12 10 1 2 9 3 8 4 7 6 5 Lecture 3 - Data Representation 3 5

What Time Is It? 11 12 10 1 2 What time was it one hour ago? 9 3 8 4 7 6 5 Lecture 3 - Data Representation 3 6

Integers (Two s Complement) Recall: Integers are typically stored in two s complement format 100 101 110 111 000 001 010 011-4 -3-2 -1 0 1 2 3 In this 3-bit example: 0 is stored as 000 Integer addition is binary addition Integer subtraction is binary addition of the negative of the right operand A-B = A + -B Conversion into two s complement +ve: use the binary representation -ve: complement and add 1 Lecture 3 - Data Representation 3 7

Integers (Two s Complement) Two s complement works exactly like clock arithmetic (modulo arithmetic) If we re at 2, what do you get if you subtract 4? 111-1 000 0 1 001 If we re at 2, what do you get if you add 4? 110-2 2 010-3 3 101-4 011 100 Lecture 3 - Data Representation 3 8

Real Numbers Integers are discrete. Real numbers are continuous. In Computer Science real numbers are often called floating point numbers For reasons that will become obvious Floats can be very large or very small 4,386,593,021,854.341 0.000,000,000,008 How do we represent real numbers in the computer? Lecture 3 - Data Representation 3 9

Scientific Notation (decimal) If you move the decimal point to the left: You increase the exponent. If you move the decimal point to the right: You decrease the exponent. p 1 Remember: 10 0 = 1 10 = 10 12,345.6 = 0.012345 = 12,345.6 x 10 0 0.012345 x 10 0 1,234.56 x 10 1 0.12345 x 10-1 123.456 x 10 2 1.2345 x 10-2 12.3456 x 10 3 12.345 x 10-3 1.23456 x 10 4 123.45 x 10-4 p Lecture 3 - Data Representation 3 10

Scientific Notation (binary) 11000.1= 11000.1 x 2 0 1100.01 x 2 1 110.001 x 2 2 11.0001 x 2 3 1.10001 x 2 4 If you move the decimal point to the left: You increase the exponent. If you move the decimal point to the right: You decrease the exponent. Remember: p 2 0 = 1 2 = 1 2 p Lecture 3 - Data Representation 3 11

Floating Point In scientific notation the number is broken into 3 parts The significant digits The power The base The base is usually implicit (we know it) If you store the other two separately then the power determines the location of the decimal point That is, the decimal point floats relative to the significant digits We call these numbers floating point numbers There s a number of formats including the IEEE 754 standard Lecture 3 - Data Representation 3 12

IEEE Floating Point Format Stored as a binary32 (single) 4-byte number (float) Where 1-bit 8-bits 23-bits s e f s = sign (0 for positive, 1 for negative) of the number e = power of two, excess 127 notation (exponent) Allowed values for e are 0-255 f = binary fraction (mantissa, significand) Without the leading 1 before the binary point Why? s (-1) x 1.f x 2 e-127 Lecture 3 - Data Representation 3 13

Decimal to IEEE Conversion 1. Convert the decimal number to binary 2. Write the binary number in scientific notation base 2 3. Write f by taking the fractional part of the normalised number and adding trailing zeroes to get 23 bits 4. Determine sign bit, s 5. Add 127 to the exponent (from step 2) to get e 6. Convert e to an 8 bit binary number (add leading zeroes if needed) 7. Write in IEEE format by concatenating s, e, and f Lecture 3 - Data Representation 3 14

Decimal to IEEE Conversion Example Convert 24.5 10 to IEEE floating point representation: 1. Convert the decimal number to binary 24.5 10 = 16 + 8 + 0 + 0 + 0 + 0.5 = 11000.1 2. Write the binary number in scientific notation using base 2 = 1.10001 * 2 4 Lecture 3 - Data Representation 3 15

Decimal to IEEE Conversion Example (cont.) 3. Write f by taking the fractional part of the normalised number and adding trailing zeroes to get 23 bits 24.5 10 = 1.10001 * 2 4 f = 10001 100 0100 0000 0000 0000 0000 4. Determine sign bit, s Positive number so s = 0 Lecture 3 - Data Representation 3 16

Decimal to IEEE Conversion Example (cont.) 5. Add 127 to the exponent (from step 2) to get e e = 4 + 127 = 131 6. Convert e to an 8 bit binary number (add leading zeroes if needed) 131= 128 + 0 + 0 + 0 + 0 + 0 + 2 + 1 = 1000 0011 7. Write in IEEE format by concatenating s, e, and f 0100 0001 1100 0100 0000 0000 0000 0000 = 41C40000 16 s e f Lecture 3 - Data Representation 3 17

IEEE to Decimal Conversion 1. Group the binary digits into 1, 8, and 23 digits (s,e,f) 2. Convert e to decimal Subtract 127 to get exp 3. Delete the trailing zeroes from f and write: 1.f x 2 exp where the exp is the value from step 2 and f is the original f with the trailing zeroes removed 4. Un-normalise the number by moving the binary point until the exp = 0 5. Convert the binary number to decimal 6. If s is 1, negate the number Lecture 3 - Data Representation 3 18

IEEE to Decimal Conversion Example Convert C1C40000 16 to decimal form C1C40000 16 = 1100 0001 1100 0100 0000 0000 0000 0000 1. Group the binary digits into 1, 8, and 23 digits (s,e,f) 1100 0001 1100 0100 0000 0000 0000 0000 s e f Lecture 3 - Data Representation 3 19

IEEE to Decimal Conversion Example (cont.) 2. Convert e to a decimal number. 1000 0011 = 128 + 0 + 0 + 0 + 0 + 0 + 2 + 1 = 131 Subtract 127 to get exp 131 127 = 4 (exponent) Lecture 3 - Data Representation 3 20

IEEE to Decimal Conversion Example (cont.) 3. Delete the trailing zeroes from f and write 1.f x 2 exp where the exp is the value from step 2 and f is the original f with the trailing zeroes removed 100 0100 0000 0000 0000 0000 10001 = 1.10001 x 2 4 4. Un-normalise the number by moving the binary point until the exp = 0 1.10001 x 2 4 = 11000.1 Lecture 3 - Data Representation 3 21

IEEE to Decimal Conversion Example (cont.) 5. Convert the binary number to decimal 11000.1 = 16 + 8 + 0 + 0 + 0 + 0.5 = 24.5 6. If s is 1, negate the number s = 1, so the number is negative The answer is -24.5 Lecture 3 - Data Representation 3 22

IEEE Special Cases The IEEE floating point has some special cases: e = 0 f = 0 Number is 0.0 e = 255 f = 0 or - e = 255 f 0 Not a number (NaN) e = 0 f 0 (-1) s x 0.f x 2-126 Lecture 3 - Data Representation 3 23

Other IEEE Representations Name Common name Base Digits E min E max Decimal Decimal digits E max binary16 Half precision 2 10+1-14 +15 3.31 4.51 binary32 Single precision 2 23+1-126 +127 7.22 38.23 binary64 Double precision 2 52+1-1022 +1023 15.95 307.95 binary128 Quad precision 2 112+1-16382 +16383 34.02 4931.77 decimal32 10 7-95 +96 7 96 decimal64 10 16-383 +384 16 384 decimal128 10 34-6143 +6144 34 6144 Lecture 3 - Data Representation 3 24

Floating Point Arithmetic (+/-) Addition (subtraction) Align the significands Convert to the same 2 e (align) Add (or subtract) Round and normalize Convert back into scientific notation Lecture 3 - Data Representation 3 25

Floating Point Arithmetic (+/-) 48 + 2.5 48 = 1.100*2 5 2.5 = 1.01 * 2 1 Convert to the same 2 e then add 48 = 2.5 = 1. 1 0 0 0 0 0 *2 5 0. 0 0 0 1 0 1 *2 5 48 + 2.5 = 1. 1 0 0 1 0 1 *2 5 = 50.5 Lecture 3 - Data Representation 3 26

Floating Point Arithmetic (*) Multiplication The significands are multiplied The exponents are added Round and normalize Lecture 3 - Data Representation 3 27

Floating Point Arithmetic (/) Division The significands are divided The exponents are subtracted Round and normalize Lecture 3 - Data Representation 3 28

Consequences What is the output of this program: #include <stdio.h> int main(void) { float x; for (x = 0; x!= 1.0; x += 0.1) printf("%f\n", x); return 0; } Java Equivalent: public static void main(string [] args) { float x; for (x = 0; x!= 1.0; x+= 0.1) System.out.println(x); } Lecture 3 - Data Representation 3 29

Consequences What is the output of this program: #include <stdio.h> int main(void) { float x; x = 123456789.0; printf("%f + 1 = %f\n", x, x + 1); return 0; } Java equivalent public static void main(string [] args) { float x = 123456789.0f; System.out.printf("%f + 1 = %f\n", x, x + 1); } Lecture 3 - Data Representation 3 30

Homework Download the two programs (previous two slides) from the COSC243 website and change them into programs that produce the correct output Lecture 3 - Data Representation 3 31