Fall 2006AE6382 Design Computing1 Numeric Representation in a Computer Learning Objectives Understand how numbers are stored in a computer and how the.

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

2-1 Chapter 2 - Data Representation Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer Architecture.
Chapter 2: Data Representation
Principles of Computer Architecture Miles Murdocca and Vincent Heuring Chapter 2: Data Representation.
Faculty of Computer Science © 2006 CMPUT 229 Floating Point Representation Operating with Real Numbers.
ECIV 201 Computational Methods for Civil Engineers Richard P. Ray, Ph.D., P.E. Error Analysis.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Round-Off and Truncation Errors
Chapter 3 Data Representation.
Chapter 3 Data Representation. Chapter goals Describe numbering systems and their use in data representation Compare and contrast various data representation.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
CSE 378 Floating-point1 How to represent real numbers In decimal scientific notation –sign –fraction –base (i.e., 10) to some power Most of the time, usual.
Chapter 2 Data Types, Declarations, and Displays
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Computer Science 210 Computer Organization Floating Point Representation.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
Simple Data Type Representation and conversion of numbers
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Information Representation (Level ISA3) Floating point numbers.
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Fixed-Point Arithmetics: Part II
IT253: Computer Organization
Computing Systems Basic arithmetic for computers.
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles of Computer.
Data Representation.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
College of Engineering Representing Numbers in a Computer Section B, Spring 2003 COE1361: Computing for Engineers COE1361: Computing for Engineers 1 COE1361:
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Data Representation in Computer Systems
Floating Point (a brief look) We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large numbers,
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
CSC 221 Computer Organization and Assembly Language
The Hexadecimal Number System and Memory Addressing ISAT 121.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Floating Point Representation.
Number Systems Revision of conversations What is a register Addition Complementation.
Problems with Floating-Point Representations Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright.
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Computer Arithmetic Floating Point. We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large.
Lecture 4 - Numerical Errors CVEN 302 June 10, 2002.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Chapter Two Hardware Basics: Inside the Box ©1999 Addison Wesley Longman2.2 Chapter Outline What Computers Do A Bit About Bits The Computer’s Core: CPU.
Data Representation. How is data stored on a computer? Registers, main memory, etc. consists of grids of transistors Transistors are in one of two states,
Module 2.2 Errors 03/08/2011. Sources of errors Data errors Modeling Implementation errors Absolute and relative errors Round off errors Overflow and.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Programming and Data Structure
Dr. Clincy Professor of CS
Recent from Dr. Dan Lo regarding 12/11/17 Dept Exam
Introduction to Java, and DrJava part 1
Storing Negative Integers
Introduction to Java, and DrJava
Recent from Dr. Dan Lo regarding 12/11/17 Dept Exam
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Computer Organization and Assembly Language
Presentation transcript:

Fall 2006AE6382 Design Computing1 Numeric Representation in a Computer Learning Objectives Understand how numbers are stored in a computer and how the computer operates on them. Topics Numbers on a computer Precision and accuracy Numeric operators Precedence Exercises Summary

Fall 2006AE6382 Design Computing2 Numbers: precision and accuracy Low precision:  = 3.14 High precision:  = Low accuracy:  = High accuracy:  = High accuracy & precision:  = Good Accuracy Good Precision Good Precision Poor Accuracy Good Accuracy Poor Precision Poor Accuracy Poor Precision

Fall 2006AE6382 Design Computing3 Computer Memory Numbers and the results of numeric computations (along with other data such as text, graphics, documents, etc) must be stored somewhere in a computer. That “somewhere” is “memory”. Memory comes in a variety of types and speeds: Cache – in the CPU itself (fastest) RAM - external to the CPU (fast) Disk - physical media, external to the CPU, r/w CDROM - physical media, (slow) Tape - physical media, (slowest) Memory is measured in “bytes” (and kilobytes, megabytes, gigabytes, and terabytes.)

Fall 2006AE6382 Design Computing4 Computer Memory is Varied…

