Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
Subtraction is associative? en.wikipedia.org/wiki/Associative_property – alancalvitti Nov 13 '12 at 16:01
@alancalvitti: Ok, ok: go ahead and replace all the - with +-. – raxacoricofallapatorius Nov 13 '12 at 16:05
4  
Just numerical roundoff error here. We know the order of evaluation for the parenthesized version, and clearly the order is different for the unparenthesized version. Use Chop on the result to get rid of the error. – murray Nov 13 '12 at 16:07
Sounds to me like @murray pretty much said it; it's just one of the vagaries of inexact arithmetic. – J. M. Nov 13 '12 at 16:11
4  
In any case, floating point operations are not necessarily associative/distributive, even if the underlying mathematical operations are. – rm -rf Nov 13 '12 at 16:26
show 11 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.