tags:

views:

3845

answers:

8

If I execute the following expression in C#:

double i = 10*0.69;

i is: 6.8999999999999995. Why?

I understand numbers such as 1/3 can be hard to represent in binary as it has infinite recurring decimal places but this is not the case for 0.69. And 0.69 can easily be represented in binary, one binary number for 69 and another to denote the position of the decimal place.

How do I work around this? Use the decimal type?

+75  A: 

Because you've misunderstood floating point arithmetic and how data is stored.

In fact, your code isn't actually performing any arithmetic at execution time in this particular case - the compiler will have done it, then saved a constant in the generated executable. However, it can't store an exact value of 6.9, because that value cannot be precisely represented in floating point point format, just like 1/3 can't be precisely stored in a finite decimal representation.

See if this article helps you.

Jon Skeet
or http://www.codinghorror.com/blog/archives/001266.html
kenny
I might be showing my naivety, but why doesn't the framework work around this and hide this problem from me and give me the right answer,0.69!!!
Dan
@Dan: Because you might not *mean* 0.69. You might mean any of the other many values which would be represented by the same bit pattern. If you want to represent values which are fundamentally decimal in nature, you should use the `decimal` type to start with. Note that choosing the "output" here is a matter of working out how to convert an instance of a particular type into a *text* format... that's somewhat separate from deciding which type to use in the first place.
Jon Skeet
Dan: because "double" is computerese for "I'd rather have it done fast than done right."
Robert L
@Jon: Maybee you should consider adding your name to the article. It gets additional credibility that way. And maybe also add an date that it was written (in case binary floating point implementation changes in future :)).
Petar Repac
I could possibly add my name at the bottom of all my articles. Will consider it. As for dating it - I don't think that's necessary.
Jon Skeet
+6  A: 

For the same reason that 1 / 3 in a decimal systems comes out as 0.3333333333333333333333333333333333333333333 and not the exact fraction, which is infinitely long.

erikkallen
+8  A: 

On this sort of issue, this Wikipedia article is a must-read.

Robert L
another good reference is: http://docs.sun.com/source/806-3568/ncg_goldberg.html
redtuna
+19  A: 

why doesn't the framework work around this and hide this problem from me and give me the right answer,0.69!!!

Stop behaving like a manager, and accept that computers, though cool and awesome, have limits. In your specific case, it doesn't just "hide" the problem, because you have specifically told it not to. The language (the computer) provides alternatives to the format, that you didn't choose. You chose double, which has certain advantages over decimal, and certain downsides. Now, knowing the answer, you're upset that the downsides don't magically disappear.

As a programmer, you are responsible for hiding this downside from managers, and there are many ways to do that. However, the makers of C# have a responsibility to make floating point work correctly, and correct floating point will occasionally result in incorrect math.

So will every other number storage method, as we do not have infinite bits. Our job as programmers is to work with limited resources to make cool things happen. They got you 90% of the way there, just get the torch home.

Russell Steen
Well admittedly I don't have a deep understanding of this. But I was thinking there must be some sort of work around to this, such as using some alternate notation, it seems to be the number 69 its easily storable in binary, as well as the position of a decimal point.
Dan
@Dan: and that, essentially (I believe), is what a Decimal does. So yes, you should have used that if you wanted an exact answer.
Dan Tao
(much confusion over Dan's) - Yes Decimal does solve that particular problem. It adds some of it's own. Without knowing the details of the problem, it's hard to say which is "Correct" for you. It may very well be that the "correct" solution to your problem is to store a Double and round it off for your users.
Russell Steen
@Dan: The decimal place is stored in binary. So it comes after a position like 1/2, 1/4, 1/8, 1/16, 1/32, etc.
280Z28
+5  A: 

but why doesn't the framework work around this and hide this problem from me and give me the right answer,0.69!!!

Because you told it to use binary floating point, and the solution is to use decimal floating point, so you are suggesting that the framework should disregard the type you specified and use decimal instead, which is very much slower because it is not directly implemented in hardware.

A more efficient solution is to not output the full value of the representation and explicitly specify the accuracy required by your output. If you format the output to two decimal places, you will see the result you expect. However if this is a financial application decimal is precisely what you should use - you've seen Superman III (and Office Space) haven't you ;)

Note that it is all a finite approximation of a infinite range, it is merely that decimal and double use a different set of approximations. The advantage of decimal is it produces the same approximations that you would if you were performing the calculation yourself. For example if you calculated 1/3, you would eventually stop writing 3's when it was 'good enough'.

Clifford
+5  A: 

And 0.69 can easily be represented in binary, one binary number for 69 and another to denote the position of the decimal place.

I think this is a common mistake - you're thinking of floating point numbers as if they are base-10 (i.e decimal - hence my emphasis).

So - you're thinking that there are two whole-number parts to this double: 69 and divide by 100 to get the decimal place to move - which could also be expressed as:
69 x 10 to the power of -2.

However floats store the 'position of the point' as base-2.

Your float actually gets stored as:
68999999999999995 x 2 to the power of some big negative number

This isn't as much of a problem once you're used to it - most people know and expect that 1/3 can't be expressed accurately as a decimal or percentage. It's just that the fractions that can't be expressed in base-2 are different.

Keith
A: 

7.5 * 8.3 gives 62.25000000000001 in Java/Windows. The same computation gives the correct 65.25 in g++/Ubuntu. I'm just suggesting that the claim that you'd have this problem no matter what language or platform is not entirely correct. I've no deep insights as to why the difference is there.

Student4Life
No, you'll get the same 62.250000000000007 with g++ on Ubuntu, if only you print it out with a wider format (you see - your Java also truncated the output, the actual double value is wider).
SK-logic
@Student4Life - deep insight is available at http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary/1089026#1089026.
mtrw
A: 

This is because the specification was not really designed for computers which is kinda sad. Computers supposed to build for computing, basically for doing math. The fact that specification was correctly implemented is an excuse for not doing the right thing, which can be done easily by most of the humans using pen and pencil: do this computations without epic fail.

mark