HP 15c User Manual

Page 163

Advertising
background image

Appendix: Accuracy of Numerical Calculations

163

Example 7: The Angle in a Triangle. The cosine law for triangles says

r

2

= p

2

+ q

2

– 2pq cos θ

for the figure shown below. Engineering and scientific calculations often require that the
angle θ be calculated from given values p, q, and r for the length of the triangle's sides. This
calculation is feasible provided 0 < p q + r, 0 < q p+r, and 0 ≤ rp+q, and then

0 ≤ θ = cos

-1

(((p

2

+q

2

) −r

2

)/(2pq)) ≤ 180°;

Otherwise, no triangle exists with those side lengths, or else θ = 0/0 is indeterminate.

The foregoing formula for θ defines a function θ = f(p, q, r) and also in a natural way, a
program F(p, q, r) intended to calculate the function. That program is labeled "A" below,
with results F

A

(p, q, r) tabulated for certain inputs p, q, and r corresponding to sliver-shaped

triangles for which the formula suffers badly from roundoff. The numerical unreliability of
this formula is well known as is that of the algebraically equivalent but more reliable formula
θ = f(p, q, r) = 2 tan

-1

)

/(cs

ab

, where s = (p + q + r)/2, a = s − p, b = s − q, and c = s − r.

Another program F(p, q , r) based upon this better formula is labeled "B" below, with results
F

B

(p, q, r) for selected inputs. Apparently F

B

is not much more reliable than F

A

.

Most of the

poor results could be explained by backward error analysis if we assume that the calculations
yield F(p, q, r) = f(p + δp, q + δq, r + δr) for unknown but small perturbations satisfying
|δp| < 10

-9

|p|, etc. Even if this explanation were true, it would have perplexing and

disagreeable consequences, because the angles in sliver-shaped triangles can change
relatively drastically when the sides are perturbed relatively slightly; f(p, q, r) is relatively
unstable for marginal inputs.

Actually the preceding explanation is false. No backward error analysis could account for the
results tabulated for F

A

and F

B

under case 1 below unless perturbations δp, δq, and δr were

allowed to corrupt the fifth significant digit of the input, changing 1 to 1.0001 or 0.9999.
That much is too much noise to tolerate in a 10-digit calculation. A better program by far is
F

C

, labeled "C" and explained shortly afterwards.

The three bottom lines in the table below show results for three programs "A", "B", and "C"
based upon three different formulas F(p, q, r) all algebraically equivalent to

θ = f(p, q, r) = cos

-1

((p

2

+ q

2

r

2

)/(2pq)).

Advertising