spring 2006cisc101 - prof. mcleod1 announcements assn 4 is posted. note that due date is the 12 th...
Post on 18-Jan-2018
217 Views
Preview:
DESCRIPTION
TRANSCRIPT
Spring 2006 CISC101 - Prof. McLeod 1
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.
Spring 2006 CISC101 - Prof. McLeod 2
Last Time• Variable Scope and Lifetime
• Sorting
Spring 2006 CISC101 - Prof. McLeod 3
Today• Searching
– Sequential search– Binary search
• Source and effects of round-off error.
• Start working on assignment?
• Exam prep (mostly Thursday).
Spring 2006 CISC101 - Prof. McLeod 4
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.
Spring 2006 CISC101 - Prof. McLeod 5
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;}
Spring 2006 CISC101 - Prof. McLeod 6
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.
Spring 2006 CISC101 - Prof. McLeod 7
Searching in an Ordered Dataset• How do you find a name in a telephone book?
Spring 2006 CISC101 - Prof. McLeod 8
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.
Spring 2006 CISC101 - Prof. McLeod 9
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;
}
Spring 2006 CISC101 - Prof. McLeod 10
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:
Spring 2006 CISC101 - Prof. McLeod 11
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.
m
nnnnn2
...,,2
,2
,2
, 32
Spring 2006 CISC101 - Prof. McLeod 12
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!
Spring 2006 CISC101 - Prof. McLeod 13
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.)
Spring 2006 CISC101 - Prof. McLeod 14
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.
Spring 2006 CISC101 - Prof. McLeod 15
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”.
Spring 2006 CISC101 - Prof. McLeod 16
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);
10000
1
1.0i
Spring 2006 CISC101 - Prof. McLeod 17
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.
Spring 2006 CISC101 - Prof. McLeod 18
float AccumulationProgress of float Roundoff Error
-1.20E-01
-1.00E-01
-8.00E-02
-6.00E-02
-4.00E-02
-2.00E-02
0.00E+00
2.00E-02
4.00E-02
0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
Spring 2006 CISC101 - Prof. McLeod 19
double AccumulationProgress of double Roundoff Error
-2.00E-11
0.00E+00
2.00E-11
4.00E-11
6.00E-11
8.00E-11
1.00E-10
1.20E-10
1.40E-10
1.60E-10
1.80E-10
0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
Spring 2006 CISC101 - Prof. McLeod 20
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|
Spring 2006 CISC101 - Prof. McLeod 21
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.
Type Absolute Relativefloat 0.0971 9.71E-5double 1.588E-10 1.588E-13
Spring 2006 CISC101 - Prof. McLeod 22
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.
)cos(1)( xxf
Spring 2006 CISC101 - Prof. McLeod 23
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.
)cos(1)(sin)()('
2
xxxfxf
Spring 2006 CISC101 - Prof. McLeod 24
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:
aacbbx
242
1
acxx 21
Spring 2006 CISC101 - Prof. McLeod 25
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.
acbbcx
4221
Spring 2006 CISC101 - Prof. McLeod 26
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.
Spring 2006 CISC101 - Prof. McLeod 27
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!
Spring 2006 CISC101 - Prof. McLeod 28
The Effect on Summations• Taylor Series are used to approximate many
functions. For example:
• For ln(2):
1
1)1()1ln(i
ii
ixx
...41
31
211)1()2ln(
1
1
i
i
i
Spring 2006 CISC101 - Prof. McLeod 29
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!
Spring 2006 CISC101 - Prof. McLeod 30
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!
Spring 2006 CISC101 - Prof. McLeod 31
The Effect on Summations - Cont.
• Here is another way to compute natural logs:
• Using x = 1/3 will provide ln(2).
0
12
1212
11ln
i
ixix
x
Spring 2006 CISC101 - Prof. McLeod 32
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!!
Spring 2006 CISC101 - Prof. McLeod 33
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.
Spring 2006 CISC101 - Prof. McLeod 34
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.
top related