Fall 2006AE6382 Design Computing5 Memory in MATLAB

Fall 2006AE6382 Design Computing6 Inside the Bytes In the previous slide, we see: What is the size of the variable “i” What does “class” represent? How many bytes are used to store the value? Name Size Bytes Class k 1x1 8 double array s 1x12 24 char array x 1x double array Grand total is 213 elements using 1632 bytes

Fall 2006AE6382 Design Computing7 Numbers and their Bases Numbers we use are DECIMAL (or base 10): –Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 –123 = 1* * *10 0 But we can always use other bases: Octal (base 8): –Digits: 0, 1, 2, 3, 4, 5, 6, 7 –123 = 1* * *8 0 –123 8 = = Binary (base 2): –Digits: 0, 1 –1011 = 1* * * *2 0 – = = –123 8 = = Hexidecimal (base 16): –Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F –123 = 1* * *16 0 – = = – = =

Fall 2006AE6382 Design Computing8 Inside the Bytes A byte is the smallest memory allocation available. A byte contains 8 bits so that: –Smallest: = 0 10 –Largest: = 1*2 7 +1*2 6 +1*2 5 +1*2 4 +1*2 3 +1*2 2 +1*2 1 +1*2 0 = (or ) Result: a single byte can be used to store an integer number ranging from 0 to 255 (256 different numbers) If negative numbers are included, one bit must be dedicated to the sign, leaving only 7 bits for the number –Smallest: 0 –Largest: or

Fall 2006AE6382 Design Computing9 Inside the Bytes 2^8-1 (255) is not a very big number, so computers generally use multiple bytes to represent numbers: Here is some vocabulary: –byte = 8 bits –single (precision) = 4 bytes –double (precision) = 8 bytes –quad (precision) = 16 bytes –char = 2 bytes (used to be 1 byte) Note: –A word is the basic size of the CPU registers and for Pentium chips it is 4 bytes or 32 bits; it is 8 bytes for the new Itanium and some unix chipsets; it was 2 bytes for early Intel chips; some game consoles use 16 byte words.

Fall 2006AE6382 Design Computing10 Inside the Bytes: Exercise Let’s investigate how MATLAB stores numbers: Try the following MATLAB commands: format short 2^8 2^8-1 %(is the answer correct?) 2^64 2^64-1 % (is the answer correct?) Question: what is the largest value of the exponent so that the answer above is correct?

Fall 2006AE6382 Design Computing11 Inside the Bytes How are fractional (floating point) numbers stored in a computer? The IEEE 754 double format consists of three fields: –a 52-bit fraction, f –an 11-bit biased exponent, e –and a 1-bit sign, s These fields are stored contiguously in 8 bytes (or 2 successively addressed 4-byte words):

Fall 2006AE6382 Design Computing12 Inside the Bytes Because only a finite number of bits can be used for each number, not all possible numbers can be represented: Negative numbers less than -( ) x (negative overflow) Negative numbers greater than (negative underflow) Positive numbers less than (positive underflow) Positive numbers greater than ( ) x (positive overflow) Zero (actually is a special combination of bits)

Fall 2006AE6382 Design Computing13 Inside the Bytes Others sources of error in computation: Errors in the input data - measurement errors, errors introduced by the conversion of decimal data to binary, roundoff errors. Roundoff errors during computation (as discussed) Truncation errors - using approximate calculation is inevitable when computing quantities involving limits and other infinite processes on a computer –Never try to compare two floating point numbers for equality because all 16 digits would have to match perfectly…

Fall 2006AE6382 Design Computing14 Describing error Precision The smallest difference that can be represented on the computer (help eps) Accuracy How close your answer is to the “actual” or “real” answer. Recognize: MATLAB (and other programs that use IEEE doubles) give you “good” digits MATLAB COMMANDS realmin, realmax, eps (try with help )

