Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 15, 9am. Room GOO247. Assn 3 solution is posted.

2 Spring 2006CISC101 - Prof. McLeod2 Last Time Variable Scope and Lifetime Sorting

3 Spring 2006CISC101 - Prof. McLeod3 Today Searching –Sequential search –Binary search Source and effects of round-off error. Start working on assignment? Exam prep (mostly Thursday).

4 Spring 2006CISC101 - Prof. McLeod4 Searching Sequential search pseudocode: Loop through array starting at the first element until the value of target matches one of the array elements. If a match is not found, return –1.

5 Spring 2006CISC101 - Prof. McLeod5 Sequential Search As code: public int seqSearch(int[] A, int target) { for (int i = 0; i < A.length; i++) if (A[i] == target) return i; return –1; }

6 Spring 2006CISC101 - Prof. McLeod6 Sequential Search, Cont. Works on any kind of dataset. Finds the first matching element – does not say anything about how many matches there are! Best case – one comparison. Worst case (target not found) – n comparisons (n is the same as A.length. On average - n/2 comparisons.

7 Spring 2006CISC101 - Prof. McLeod7 Searching in an Ordered Dataset How do you find a name in a telephone book?

8 Spring 2006CISC101 - Prof. McLeod8 Binary Search Binary search pseudocode. Only works on ordered sets: a)Locate midpoint of array to search. b) Determine if target is in lower half or upper half of array. If in lower half, make this half the array to search. If in upper half, make this half the array to search. Loop back to step a), unless the size of the array to search is one, and this element does not match, in which case return –1.

9 Spring 2006CISC101 - Prof. McLeod9 Binary Search – Cont. public int binSearch (int[] A, int key) { int lo = 0; int hi = A.length - 1; int mid = (lo + hi) / 2; while (lo <= hi) { if (key < A[mid]) hi = mid - 1; else if (A[mid] < key) lo = mid + 1; else return mid; mid = (lo + hi) / 2; } return -1; }

10 Spring 2006CISC101 - Prof. McLeod10 Binary Search – Cont. For the best case, the element matches right at the middle of the array, and the loop only executes once. For the worst case, key will not be found and the maximum number of loops will occur. Note that the loop will execute until there is only one element left that does not match. Each time through the loop the number of elements left is halved, giving the progression below for the number of elements:

11 Spring 2006CISC101 - Prof. McLeod11 Binary Search – Cont. Number of elements to be searched - progression: The last comparison is for n/2 m, when the number of elements is one (worst case). So, n/2 m = 1, or n = 2 m. Or, m = log 2 (n). So, the algorithm loops log(n) times for the worst case.

12 Spring 2006CISC101 - Prof. McLeod12 Binary Search - Cont. Binary search with log(n) iterations for the worst case is much better than n iterations for sequential search! Major reason to sort datasets!

13 Spring 2006CISC101 - Prof. McLeod13 From Before: A float occupies 4 bytes A double occupies 8 bytes of memory (A byte is 8 bits, where a bit is zero or one.)

14 Spring 2006CISC101 - Prof. McLeod14 Storage of Real Numbers 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.

15 Spring 2006CISC101 - Prof. McLeod15 Storage of “Real” or “Floating-Point” Numbers - Cont. Consider 0.1: (0.1) 10 = (0.0 0011 0011 0011 0011 0011…) 2 What happens to the part of a real number that cannot be stored? It is lost - the number is truncated. The “lost part” is called the “roundoff error”.

16 Spring 2006CISC101 - Prof. McLeod16 Storage of “Real” or “Floating-Point” Numbers - Cont. Back to the storage of 0.1: Compute: And, compare to 1000. float sum = 0; for (int i = 0; i < 10000; i++) sum += 0.1F; System.out.println(sum);

17 Spring 2006CISC101 - Prof. McLeod17 Storage of “Real” or “Floating-Point” Numbers - Cont. Prints a value of 999.9029 to the screen. If sum is declared to be a double then the value: 1000.0000000001588 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. On the following charts, the x-axis is the counter, i, from the code shown above.

18 Spring 2006CISC101 - Prof. McLeod18 float Accumulation

19 Spring 2006CISC101 - Prof. McLeod19 double Accumulation

20 Spring 2006CISC101 - Prof. McLeod20 Roundoff Error 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|

21 Spring 2006CISC101 - Prof. McLeod21 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 1000. TypeAbsoluteRelative float0.09719.71E-5 double1.588E-101.588E-13

22 Spring 2006CISC101 - Prof. McLeod22 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.

23 Spring 2006CISC101 - Prof. McLeod23 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.

24 Spring 2006CISC101 - Prof. McLeod24 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 :

25 Spring 2006CISC101 - Prof. McLeod25 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.

26 Spring 2006CISC101 - Prof. McLeod26 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.

27 Spring 2006CISC101 - Prof. McLeod27 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.0E10 + 1.0E-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!

28 Spring 2006CISC101 - Prof. McLeod28 The Effect on Summations Taylor Series are used to approximate many functions. For example: For ln(2):

29 Spring 2006CISC101 - Prof. McLeod29 The Effect on Summations 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 10 8 - that’s a lot of iterations!

30 Spring 2006CISC101 - Prof. McLeod30 The Effect on Summations - Cont. On testing using a float, it took 33554433 iterations and 25540 msec to compute! ( sum no longer changing, value = 0.6931375) Math.log(2) = 0.6931471805599453 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 10 15 iterations would be required! (I didn’t try this one…) So, this series does not converge quickly!

31 Spring 2006CISC101 - Prof. McLeod31 The Effect on Summations - Cont. Here is another way to compute natural logs: Using x = 1/3 will provide ln(2).

32 Spring 2006CISC101 - Prof. McLeod32 The Effect on Summations - Cont. For float, this took 8 iterations and <1msec (value = 0.6931472). Math.log(2) = 0.6931471805599453 For double, it took 17 iterations, <1 msec to give the value = 0.6931471805599451 Using the Windows calculator ln(2) = 0.693147180559945309417232121458177 (!!) So, the use of the 17 iterations still introduced a slight roundoff error. The second version is much faster!!

33 Spring 2006CISC101 - Prof. McLeod33 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 and end up swamping out the inherent error.

34 Spring 2006CISC101 - Prof. McLeod34 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.


Download ppt "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."

Similar presentations


Ads by Google