Homework #2: MA 402 Mathematics of Scientific Computing Due: Wednesday, January 26
Homework #2: MA 402 Mathematics of Scientific Computing Due: Wednesday, January 26
MA 402
Mathematics of Scientific Computing
Due: Wednesday, January 26
Problem 1. In MATLAB, the code (3*(1+eps) - 3)/eps will return the value
4. In exact arithmetic, the correct answer is 3. Carry out the floating point
arithmetic by hand, bearing in mind that the quantity must be rounded after
each individual binary operation. (Hint: only one of the operations will need to
be rounded; the other three are computed exactly.)
Problem 2 (Higham). Which of the following statements are true in IEEE
arithmetic, assuming that a and b are normalized floating point numbers and
that no overflow or underflow occurs? You should mostly rely on the principle
“fl(x y) is equal to the exact value of x y, rounded correctly.”
a) fl(a + b) = fl(b + a)
b) fl(b − a) = − fl(a − b)
c) fl(a + a) = fl(2 ∗ a)
d) fl((a + b) + c) = fl(a + (b + c))
Problem 3. The MATLAB p code hypot(x,y) (or Python math.hypot(x,y)
evaluates the quantity x + y 2 in a way that is careful to avoid overflow or
2
underflow.
a) Find an example where the code sqrt(x^2 + y^2) overflows but hypot(x,y)
does not.
b) Find an example where the code sqrt(x^2 + y^2) underflows but hypot(x,y)
does not.
c) Find an example where hypot(x,y) will overflow no matter how carefully
it is written.
1
Problem 4 (Higham). Two requirements that we might ask of√a routine for
√
computing
p x in floating point arithmetic are that the identities x2 = |x| and
( |x|)2 = |x| be satisfied. Barring overflow/underflow, it turns out that only
the first identity can be satisfied.
a) Exactly how many floating point numbers fall in the interval [1, 2]?
√
b) Approximately how many floating point numbers fall in the interval [1, 2]?
You do not have to be too precise.
c) Use the Pigeonhole Principle to show that there exist distinct floating
point numbers x and y such that sqrt(x) == sqrt(y) in floating point
arithmetic.
p
d) Why can the second identity ( |x|)2 = |x| not be satisfied in IEEE double
precision?
Problem 5. The number of ways to shuffle a standard deck of cards is 52
factorial, written as 52! and equal to 52 × 51 × · · · × 1 or
80658175170943878571660636856403766975289505440883277824000000000000.
t = 0.1
n = 1:10
e = n/10 - n*t.
What does this demonstrate about floating point arithmetic (in particular, as-
sociativity)? Explain.
Problem 7. A useful skill to develop is writing short programs in order to gain
insight into a problem or test a hypothesis. The MATLAB code rand(1,N)
(Python numpy.random.rand(1,N)) will generate a vector of length N whose
entries are uniformly distributed in [0, 1]. In addition, the code isequal(a,b)
(Python numpy.all(a==b)) will determine whether all corresponding entries of
two vectors are equal to one another.
Generate random test cases to examine at least one of the identities in either
Problem 2 or Problem 4. Submit your code separately, but report your results
along with the rest of the written homework.