Another disaster caused by numerical computing

Helen, a very clever girl, never makes any mistake on her homework. Her GPA used to be 4.3 until she got one miserable A due to a silly numerical computing error… it was a disaster.. a nightmare.. even now, she sometimes gets pissed off when she thinks about it. So, what happened to Helen last semester?

Last semester, she was working on her final project report on MS Excel 2007. The project report contained a lot of numerical values, both integers and rationals, so that she couldn’t check all the values manually. The first data set she got from her experiment was following:

Helen’s data

So far, it seems fine (really?). The data contained more than 200 rows so that she couldn’t validate if the values are correct or not. Later on, using this data, she calculated something more complicated and then finally made a super-awesome graph! Helen was so satisfied with her work because she spent 5 days to get the most accurate data she could achieve and then spent another whole day to write up this beautiful report with this awesome graph!

On the following day, she was presenting her final report in class. Well, in the middle of her presentation, somebody raised his hand up and asked,

“Hey, how did you get that number? You got 77.1 * 850 = 100,000, but I don’t think it’s correct. It should be less than 77,000 because 77.1 * 1,000 is only 77,000!”

Confused and puzzled, Helen said,

“Oh, well, I don’t know… Um… I just used MS Excel to do the calculations… I didn’t do it so I think it’s MS Excel’s bug..?”[1], [2]

Anyway, her presentation was not good at all and she did not get a good grade on her final project: ‘due to a careless mistake when validating the data’. Even if she did so well on the exams and other projects, she did not receive A+ because of her final presentation and errors on the data. Now her GPA is 4.28 and she is so sad.

Well, why is this happening? That’s because YOUR COMPUTER SOMETIMES DOES NOT REALLY KNOW HOW TO EXPRESS FLOATING NUMBERS IN BINARY NUMBERS! If you think about all the famous disasters caused by numerical errors, many of them can be categorized in one group: errors when dealing with floating numbers. If you check out the websites [1] and [2], you will find out why this bug was made (even on Microsoft Excel 2007, 2007! MS!).

Excel:

1.to surpass others or be superior in some respect or area; do extremely well: to excel in math.[3]

Personally, I hate to use float/double (in C++) kinds of primitive types because of this imperfection. Whenever I calculate something like 1/3 * 3, the result is not 1, but 0.999999999999999! This sometimes really bugs me down so that I have a weird habit now: try to avoid using any float-type data (what? how?). For instance, if you calculate Euclidean distance between two points, you will write something like this:

double dist = sqrt ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

If x1, x2, y1, and y2 are integers, I would rather write

int sqr_dist = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);

and keep this squared distance until I really need to calculate the actual value.

Another thing is, I always use some small constant ‘epsilon’ when I compare two non-integer values:

const double eps = 1.e-6; // 10^-6

bool same(double A, double B) { return abs(A-B) < eps; }

I never write something like:

if (A == B) // …

if A and B are not integer-type because 1.0 is different from 0.999999999999…!

After all, my point is, whoever realized the existence of irrational numbers[4], they made our computers and engineers suffer.. (since we can represent any rational as a pair of two integers, rationals are OK).


P.S. I hope this first posting can be a good reason for the professor to accept my late homework #1…?

[Sources]

[1] http://www.joelonsoftware.com/items/2007/09/26b.html

[2] http://blogs.msdn.com/excel/archive/2007/09/25/calculation-issue-update.aspx

[3] http://dictionary.reference.com/browse/excel

[4] http://en.wikipedia.org/wiki/Real_number#History

Posted in Topics: Education, General

Jump down to leave a comment.

Leave a Comment

You must be logged in to post a comment.



* You can follow any responses to this entry through the RSS 2.0 feed.