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?



1 Answer 1


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
  • 3
    $\begingroup$ 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. $\endgroup$ Mar 31, 2012 at 2:24
  • $\begingroup$ I would actually propose this: $Post = (# /. HoldPattern[Times[0., x : __ : 1]] :> Times[0, x]) &. $\endgroup$ Mar 31, 2012 at 2:32
  • $\begingroup$ @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? $\endgroup$
    – Mr.Wizard
    Mar 31, 2012 at 2:57
  • 2
    $\begingroup$ @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. $\endgroup$
    – Andy Ross
    Mar 31, 2012 at 4:05
  • $\begingroup$ Thanks for the answers. I can't try anything until Monday, but I really appreciate the quick response! $\endgroup$
    – Michael
    Mar 31, 2012 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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