Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is the best way to explain floating point rounding issue to customers?

I know

http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html

as well as the entries in the C++ FAQ and various other pages aimed at developers and scientists, but is there a web page, article or explanation, aimed at "regular" customers with limited mathematical or scientific background? (for which the above references fall flat).

If it were maintained or coming from a well-known and well-recognized institution or corporation, all the better, given that, as some of you might have experienced, it can be a little complicated to explain that yourself.

share|improve this question
1  
I wouldn't bother... –  John Shaft May 17 '11 at 7:53
1  
This is a really fantastic question, +10 if I could. A frequent problem for developers. –  Cody Gray May 17 '11 at 11:21
1  
It's not a problem of detail, it's answering f.i. why does adding what looks at 2 decimal digits precision numbers, you end up with a 5.9999999 and not a 6, and why you have to specify rounding precision when it's "obvious" the result shouldn't have more than two decimal digits. Or why sometimes 2 minus 2 isn't always zero, and not look like a bamboozling fool while doing so. –  Eric Grange May 17 '11 at 11:56
4  
@Eric Grange : if your customers consider those precision issues as a bug, then, it is a bug and you have to find a way to fix it (maybe by not using float). They don't care about where this precision issue comes from. They don't care about how your software works. They just want it to work. –  David May 17 '11 at 12:14
2  
@Eric: The use of floating-point is an implementation detail. I repeat my question, which asks for not something explained in the question, and which i have evidently not made clear (apologies): what's the context, and why are you discussing the use of floating point with a customer? –  Tom Anderson May 17 '11 at 16:46

6 Answers 6

I find a simple way to explain this is to demonstrate it. Discuss how dividing x by a number, then multiplying by the same number should return you to x again - get the customer to agree that this should always be the case. Then do the old (100 / 3) * 3 on a calculator; show that the value doesn't, as you would expect, return to 100. When most people see apparently simple maths "breaking down", then tend to 'get' the danger of floating point numbers where accuracy is important (although in an intuitive way, rather than to the low level the article you point to goes into).

Unfortunately most half-decent calculators (certainly all the scientific ones I've seen, and more than a few basic ones) nowadays are able to handle this - I presume they're storing extra digits beyond what can be displayed and rounding - so do check how clever your calculator is before you do it in front of your customer.

share|improve this answer
1  
Yes, almost all calculators are storing at least 2 extra digits, so you have to add a few multiplies in the mix, which muddies the explanation, and IME makes them think you're trying to fool them. Square root requires less operations, but square root is already outside the everyday realm of the regular customers. –  Eric Grange May 17 '11 at 7:56
2  
@Scott I tried on a few calculators here, none exhibited issues with (100/3)*3, even (100/3)*3-100 didn't exhibit issues.... Excel gets it right too. –  Eric Grange May 17 '11 at 8:05
5  
Take money as an example, that has a perfect limited precision. Explain you divide up one dollar, then every person gets 33 cents and one penny is lost in rounding. Anyone can relate to that. –  Inca May 17 '11 at 13:00
1  
Don't bother with the calculator. Divide 1 by 3 on paper, keeping three significant digits. –  David Thornley May 17 '11 at 14:00
5  
@omegacentauri if you think that explanation helps, I'm guessing you don't talk to customers often. –  jhocking May 17 '11 at 15:00

I don't think there are shortcuts. You have to either:

  • Understand what floating point is and how it behaves.

Or, if that's too much required, the you have to just:

  • Accept that the computer won't give you exact numeric results.

Maybe an example with irrational numbers helps (even though floating point issues apply to rational numbers as well): sqrt(2) ~ 1.414. Then 1.414^2 = 1.999396. No matter how many digits you take, you'll never get quite back to the original 2. Ok, 4 significant digits correct may be acceptable, but then consider what happens when this kind of "rounding errors" accumulate. That's where the real danger is.

share|improve this answer
1  
I personally know and understand, but for some people "floating point" is already an alien term, so you need more than mathematical or scientific explanation to explain that what they can calculate right in their head, their expensive computers and software has trouble getting right ;) Also square root is outside the everyday realm of regular customers. –  Eric Grange May 17 '11 at 7:54

