Exercise 2: Tolerance Checks and Division Guards Near a Right Angle — Possible Solution ==================================================================== WHY THE TOLERANCE CHECK IS BETTER ------------------------------ This chapter verified directly that cos(pi/2), computed in standard double-precision floating point, is not exactly 0 - it's approximately 6.12*10^-17, a tiny but genuinely nonzero value. This happens because pi itself has no exact binary representation, so pi/2 as actually stored and passed to cos() is never quite the true mathematical pi/2. A check written as if cos(angle) == 0: would therefore almost never evaluate to true, even at an angle that is conceptually exactly 90 degrees - the object would fail to be recognized as being at a right angle essentially every time, producing a silent, hard-to-diagnose bug. A tolerance-based check, if abs(cos(angle)) < 1e-9:, correctly recognizes any value close enough to zero (including this chapter's own verified ~6.12*10^-17 result, which is far smaller than the 1e-9 threshold) as "effectively zero" for practical purposes, matching the tolerance-comparison principle Numerical Methods & Floating-Point Computation's own Chapter 1 established for exactly this kind of situation. WHY A DIVISION GUARD IS ALSO NEEDED ------------------------------ This chapter also verified that tan(pi/2) - which internally divides sin(angle) by cos(angle) - doesn't fail or raise an error near this same angle. Instead, because cos(pi/2) is that tiny nonzero ~6.12*10^-17 value rather than true zero, the division produces a huge but completely ordinary-looking finite number (~1.63*10^16), which could easily be mistaken for a valid, if extreme, physical result rather than recognized as a division that's effectively by zero. A physics engine that computes anything by dividing by cos(angle) (such as certain force or torque calculations that involve a tangent) needs an EXPLICIT guard - for example, checking abs(cos(angle)) against a small threshold before dividing - because the division will not fail loudly on its own; it will simply return a plausible-looking but physically meaningless huge number. WHY BOTH ISSUES SHARE THE SAME ROOT CAUSE ------------------------------ Both problems stem from the same fact verified in this chapter: values that are mathematically exactly zero at a right angle (cos(pi/2)=0 in the true, idealized unit circle) are only approximately zero in floating point. An exact-equality check misses this near-zero value entirely, and a division that assumes the denominator can't be zero misses it in the opposite way, silently producing a huge distorted result instead of a clean failure. WHY THIS WORKS AS AN ANSWER ------------------------------ The answer explains both the tolerance-check improvement and the division-guard requirement using this chapter's own two specific verified numbers (cos(pi/2)'s tiny nonzero value and tan(pi/2)'s huge finite result), rather than treating them as two unrelated concerns, and identifies the single shared root cause connecting them.