
Why 0.1 Does Not Exist In Floating-Point  - wglb
http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/
======
lmkg
Representing 0.1 (or rather, 1/10) has the same problem in binary that
representing 1/3 has in decimal. Your denominator doesn't divide any power of
your radix, so your representation has to be either infinite, or inaccurate.
If you've ever tried to do complex decimal math by copying results like
0.6666667 into and out of a calculator, you've probably run into the same
issues.

~~~
SilasX
Yeah, I was surprised why it didn't mention that as being the same problem in
a different, more familiar form.

------
ChuckMcM
Hah! If Mike Cowlishaw was here he could give you a wonderfully eloquent
treatment on why binary floating point sucks. And he's actually done something
about it [1] with both a format and software to deal with decimal numbers.

Further the densely packed form gets very close to optimal in terms of bit
representation. Assume that 4 bits (BCD) is 'worst' case, you can represent
0-9 but you 'waste' the information space 'a-f' (in hex, 10-15 in decimal) but
3 bits only gives you 8 states. What you want is a total of 10 states which is
3.3125 bit's worth (best case) or 3 digits in 9.9375 bits, these formats give
you 3 digits in 10 bits which is pretty close.

Mike goes on to talk about how to build hardware that does floating point
operations on this stuff. I built some into an FPGA and it really is pretty
straight forward, especially if you do iterative multiply and divide.

Consider the interesting thing where you take 128 bits (two 64 bit words)
where you use 60 bits for the whole part (18 digits) and 60 bits for the
fractional part (18 digits) and you've got 8 bits left over for various non-
number entities (+inf, -inf, NaN, etc). Would be great for CAD package, or a
financial spreadsheet.

[1] <http://speleotrove.com/decimal/DPDecimal.html>

~~~
matthiasl
That was an interesting link, thanks. I liked other parts of the site too, for
instance

<http://speleotrove.com/decimal/>

Any idea what decimal floating point is used for?

(I have little to no idea about CAD or financial software.

I can see that fixed-point decimal with two decimal places is useful for
things where you want to be able to reason about rounding of cents.

I can also see that you'd want more than two decimal places for a lot of
calculations, for instance computing interest.

But decimal floating point...what's specified in such a way that you need to
use decimal floating point?

I checked the wikipedia decimal floating point page. Didn't see any concrete
examples there either.)

~~~
ChuckMcM
It is used in high end CAD systems where tolerances need to be exact, there
was also a wonderful decimal package for COBOL which was doing financial
computations. Unknown to many (most?) people some early IBM Mainframes [1]
were decimal machines.

If you work in 3D modeling you will be familiar with the effects of binary
floating point as 'gaps' between polygons where they shouldn't be. Sometimes
those gaps only appear at a certain scale because that is where the rounding
gets it wrong, or if you compensate by always rounding up you get overlaps or
texture issues.

[1]
[http://en.wikipedia.org/wiki/IBM_700/7000_series#Decimal_arc...](http://en.wikipedia.org/wiki/IBM_700/7000_series#Decimal_architecture_.287070.2F7072.2F7074.29)

------
ja27
It depends on the underlying representation. I know it's almost never anything
except IEEE 754 now, but it could be different.

When I was an undergrad doing the ACM programming contest, one year we had
this triangle problem where, given the length of the three sides, you had to
print whether it was an equilateral, isosceles, right, or not a triangle at
all. Lots of naive programmers (including me) just checked if (a _a + b_ b) ==
c*c for a right triangle. The kicker is, for the (secret) test data, that
worked fine if you used single-precision floats and failed if you used double-
precision floats.

That educated a whole region of ACM contest competitors on floating point
representations.

~~~
Evbn
How did the spec specify input format?

~~~
davidcuddeback
In the ACM programming contests, input formats are usually ASCII text, read
from STDIN or from a file.

------
adaml_623
You know how you can't store 1/3 in decimal. (I think I learnt that in primary
school)

Same case with 1/10 to binary.

Discussion Over. How does this story have any up votes?

~~~
kmfrk
Your comment is exactly why getting into programming for the first time is a
shit experience for many people.

I even had Ph.D. TAs in college with the same attitude.

Unbelievable.

~~~
kevhsu
This is the type of thing I learned in my first semester of undergrad as a
CompE. This information is not a revelation for 90% of people with programming
experience unless I'm greatly overestimating our collective knowledge.

~~~
adaml_623
kmfrk: You have a point. I guess my point was that _I_ don't think HN is a
venue for sharing articles aimed at beginners. But the upvotes prove me wrong
:) The article is pretty well written.

------
sbanach
Sure it does - in that any floating point scheme is a well defined partition
of the real line in the range [-FLT_MAX, +FLT_MAX] into 2^n intervals (less
two Infs, a load of NaNs and a spare zero), one of which absolutely contains
0.1

~~~
adaml_623
Perhaps you could prove your point by supplying 0.1 in a binary floating point
format. You can choose the standard.

~~~
sbanach
Sure adaml_623,

0x3dcccccd in IEEE 754-2008 (binary32) corresponds to the interval
(0.09999999776482582, 0.10000000521540642) - which contains 0.1.

Disclaimers: I worked this out with double precision, and if you care about if
the endpoints are inclusive/exclusive, the wikipedia article will help.

~~~
Evbn
X \elem S is not the same as X == S, in many important contexts.

~~~
sbanach
Agreed. I "proved my point" but I didn't do it "by supplying 0.1 in a binary
floating point format", because that wasn't what my point was.

------
millerc
I would be MUCH more interested to get some insight as to WHY a binary
numbering system, which can't represent accurately a number so trivial as 0.1,
have ever become ubiquitous for general-purpose floating-point applications.
Or why it's no worse than any other system.

Any taker?

The original article in itself is pretty interesting, but of very limited
appeal to the HN crowd. The visitors here being mostly technical types, anyone
with a Comp. Sci. or Comp. Eng. degree has already gone through those
computations many times over during their career.

------
kristianp
The exact way of representing of numbers like 1/10 and 1/3 is to use a
Rational type.

I'm not clear as to why this is a problem though, and where it can be
magnified even despite using rounding in your application.

