Learning Scientific Programming with Python (2nd edition)
P10.1.1: Heron's formula
Question P10.1.1
Heron's formula for the area of a triangle (as used in Example E2.3): $$ A = \sqrt{s(s-a)(s-b)(s-c)} \;\mathrm{where}\;s = \textstyle\frac{1}{2}(a+b+c). $$ is inaccurate if one side is very much smaller than the other two ("needle-shaped" triangles). Why? Demonstrate that the following reformulation [1] gives a more accurate result in this case by considering the triangle with sides $(10^{-13}, 1, 1)$ which has the area $5 \times 10^{-14}$. $$ A = \frac{1}{4}\sqrt{(a+(b+c))(c-(a-b))(c+(a-b))(a+(b-c))}, $$ where the sides have been relabelled so that $a \ge b \ge c$.
What happens if you rewrite the factors in this equation to remove their inner parentheses? Why?
References
- This formula is due to William Kahan, one of the designers of the IEEE-754 floating point standard.