Floating point numbers in computers use binary, so just like we have a number system with a ones, tens, hundreds, and tenths, hundredths columns, floating point numbers in computers actually have a ones, twos, fours, and halves, quarters, and eighths columns. If the customer is familiar with feet/inches, then remind them of how you typically use base-2 fractions of an inch for measurement.

Now try to store 10 cents as a combination of halves, quarters, eighths of a dollar. It just doesn't work:

.00011001100110011 . . . (repeats infinitely)

It's the same as taking a standard imperial measuring tape and trying to measure one tenth of an inch. You can't do it accurately. There is no representation of 1/10 as X/Y where X and Y are whole numbers and Y is a power of 2.

That's why we have the decimal data types that use 4 bits to store each decimal digit, so we're back to base 10 representation. The trade-off is in space and performance (about a 100% performance hit, from what I've read).

share|improve this answer

First, determine what they're complaining about. Financial transactions have to be done precisely, with the right number of decimal places and the right rounding rules. This typically means maintaining integral numbers of currency units and making sure the arithmetic is done right.

Alternatively, they may be complaining about overexact displays, and reducing the number of significant digits output may be all that's necessary.

For numbers in general, you can always try to come up with a three-digit decimal x such that x * 3 is 10. That shows the basic principles.

There are two remaining problems. One is that certain numbers can be expressed exactly in decimal but not binary (3.15, say). That's going to be hard to explain to non-technical people, and your best bet is to try to avoid it by not providing enough significant digits for it to show up. The other is the customer who knows a little bit, enough to know that computer arithmetic isn't always exact and not enough to realize that decimal arithmetic isn't always exact. I have argued with a few of those, and have nothing useful to report.

share|improve this answer

Tell them that just like their bank account cannot hold 4.4423425908459032890413... dollars (it's either $4.44 or $4.45, nothing in between), the computer cannot easily store a number with arbitrary precision. Imperfections of storage lead to imperfections of computations.

(It's slightly cheating, but should give them an idea of what the problem is.)

share|improve this answer
2  
Alas, that explanation doesn't work, as the precision issue can happen when summing up numbers that all have only two digits of precision to begin with. –  Eric Grange May 17 '11 at 8:07
    
Two decimal digits. Yes, I agree, an inquisitive customer will spot holes in it. But then you can hit them with the discussion of binary representation -- they asked for it ;-) –  quant_dev May 17 '11 at 8:21
    
Well, already trying to explain floating point they IME immediately start thinking you're trying to bamboozle them, which is something that could be alleviated if it was coming in simple, understandable terms, or from a well-know institution or corporation. :) –  Eric Grange May 17 '11 at 8:33
    
@Eric Math is hard, let's go play baseball :P –  quant_dev May 17 '11 at 8:36
    
Ask whether it's more precise to measure something to the nearest 1/10", or the nearest millimeter. The latter is more precise, but objects which are a precise multiple of 0.1" will not be a precise multiple of 1mm unless they are also a precise multiple of 5" (precisely 127mm). Adding the size of two 2.54mm objects which are measured to the nearest 0.1" will yield a combined size of 0.2"; adding together the sizes rounded to the nearest millimeter will yield 6mm even though the actual size should be 5.08mm. –  supercat Jul 31 '12 at 22:59

When doing calculations computers usually use approximations to numbers (like rather than using 1000000.7 they use 1000000) because using approximations is much faster. The problem with that is that when you do calculations with approximations you get approximations back. Usually that works pretty well, but sometimes it leads to unexpected results.

share|improve this answer
    
I don't really understand what you're saying here. "Because using approximations is much faster"? Sometimes integer arithmetic is at least as fast, and that's precise. Sometimes there's no alternative (as in printing out the square root of 2). –  David Thornley May 17 '11 at 14:04
    
Well you try explaining to a marketing guy why computers can't actually represent irrational numbers, or basically any number in the grand scheme of things (oh and then you might want to give him a quick lecture on the irrationality of \pi: something on Fourier series might be cute). Approximate is a word that people can understand. You are approaching this from the point of view of someone who knows that all numbers are not equal. –  user23157 May 17 '11 at 19:56

Your Answer

 
discard

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.