340 likes | 354 Views
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. Last Time. Variable Scope and Lifetime Sorting. Today. Searching Sequential search Binary search
E N D
Announcements • Assn 4 is posted. Note that due date is the 12th (Monday) at 7pm. (Last assignment!) • Final Exam on June 15, 9am. Room GOO247. • Assn 3 solution is posted. CISC101 - Prof. McLeod
Last Time • Variable Scope and Lifetime • Sorting CISC101 - Prof. McLeod
Today • Searching • Sequential search • Binary search • Source and effects of round-off error. • Start working on assignment? • Exam prep (mostly Thursday). CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod
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; } CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod
Searching in an Ordered Dataset • How do you find a name in a telephone book? CISC101 - Prof. McLeod
Binary Search • Binary search pseudocode. Only works on ordered sets: • 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. CISC101 - Prof. McLeod
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; } CISC101 - Prof. McLeod
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: CISC101 - Prof. McLeod
Binary Search – Cont. • Number of elements to be searched - progression: • The last comparison is for n/2m, when the number of elements is one (worst case). • So, n/2m = 1, or n = 2m. • Or, m = log2(n). • So, the algorithm loops log(n) times for the worst case. CISC101 - Prof. McLeod
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! CISC101 - Prof. McLeod
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.) CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod
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”. CISC101 - Prof. McLeod
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); CISC101 - Prof. McLeod
Storage of “Real” or “Floating-Point” Numbers - Cont. • Prints a value of 999.9029 to the screen. • If sumis declared to be a doublethen 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. CISC101 - Prof. McLeod
float Accumulation CISC101 - Prof. McLeod
double Accumulation CISC101 - Prof. McLeod
Roundoff Error • This error is referred to in two different ways: • The absolute error: absolute error = |x - xapprox| • The relative error: relative error = (absolute error) |x| CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod
The Effects of Roundoff Error • Using doublevariables, 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. CISC101 - Prof. McLeod
The Effects of Roundoff Error - Cont. • Another example. Consider the smallest root of the polynomial: ax2+bx+c=0: • What happens when ac is small, compared to b? • It is known that for the two roots, x1 and x2: CISC101 - Prof. McLeod
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 << b2. CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod
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 trueto 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! CISC101 - Prof. McLeod
The Effect on Summations • Taylor Series are used to approximate many functions. For example: • For ln(2): CISC101 - Prof. McLeod
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 108 - that’s a lot of iterations! CISC101 - Prof. McLeod
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 1015 iterations would be required! (I didn’t try this one…) • So, this series does not converge quickly! CISC101 - Prof. McLeod
The Effect on Summations - Cont. • Here is another way to compute natural logs: • Using x = 1/3 will provide ln(2). CISC101 - Prof. McLeod
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!! CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod
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. CISC101 - Prof. McLeod