Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 12 Last time: –Efficient recursive and non-recursive sorts. –Analyzing the complexity of recursive.

Slides:



Advertisements
Similar presentations
Winter 2006CISC121 - Prof. McLeod1 Stuff Assn 5 is available and due Friday, 7pm. Winding down! Possible Remaining topics: –Shell and Radix sorts (not.
Advertisements

Roundoff and truncation errors
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Topics covered: Floating point arithmetic CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
ECIV 201 Computational Methods for Civil Engineers Richard P. Ray, Ph.D., P.E. Error Analysis.
Round-Off and Truncation Errors
Assembly Language and Computer Architecture Using C++ and Java
Level ISA3: Information Representation
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
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.
1 Error Analysis Part 1 The Basics. 2 Key Concepts Analytical vs. numerical Methods Representation of floating-point numbers Concept of significant digits.
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.
Binary Representation and Computer Arithmetic
Data Representation Number Systems.
Simple Data Type Representation and conversion of numbers
Numbers and number systems
Numeral Systems Subjects: Numeral System Positional systems Decimal
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Information Representation (Level ISA3) Floating point numbers.
Computer Arithmetic Nizamettin AYDIN
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
IT253: Computer Organization
Lecture 2 Number Representation and accuracy
Computing Systems Basic arithmetic for computers.
Lecture Overview Introduction Positional Numbering System
Number Systems Spring Semester 2013Programming and Data Structure1.
Data Representation in Computer Systems
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Winter 2006CISC121 - Prof. McLeod1 Stuff Assn 5 is available and due Monday, 7pm. Pre-exam tutorial time. (Exam is April 27). April 18, room TBA. Office.
Round-off Errors.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
CSC 221 Computer Organization and Assembly Language
Floating Point Arithmetic
MECN 3500 Inter - Bayamon Lecture 3 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
CS 160 Lecture 4 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Numbers in Computers.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Dr. ClincyLecture 2 Slide 1 CS Chapter 2 (1 of 5) Dr. Clincy Professor of CS Note: Do not study chapter 2’s appendix (the topics will be covered.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
Floating Point Representations
Data Representation COE 308 Computer Architecture
Programming and Data Structure
Data Representation.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Number Representation
Number Systems and Binary Arithmetic
Data Structures Mohammed Thajeel To the second year students
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
Approximations and Round-Off Errors Chapter 3
CISC124 Assignment 1 due this Friday, 7pm.
CISC124 Assignment 1 due this Friday, 7pm.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
Data Representation COE 308 Computer Architecture
Presentation transcript:

Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 12 Last time: –Efficient recursive and non-recursive sorts. –Analyzing the complexity of recursive methods. Today –Last lecture!

Summer 2007CISC121 - Prof. McLeod2 You Will Need To: Look at exercise 5 and assignment 5. If you wish to replace any of your assignment marks you can submit the “makeup” assignment on the day of the final exam.

Summer 2007CISC121 - Prof. McLeod3 Final Exam… On the 16 th, unless other arrangements have been made. Exam topics are listed off the course web site. Exambank has 18 exams from 1996 to I will not provide full exam solutions, but will be happy to discuss individual exam problems. Ana or Krista can supply extra tutoring if needed. Ask them if you want an exam prep tutorial.

Summer 2007CISC121 - Prof. McLeod4 Today Analyze the complexity of mergesort. Number representation and roundoff error. Movie! “Satisfaction” survey

Summer 2007CISC121 - Prof. McLeod5 Mergesort – “ aMergeSort ” Code Code for sorting arrays: public static void aMergeSort (int[] A) { aMergeSort(A, 0, A.length-1); } // end aMergeSort public static void aMergeSort (int[] A, int first, int last) { if (first < last) { int mid = (first + last) / 2; aMergeSort(A, first, mid); aMergeSort(A, mid + 1, last); aMerge(A, first, last); } // end if } // end aMergeSort recursive

Summer 2007CISC121 - Prof. McLeod6 Mergesort – “ aMerge ” Code private static void aMerge (int[] A, int first, int last) { int mid = (first + last) / 2; int i1 = 0, i2 = first, i3 = mid + 1; int[] temp = new int[last - first + 1]; while (i2 <= mid && i3 <= last) { if (A[i2] < A[i3]) { temp[i1] = A[i2]; i2++; } else { temp[i1] = A[i3]; i3++; } i1++; } // end while

Summer 2007CISC121 - Prof. McLeod7 Mergesort – “ aMerge ” Code - Cont. while (i2 <= mid) { temp[i1] = A[i2]; i2++; i1++; } // end while while (i3 <= last) { temp[i1] = A[i3]; i3++; i1++; } // end while i1 = 0; i2 = first; while (i2 <= last) { A[i2] = temp[i1]; i1++; i2++; } // end while } // end aMerge

Summer 2007CISC121 - Prof. McLeod8 Complexity of Mergesort Consider the aMergeSort code shown above: Suppose that the entire method takes t(n) time, where n is A.length. We want to know the big O notation for t(n). There are no loops in aMergeSort, just some constant time operations, the two recursive calls and the call to aMerge.

Summer 2007CISC121 - Prof. McLeod9 Complexity of Mergesort - Cont. What is the time function for aMerge ? There is some O(1) stuff and four loops that are O(n): So,

Summer 2007CISC121 - Prof. McLeod10 Complexity of Mergesort - Cont. So far, we have not made any mention of the state of the data. Does it make any difference if the data is in reverse order (worst case), random order (average case) or in order already (best case)? Express t(n) in a recursive expression:

Summer 2007CISC121 - Prof. McLeod11 Complexity of Mergesort - Cont. Assume that n is a power of 2: (It is easy enough to show that the proof still holds when n is not a power of two - but I’m not going to do that here).

Summer 2007CISC121 - Prof. McLeod12 Complexity of Mergesort - Cont. Substitute n/2 for n, to get t(n/2):

Summer 2007CISC121 - Prof. McLeod13 Complexity of Mergesort - Cont. Do the next unrolling, which will be n/2 2 : So, after i unrolling’s:

Summer 2007CISC121 - Prof. McLeod14 Complexity of Mergesort - Cont. This recursion stops when the anchor case, n  1 is encountered. This will occur when: Substituting this back in the equation on the previous slide:

Summer 2007CISC121 - Prof. McLeod15 Complexity of Mergesort - Cont. At the anchor case: Now the equation can be simplified to yield the big O notation, which indicates that t(n) is O(nlog(n)).

Summer 2007CISC121 - Prof. McLeod16 public static void quickSort (int[] A, int first, int last) { int lower = first + 1; int upper = last; swap(A, first, (first+last)/2); int pivot = A[first]; while (lower <= upper) { while (A[lower] < pivot) lower++; while (A[upper] > pivot) upper--; if (lower < upper) swap(A, lower++, upper--); else lower++; } swap(A, upper, first); if (first < upper - 1) quickSort(A, first, upper-1); if (upper + 1 < last) quickSort(A, upper+1, last); } // end quickSort(subarrays)

Summer 2007CISC121 - Prof. McLeod17 Complexity of Quicksort The worst case is when a near-median value is not chosen – the pivot value is always a maximum or a minimum value. Now the algorithm is O(n 2 ). However, if the pivot values are always near the median value of the arrays, the algorithm is O(nlog(n)) – which is the best case. (See the derivation of this complexity for merge sort). The average case also turns out to be O(nlog(n)).

Summer 2007CISC121 - Prof. McLeod18 Number Representation Binary numbers or “base 2” is a natural representation of numbers to a computer. As a transition, hexadecimal (or “hex”, base 16) numbers are also used. Octal (base 8) numbers are used to a lesser degree. Decimal (base 10) numbers are *not* naturally represented in computers.

Summer 2007CISC121 - Prof. McLeod19 Number Representation - Cont. In base 2 (digits either 0 or 1): r=2, a binary number: ( ) 2 = 1×2 5 +1×2 4 +0×2 3 +1×2 2 +0×2 1 +1×2 0 +1× ×2 -2 = =53.75 (in base 10) “r” is the “radix” or the base of the number

Summer 2007CISC121 - Prof. McLeod20 Number Representation - Cont. Octal Numbers: a base-8 system with 8 digits: 0, 1, 2, 3, 4, 5, 6 and 7: For example: (127.4) 8 = 1×8 2 +2×8 1 +7×8 0 +4×8 -1 =87.5

Summer 2007CISC121 - Prof. McLeod21 Number Representation - Cont. Hexadecimal Numbers: a base-16 system with 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F: For example: (B65F) 16 = 11× × × ×16 0 =

Summer 2007CISC121 - Prof. McLeod22 Number Representation - Cont. The above series show how you can convert from binary, octal or hex to decimal. How to convert from decimal to one of the other bases?: integral part: divide by r and keep the remainder. decimal part: multiply by r and keep the carry “r” is the base - either 2, 8 or 16

Summer 2007CISC121 - Prof. McLeod23 Number Representation - Cont. For example, convert to binary: So, is Divisor(r)DividendRemainder (quotient) most significant digit least significant digit

Summer 2007CISC121 - Prof. McLeod24 Number Representation - Cont. For the “ ” part: So, is is: ( ) 2 Multiplier(r)MultiplicandCarry (product)

Summer 2007CISC121 - Prof. McLeod25 Number Representation - Cont. Converting between binary, octal and hex is much easier - done by “grouping” the numbers: For example: ( ) 2 =(?) ( ) 8

Summer 2007CISC121 - Prof. McLeod26 Number Representation - Cont. Another example: (2C6B.F06) 16 =(?) 2 (2 C 6 B. F 0 6) 16 ( ) 2

Summer 2007CISC121 - Prof. McLeod27 From Before: Integer Primitive Types in Java For byte, from -128 to 127, inclusive (1 byte). For short, from to 32767, inclusive (2 bytes). For int, from to , inclusive (4 bytes). For long, from to , inclusive (8 bytes). A “byte” is 8 bits, where a “bit” is either 1 or 0.

Summer 2007CISC121 - Prof. McLeod28 Storage of Integers An “un-signed” 8 digit binary number can range from to is 0 in base is 1x x x2 2 + … + 1x2 7 = 255, base 10.

Summer 2007CISC121 - Prof. McLeod29 Storage of Integers - Cont. So, how can a negative binary number be stored? One way is to use the Two’s Complement system of storage. Make the most significant bit a negative number: So, the lowest “signed” binary 8 digit number is now: , which is -1x2 7, or -128 base 10.

Summer 2007CISC121 - Prof. McLeod30 Storage of Integers - Cont. Two’s Complement System: binarybase

Summer 2007CISC121 - Prof. McLeod31 Storage of Integers - Cont. For example, the binary number is 1x x x x2 7 = = -107 base 10 Now you can see how the primitive integer type, byte, ranges from -128 to 127.

Summer 2007CISC121 - Prof. McLeod32 Storage of Integers - Cont. Suppose we wish to add 1 to the largest byte value: This would be equivalent to adding 1 to 127 in base 10 - the result would normally be 128. In base 2, using two’s compliment, the result of the addition is , which is -128 in base 10! So integer numbers wrap around, in the case of overflow - no warning is given in Java!

Summer 2007CISC121 - Prof. McLeod33 Storage of Integers - Cont. An int is stored in 4 bytes using “two’s complement”. An int ranges from: to or to in base 10

Summer 2007CISC121 - Prof. McLeod34 Real Primitive Types For float, (4 bytes) roughly ±1.4 x to ±3.4 x to 7 significant digits. For double, (8 bytes) roughly ±4.9 x to ±1.7 x to 15 significant digits.

Summer 2007CISC121 - Prof. McLeod35 Storage of Real Numbers The system used to store real numbers in Java complies with the IEEE standard number 754. Like an int, a float is stored in 4 bytes or 32 bits. These bits consist of 24 bits for the mantissa and 8 bits for the exponent: mantissaexponent

Summer 2007CISC121 - Prof. McLeod36 Storage of Real Numbers - Cont. So a value is stored as: value = mantissa  2 exponent The exponent for a float can range from to 2 128, which is about to The float mantissa must lie between -1.0 and 1.0 exclusive, and will have about 7 significant digits when converted to base 10.

Summer 2007CISC121 - Prof. McLeod37 Storage of Real Numbers - Cont. The double type is stored using 8 bytes or 64 bits - 53 bits for the mantissa, and 11 bits for the exponent. The exponent gives numbers between and , which is about and The mantissa allows for the storage of about 16 significant digits in base 10. (Double.MAX_VALUE is: E308 )

Summer 2007CISC121 - Prof. McLeod38 Storage of Real Numbers - Cont. See the following web site for more info: Or: point_standard

Summer 2007CISC121 - Prof. McLeod39 Storage of Real Numbers - Cont. So, a real number can only occupy a finite amount of storage in memory. This effect is very important for two kinds of numbers: –Numbers like 0.1 that can be written exactly in base 10, but cannot be stored exactly in base 2. –Real numbers (like  or e) that have an infinite number of digits in their “real” representation can only be stored in a finite number of digits in memory. And, we will see that it has an effect on the accuracy of mathematical operations.

Summer 2007CISC121 - Prof. McLeod40 Roundoff Error Consider 0.1: (0.1) 10 = ( …) 2 What happens to the part of a real number that cannot be stored? It is lost - the number is either truncated or rounded (truncated in Java). The “lost part” is called the Roundoff Error.

Summer 2007CISC121 - Prof. McLeod41 Storage of “Real” or “Floating-Point” Numbers - Cont. Compute: And, compare to float sum = 0; for (int i = 0; i < 10000; i++) sum += 0.1; System.out.println(sum);

Summer 2007CISC121 - Prof. McLeod42 Storage of “Real” or “Floating-Point” Numbers - Cont. Prints a value of to the screen. If sum is declared to be a double then the value: is printed to the screen. So, the individual roundoff errors have piled up to contribute to a cumulative error in this calculation. As expected, the roundoff error is smaller for a double than for a float.

Summer 2007CISC121 - Prof. McLeod43 Roundoff Error – Cont. This error is referred to in two different ways: The absolute error: absolute error = |x - x approx | The relative error: relative error = (absolute error)  |x|

Summer 2007CISC121 - Prof. McLeod44 Roundoff Error - Cont. So for the calculation of 1000 as shown above, the errors are: The relative error on the storage of 0.1 is the absolute error divided by TypeAbsoluteRelative float E-5 double1.588E E-13

Summer 2007CISC121 - Prof. McLeod45 The Effects of Roundoff Error Roundoff error can have an effect on any arithmetic operation carried out involving real numbers. For example, consider subtracting two numbers that are very close together: Use the function for example. As x approaches zero, cos(x) approaches 1.

Summer 2007CISC121 - Prof. McLeod46 The Effects of Roundoff Error Using double variables, and a value of x of 1.0E-12, f(x) evaluates to 0.0. But, it can be shown that the function f(x) can also be represented by f’(x): For x = 1.0E-12, f’(x) evaluates to 5.0E-25. The f’(x) function is less susceptible to roundoff error.

Summer 2007CISC121 - Prof. McLeod47 The Effects of Roundoff Error - Cont. Another example. Consider the smallest root of the polynomial: ax 2 +bx+c=0: What happens when ac is small, compared to b? It is known that for the two roots, x 1 and x 2 :

Summer 2007CISC121 - Prof. McLeod48 The Effects of Roundoff Error - Cont. Which leads to an equation for the root which is not as susceptible to roundoff error: This equation approaches –c/b instead of zero when ac << b 2.

Summer 2007CISC121 - Prof. McLeod49 The Effects of Roundoff Error - Cont. The examples above show what can happen when two numbers that are very close are subtracted. Remember that this effect is a direct result of these numbers being stored with finite accuracy in memory.

Summer 2007CISC121 - Prof. McLeod50 The Effects of Roundoff Error - Cont. A similar effect occurs when an attempt is made to add a comparatively small number to a large number: boolean aVal = ((1.0E E-20)==1.0E10); System.out.println(aVal); Prints out true to the screen Since 1.0E-20 is just too small to affect any of the bit values used to store 1.0E10. The small number would have to be about 1.0E-5 or larger to affect the large number. So, keep this behaviour in mind when designing expressions!

Summer 2007CISC121 - Prof. McLeod51 The Effect on Summations Taylor Series are used to approximate many functions. For example: For ln(2):

Summer 2007CISC121 - Prof. McLeod52 The Effect on Summations – Cont. Since we cannot loop to infinity, how many terms would be sufficient? Since the sum is stored in a finite memory space, at some point the terms to be added will be much smaller than the sum itself. If the sum is stored in a float, which has about 7 significant digits, a term of about 1x10 -8 would not be significant. So, i would be about that’s a lot of iterations!

Summer 2007CISC121 - Prof. McLeod53 The Effect on Summations - Cont. On testing using a float, it took iterations and msec to compute! (sum no longer changing, value = ) Math.log(2) = So, roundoff error had a significant effect and the summation did not even provide the correct value. A float could only provide about 5 correct significant digits, tops. For double, about iterations would be required! (I didn’t try this one…) So, this series does not converge quickly, and roundoff error has a strong effect on the answer!

Summer 2007CISC121 - Prof. McLeod54 The Effect on Summations - Cont. Here is another way to compute natural logs: Using x = 1/3 will provide ln(2).

Summer 2007CISC121 - Prof. McLeod55 The Effect on Summations - Cont. For float, this took 8 iterations and <1msec (value = ). Math.log(2) = For double, it took 17 iterations, <1 msec to give the value = Using the Windows calculator ln(2) = (!!) So, the use of the 17 iterations still introduced a slight roundoff error.

Summer 2007CISC121 - Prof. McLeod56 Aside - Extended Precision in Windows

Summer 2007CISC121 - Prof. McLeod57 Numeric Calculations Error is introduced into a calculation through two sources (assuming the formulae are correct!): –The inherent error in the numbers used in the calculation. –Error resulting from roundoff error. Often the inherent error dominates the roundoff error. But, watch for conditions of slow convergence or ill-conditioned matrices, where roundoff error will accumulate or is amplified and end up swamping out the inherent error.

Summer 2007CISC121 - Prof. McLeod58 Numeric Calculations - Cont. Once a number is calculated, it is very important to be able to estimate the error using both sources, if necessary. The error must be known in order that the number produced by your program can be reported in a valid manner. This is a non-trivial topic in numeric calculation that we will not discuss in this course.

Summer 2007CISC121 - Prof. McLeod59 Real World Roundoff Error Disasters Arianne 5 Launch Patriot Missiles See the movie!

Summer 2007CISC121 - Prof. McLeod60 Patriot Missile Problem The Patriot’s tracking system used a 24 bit number to keep track of the number of tenth seconds passed since the tracking system was turned “on”. 0.1 in binary is If you only have 24 bits to store this number then the error is …. or in base 10

Summer 2007CISC121 - Prof. McLeod61 Patriot Missile Problem, Cont. So over 100 hours of operation: ×100×60×60×10=0.34 seconds out. A scud is moving a mach 5 = 1,676 metres per second, so the error is 0.34×1,676 = 570 metres.