I'm getting results that are sensitive to where I place parentheses with respect to operations that are associative1 (and should thus be insensitive to such placement). For example, if I define2
<< Units`; << PhysicalConstants`;
stellarDayTextbook = 1/(1/Day + 1/Convert[SiderealYear, Day])
and then calculate
1/stellarDayTextbook - 1/Day - 1/Convert[SiderealYear, Day]
I get precisely zero, as expected. But if I add parentheses
(1/stellarDayTextbook - 1/Day) - 1/Convert[SiderealYear, Day]
I get
-(3.03577*10^-17/Day)
What's causing this?
1. Remember, this isn't C, it's math: "subtraction" of $x$ is just the addition of $-x$. Check TreeForm.
2. I realize this isn't the definition of a "stellar day", but merely a textbook approximation. The distinction is not material to the question.
Answer
What you're seeing here is just the impreciseness of floating point arithmetic. It is important to remember that floating point operations are not associative or distributive even if the underlying mathematical operations are. A very simple example demonstrating the lack of associativity:
1. + (1.*^20 - 1.*^20)
(* 1. *)
(1. + 1.*^20) - 1.*^20
(* 0. *)
Much has been said and written about this topic over the years, so instead of repeating, I'll just link you to a good article to read on the subject: D. Goldberg, "What every computer scientist should know about floating-point arithmetic," ACM Comput. Surv. 23, 1 (March 1991), 5-48
Comments
Post a Comment