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.
-with+-. – orome Nov 13 '12 at 16:05Chopon the result to get rid of the error. – murray Nov 13 '12 at 16:07TreeForm[x - y - z]. – orome Nov 13 '12 at 16:23stellarDayTextbookwas not consulted. I'd always assumed (I have no idea why) that Mathematica, in addition to performing the numeric calculation, also "checked" what symbolic information it had to see if a purely symbolic manipulation could be used to produce a different result. Here it would have shown that the expression in question was just a rearrangement of the definition ofstellarDayTextbook, and produced0. – orome Nov 13 '12 at 19:00a, b, cdon't have values then(a-c) - banda - c - bevaluate to the same, we should get the same result regardless of parentheses even if we give them values? This raises some interesting questions about the actual evaluation order. – Szabolcs Nov 13 '12 at 19:03Block[{a = 3.2, b = 2.1, c = 1.1}, TracePrint[(a - c) - b]]suggests that ifaandchave values,a - cis evaluated before the associativity rule is applied (I'm not arguing for this behaviour, just noticing it). – Szabolcs Nov 13 '12 at 19:17