Fall 2006AE6382 Design Computing15 Inside the bytes Back to MATLAB - what does all this mean? We must pay attention to the math! (on any computer!) Given a finite (limited) number of bits, almost all computations will result in numbers that can’t be represented, Remember that the result of a floating-point computation must be ROUNDED to fit back into it’s finite representation Always check your results - design programs to allow for independent verification of computations!

Fall 2006AE6382 Design Computing16 SUMMARY Describe memory. List different kinds of memory. What is IEEE 754? Describe how MATLAB represents numbers. Draw a number line and identify ranges where computers will generate errors. Describe three potential sources of errors in computation. Describe precision. Describe accuracy. Describe how we can protect ourselves from computation error.

Fall 2006AE6382 Design Computing17 Numeric computation in MATLAB

Fall 2006AE6382 Design Computing18 Simple Math and Evaluation Expressions are evaluated using standard algebraic hierarchy, with parenthesis overriding the normal convention. ( help ops ) OperationSymbolExample Exponentiation^3 ^ 2 Multiplication*5 * 3 Division/ or \5 / 4 or 4 \ 5 Addition+6+2 Subtraction-5 – 3 PRECEDENCE - the order expressions are evaluated!

Fall 2006AE6382 Design Computing19 Practice: Simple Calculations What are the results of the following MATLAB expressions? –5 ^ / 2 ^ 3 – 25 –(5-3)^2 –(5-3)*(2-1)\8 –2*pi –sin(pi/2) Try entering the values above in MATLAB Try entering help ops NOTE Using spaces before and after operators is a matter of style and readability

Fall 2006AE6382 Design Computing20 Variables and Names A variable is a placeholder in memory Variables contain values Variable names: –Are case sensitive: Cost, cost, COST are different –May contain up to 31 characters (more are ignored) –Must start with a letter, –May contain numbers and letters –May NOT contain punctuation except “_” How do I view the contents of a variable? –Just type the variable name without a following “;”

Fall 2006AE6382 Design Computing21 Practice: simple computations Try the following MATLAB code: –Example 1 X = 5 ^ 2; Y = 2 * 2; Z = X * Y; Z –Example 2 Price = 19.95; Tax = 0.07; Units = 3; Cost = (Units * Price) * (1.0 + Tax ) What about the “;”? What is Y? What is Price?

Fall 2006AE6382 Design Computing22 Where are Variables Stored? Variables are stored in a “workspace” Workspace commands: –who – display variables –whos – display variables and sizes –clear – removes variables from the work space –help clear – display help on command “clear” –clc - clears the command window (screen) –home - move cursor to top-left of command window Workspace is shown in the upper left MATLAB pane –Double-click on any variable to view it’s contents!

Fall 2006AE6382 Design Computing23 How are numbers displayed? Display is different from storage. MATLAB computes with significant digits (IEEE double), but often shows less! The format command controls how values are displayed: –format short; format long –format short e; format long e; –format short g; format long g –format rat; format short –format compact; format loose Try using help format

Fall 2006AE6382 Design Computing24 MATLAB Built-in Functions MATLAB offers a wealth of built-in math functions that can be quite helpful for many computational problems Elementary MATLAB functions ( help elfun ) –Trigonometric functions –Exponential functions –Complex functions –Rounding and remainder functions Specialized MATLAB functions ( help specfun ) –Specialized math functions –Number theoretic functions –Coordinate transformations WE can write of own functions, also!

Fall 2006AE6382 Design Computing25 Practice: Matlab Expressions Write MATLAB expressions for the following:

Fall 2006AE6382 Design Computing26 Summary Topics Memory Numbers on a computer Precision and accuracy Numeric operators Precedence Writing complicated expressions.

Fall 2006AE6382 Design Computing27 Lecture references online Computer memory Numeric representation at.html 1/NUMCOMPGD/ncg_goldberg.html node4.html Standards

Fall 2006AE6382 Design Computing28 MATLAB commands used format rat format short format long clear who whos realmin realmax eps help arith help slash help ops help format