Due: September 11, 2008
CS 257 (Luke Olson): Homework #2 Solutions
Problem 1
[Range Reduction] We are developing code for a financial firm and are required to compute
tan
(10
n
) for
various
n
as a component of our financial model. Use range reduction to show how to compute
tan
(10
n
) for
small
n
(
n <
5). What happens for large
n
(
n
= 20, 50, 100)? Hint: (Think of the role of the less significant
digits in a number, when that number is multiplied by a large number). Hand in pseudocode or matlab code
to compute
tan
(10
4
) and discuss in a couple of sentences the problem of computing
tan
(10
20
).
Solution
The
tan
function is
π
periodic. To use range reduction, we have to subtract off integer multiples
of
π
, until the value lies in the range 0 to
π
. Using this strategy to compute
tan
(10
n
), we find
an integer
k
such that 0
≤
(10
n

kπ
)
< π
. Assume we want to calculate (10
n

kπ
) up to two
decimal places of accuracy , for
n <
5. Since
k
≤
10
4
, the digit of
π
that is six places to the
right of the decimal point may be two places to the right of the decimal point in
kπ
. Therefore
we need to use a value of
π
which is accurate up to 6 decimal places.(Using matlab value of pi
is fine too. Thats accurate upto 15 decimal digits).
Listing 1: Code to compute
tan
(10
4
)
1
2
%Value of \pi accurate upto 6 decimal places
3
mypi = 3.141593;
4
x = 10ˆ4;
5
%Subtract off integer multiples
6
while
(x > mypi)
7
x = x  mypi;
8
end
9
%calcute the tan now
10
tan_x =
tan
(x);
11
fprintf
(
’tan(10ˆ4)
= tan(%g) = %g\n’
, x,tan_x);
As
n
gets large, we need more and more digits of accuracy of
π
to compute (10
n

kπ
) up to desired
accuracy. In double precision, we can have 52 bits for representation of
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Olson
 Luke Olson

Click to edit the document details