Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently upgraded from Mathematica 5 to Mathematica 8 and I have a number of notebooks that no longer work correctly. The problems relate to the use of units. I don't need complicated unit conversions so I typically just tag values with symbols for the units. Under Mathematica 5, I could write the following (I understand that this example isn't physically meaningful):

3. in + 4. in - 7. in + 5. lb

And Mathematica would return

5. lb

Now, under Mathematica 8, I get

0. + 5. lb

This breaks a large number of notebooks. Can someone explain what is happening, and what if anything I can do to get the old behavior back?


share|improve this question
up vote 4 down vote accepted

Only precise zero coefficients are eliminated. Use Chop:

3. in + 4. in - 7. in + 5. lb // Chop
5. lb

Leonid admonished me for posting a method overloading Times. I didn't honestly expect anyone to use that and I think I made that pretty clear in the original post.

Nevertheless, here is a safer method that only affects uses of specified units:

units = {in, lb};

(# /: 0. # := 0 #) & /@ units;

3. in + 4. in - 7. in + 5. lb
5. lb
share|improve this answer
You have just committed a deadly sin (and you know about it): nothing can excuse overloading Times (or similar commands) globally. Why don't you use a local dynamic environment? I am afraid that your answer in its present form will lead to many lost souls. – Leonid Shifrin Mar 31 '12 at 2:24
I would actually propose this: $Post = (# /. HoldPattern[Times[0., x : __ : 1]] :> Times[0, x]) &. – Leonid Shifrin Mar 31 '12 at 2:32
@Leonid naughty, aren't I? :-> The problem with Post is that it only affects the output. If I remove the overloading method will you vote for Chop? – Mr.Wizard Mar 31 '12 at 2:57
@LeonidShifrin Though I completely agree that it is bad practice to overload arithmetic functions like Times the fan boy in me loves the fact that we can. Mathematica is like putty in the hands, leaving us free to experiment with it in ways that just aren't possible in most places. – Andy Ross Mar 31 '12 at 4:05
Thanks for the answers. I can't try anything until Monday, but I really appreciate the quick response! – Michael Mar 31 '12 at 15:44

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.