• On MovieTome: TRANSFORMERS 2 SPOILERS!
May 3, 2008 12:36 PM PDT

Beware of a bug in Excel when doing addition

Posted by Michael Horowitz
  • Print

If there is anything a computer should be able to do, it's compute. Apparently however, Excel 2002 and Excel 2007 have trouble with this, most basic, task.

Office-watch.com details a bug involving the addition of numbers with two decimal places. Simply put, Excel generates the wrong total. It comes close to the right answer, but at this point, it really should get the exact, completely correct answer - and it doesn't. See Excel SUM anomaly from April 29, 2008.

You may also want to verify that your copy of Excel has the necessary fix for another calculation bug described at office-watch.com in Excel's problem with 65,535 & 65,536 from September 2007.

Update: May 3, 2008. For those of you who don't read the comments Woody Leonhard left the following comment to this story. Basically what he wrote is a copy of a posting he did on his askwoody.com site called The Excel bug that isn't. Quoting Woody:

"...the problem has nothing to do with Excel. It's a congenital problem in the way computers represent decimal numbers with bits and bytes. Here's how I explained it last October, in Windows Secrets Newsletter: Excel works internally with binary numbers. It's therefore subject to all the myriad problems programmers encounter when they translate base-10 numbers into base-2 and back ... The number 0.1 can't be represented precisely in binary ... If you need precise decimal accuracy, you have to use a program that performs calculations in decimal. It's much slower than calculating in binary, but in some cases it's worth the effort. If you need to perform decimal arithmetic, take a look at an Excel add-in called RCOM, from Thomas Baier and Erich Neuwirth at the University of Vienna."

I respect Woody, having read his newsletter, and other writings of his, for a long time. But, on this point, I disagree with him. For one thing, I added the same numbers using the Calculator in Windows XP and got the totally correct answer, zero, not a number very close to zero. The Calc spreadsheet in Open Office also yielded exactly zero. For another, he is basically saying that computers can't do arithmetic. That adding 1 plus 1 may yield a number very close to 2, but not exactly 2 and that's the nature of the beast, so we should get over it.

Regardless of whether the computer internally uses base 2 numbers, or the Klingon numbering system, there is no excuse for a computer not to compute correctly. Perfectly correctly. Totally, 100% correctly. If you paid for Excel, you deserve perfect addition.

The article Woody links to at Wikipedia is about Floating point numbers. If Excel is, in fact, using floating point numbers internally, this is a mistake by Microsoft. A big one, if you ask me. Floating point numbers are not very accurate. All programmers know this, it has been true from the get-go. They are appropriate only for representing very large numbers. Using floating point numbers for decimal arithmetic on small numbers is a design mistake. One that the Calc spreadsheet and the Windows XP calculator don't make. I programmed applications in a mainframe environment for a decade and never once chose to use floating point numbers to represent anything. They were the wrong tool for the job.

Besides being mis-guided, Woody's comments scare me. They represent a mindset that I see all too often. Last year I wrote about Why Java can't do addition correctly which is another case of the same mindset. The designers of the Java language are very happy to let it add two numbers, get the wrong answer and continue running without raising an error condition. This is unacceptable, at least to me, and it should be unacceptable to you too.

And, just as an aside, IBM mainframes can calculate in decimal, natively.

See a summary of all my Defensive Computing postings.

Michael Horowitz is an independent computer consultant and the author of several classes on Defensive Computing. He is a member of the CNET Blog Network, and is not an employee of CNET. Disclosure.
Recent posts from Defensive Computing
Fixing bugs in the Flash Player yet again
Getting more battery power for your computer
Get an MSI Wind Netbook for only $349
Not interested in a Netbook computer? Consider the Honda Fit
Beware emails linking to blogspot.com
Add a Comment (Log in or register) 2 comments
by menotbug May 3, 2008 4:42 PM PDT
Woody?s no-bull news, tips and help for Windows and Office at http://www.askwoody.com/newscomments.php?newsid=2080 says "Sorry, but you can't lay this problem at Microsoft's door." "...That's absolutely correct. The numbers don't total to, precisely, zero. But the problem has nothing to do with Excel. It's a congenital problem in the way computers represent decimal numbers with bits and bytes. Here's how I explained it last October, in Windows Secrets Newsletter: Excel works internally with binary numbers. It's therefore subject to all the myriad problems programmers encounter when they translate base-10 numbers into base-2 and back. (Wikipedia has a good article on the inherent problems with floating point arithmetic in a decimal world.) The number 0.1 can't be represented precisely in binary. So when Excel multiples 850 by 77.1, Excel comes up with a number that's close to 65534.99999999995. It can't get precisely 65,535 -- a congenital defect. If you need precise decimal accuracy, you have to use a program that performs calculations in decimal. It's much slower than calculating in binary, but in some cases it's worth the effort. If you need to perform decimal arithmetic, take a look at an Excel add-in called RCOM, from Thomas Baier and Erich Neuwirth at the University of Vienna."
Reply to this comment
by harveybook May 4, 2008 7:58 AM PDT
I have programmed in various computer languages, including machine languages, since 1967; I have done binary, decimal and floating point calculations. I have never given back incorrectly rounded results to a user unless there were complex calculations requiring a great amount of significant digits in which case the end-user would realize the issue. I would never give back incorrectly rounded results to users that were mathematically challenged, and would certainly never do so in a widely-used product where the understanding of users varies widely. As far as Woody's example of multiplying 850 by 77.1, and Woody's comment of, "the number 0.1 can't be represented precisely in binary," the way I learned to do this was to multiply 850 by 771 and divide by 10. There would be no issue with 0.1 and would be no rounding problem, for if a computer could not represent the number 771 in binary, it should be sent to the junkyard. Computers were designed to help solve problems, not create new ones. Unless all Excel users could be taught to understand something that some of them are incapable of, the fact that rounding errors can occur, internal arithmetic of Excel must ensure that, where possible, rounding errors do not occur.
Reply to this comment
advertisement

In the news now

Apple's iPhone 2.2
hits the street

The latest software update offers several improvements to Google maps as well as wireless downloading for podcasts.



The big chill for holiday parties?

Tech companies faced with cost-cutting may not be canceling the annual festivities outright, but things are certainly being done differently this year.



Resource center from CNET News sponsors
Business. Ready.
Sony VAIO® Professional PCs.

Click Here!
A new grade in mobility demands a new kind of notebook. And Sony delivers.Tough, portable and featuring up to 7.5 hours of battery life! VAIO® Professional notebooks are built for business. Learn more.

Click Here!
Built tough for business.

Learn more about the rigorous quality testing Sony puts its notebooks through.

Protect your investment.

Find out why VAIO® tech support recently won a Laptop Editors' Choice Award, July 2008.

Long battery life.

Up to 7.5 hours of battery life! See how VAIO® PCs will keep you productive longer when on the road.

Travel light

Check out our ultraportable line-up, starting at 2.87 lbs.

PCs for every need.

Find out which VAIO® notebook is right for you.

About Defensive Computing

Michael Horowitz is an independent computer consultant and the author of several classes on Defensive Computing. He views Defensive Computing as taking steps, when things are running well, to avoid or minimize the inevitable problems down the road. It's about educating yourself to the level where you can make your own intelligent decisions about keeping your computers and data happy and healthy. If you depend on computers, yet are on your own, without an IT department or nearby nerd, this blog's for you. His personal web site is michaelhorowitz.com.

He is a member of the CNET Blog Network and is not an employee of CNET.

Disclosure.

Add this feed to your online news reader

Defensive Computing topics

advertisement
advertisement

Inside CNET News

Scroll Left Scroll Right