Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. 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

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

I know

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

closed as primarily opinion-based by MichaelT, durron597, Ixrec, Snowman, gnat Aug 30 '15 at 16:28

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

I wouldn't bother... – John Shaft May 17 '11 at 7:53
This is a really fantastic question, +10 if I could. A frequent problem for developers. – Cody Gray May 17 '11 at 11:21
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
@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
@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

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
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
@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
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
Don't bother with the calculator. Divide 1 by 3 on paper, keeping three significant digits. – David Thornley May 17 '11 at 14:00
@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
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

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

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

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
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


Ask them to write down the exact answer to two divided by 3.
Since the answer 'goes on for forever' you can point that out.

Using 1/3 would also work but 2/3 is perhaps a slightly better example as rounding gives you (e.g.) .6666667 whereas .3333333 looks like it can just be truncated.

share|improve this answer

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

Some calculations are done according to some legal rule. For example, if you want to calculate how much income tax has to be paid on a taxable annual income of €79.245,18 in Germany, there is only one correct answer. You get it right or you get it wrong. If you get it right, you don't need to explain how floating point arithmetic works. If you get it wrong, you don't need to explain how floating point arithmetic works, you have to fix your broken code.

Sometimes you display results that don't look right. For example, if you convert US$ 13,297.46 into UK£ with two decimal digits, and then convert that amount of UK£ back to US$, you might not get US$ 13,297.46 but US$ 13,297.45 or US$ 13,297.47. That has nothing to do with floating-point arithmetic. It's an unavoidable problem and you better be able to explain why it is unavoidable. (You should also know why the problem doesn't happen when you convert from UK£ to US$ and back).

There are other possible results that don't look right. If you convert numbers to percentages the percentages should add up to 100%, but they might not. If you display four percentages with two decimals, the four displayed percentages might add up to 99.99% or 100.01%. Nothing to do with floating-point arithmetic. Still you should be able to explain why.

Next, there are situations where careless use of floating-point arithmetic leads to inappropriate results. For example, a + b + c is usually not the same as b + c + a. If that causes a problem, there is nothing to explain, it's something that you fix.

share|improve this answer

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