Why 0.1 Does Not Exist In Floating-Point
exploringbinary.com
exploringbinary.com
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.
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.)
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...
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 (aa + bb) == 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.
One of my favorite sort of problem was one that could easily be solved if you could handle arithmetic with integers larger than 32/64 bit types.
Same case with 1/10 to binary.
Discussion Over. How does this story have any up votes?
A lot of people, when they're starting out, have no idea how computers store digits. I can easily store 1/3 in decimal - 0.3 with a bar over the 3. But, as the article points out, computers don't store bars.
But did you learn that on the first day that you sat down at a computer?
The article opens with a good summary of the problem:
Questions [about floating point issues] are asked every day, on online forums like stackoverflow.com.
The answer is that most decimals have infinite representations in binary.(There's no need to be aggressive about it!)
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.ht...
I even had Ph.D. TAs in college with the same attitude.
Unbelievable.
For IEEE754 64-bit doubles, the interval containing 0.1 is: [0.099999999999999991673327315311, 0.100000000000000005551115123126]
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.
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.
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.