6 ms·
Correct. Not the bug of the floating point hardware. Floating point hardware which implements IEEE does what it is supposed to do. It's a "PEBKAC." Moreover, i
by acqq 14y ago
Correct. Not the bug of the floating point hardware. Floating point hardware which implements IEEE does what it is supposed to do. It's a "PEBKAC."
Moreover, if OP understood the subject a little better, he'd know that 123 is stored exactly. Specifically, 123 is in binary 1111011. When stored in IEEE format you'll see in the mantissa bits the exact match: 111011 (the first bit is implicit). You can check it here:
http://www.h-schmidt.net/FloatConverter/IEEE754.html http://www.h-schmidt.net/FloatConverter/IEEE754.html
OP was bitten by assuming that decimal parts have exact representation, whereas from .1 to .9 only .5 has.
Once he divides the exact 123 with the exact 10 he has to have 12.3 stored in the register, 12.3 has an infinite number of bits in the given representation and they have to be cut somewhere to fit any finite storage:
10001(0011)...
If you check with the same converter, IEEE is careful enough to even know that after last 00 bits that fit in the 64-bits two 11 follow, so what's stored is 01 instead 00 for the last digits.
The only solution for people who don't understand all the pitfalls of the standardized binary representation would be using base-10 floating point which is also standardized but sadly the last time I've checked existed only in some IBM hardware:
http://en.wikipedia.org/wiki/IEEE_floating_point http://en.wikipedia.org/wiki/IEEE_floating_point
(see IEEE 754-2008)
The software support is equivalently rare.
- andrewcooke 14y agofrom .1 to .9 only .5 has and .25 and 0.75 and... The only solution for people who don't understand and rational number libraries and multiple precision libraries and ...
- acqq 14y agoI considered the results of the division of the integers by 10, then 0.25 is not there.
- MikeDucker 14y agoHi acqq, I did type 123 into that float converter and the numbers I got back are what I posted. I'm not sure how we're getting different FP reps for the same number. I simply typed 123 into the decimal representation box. If I'm missing something please let me know.
- andrewcooke 14y agothe problem is that for 0.123 (say) it's not 123 multiplied by 0.001 - the multiplier has to be a power of 2, so the thing being multiplied is no longer a simple integer. see my answer at http://stackoverflow.com/a/7289400/181772 http://stackoverflow.com/a/7289400/181772 for a similar case.
- acqq 14y agoRead carefully. 123 is represented 100% exactly, bit for bit. Look at the bits in the mantissa, 111011, leading 1 is the implicit and it matches >>> bin( 123 ) '0b1111011' not at the "interpretation for humans" which shows 1.921875. All integers up to some very big one are represented exactly in IEEE base-2 FP (or you can say that the fractions that are the result from the storage of the integers have a finite binary representation, but then you make harder for you to have an easy insight). Disclaimer: I have similar experiences to smoyer, in the eighties of the previous century I've implemented a reasonable chunk of IEEE 754 in x86 assembly.
- MikeDucker 14y agoI see what you're saying, but if you take the 111011 as representing 1/2 1/4 1/8 etc. then it does equal 0.921875. I'm presuming that this value is added onto 1 (fiddling with the tick boxes confirms this) to be used as the mantissa. I'm not doubting what you know about floating points, but I think that this calculator is still trying to say (whether that be right or wrong based on IEEE) 6 ^ 1.921875
- acqq 14y agoYou didn't get the different FP representation, what's important are the bits, they are what they are, not how you decide to translate them to the decimal system. The only thing being processed are the bits. Concentrating on the decimal interpretation (which is just that, the number is processed as the base 2 IEEE 754 FP) you missed that 123 has an absolutely exact binary representation.
- Tuna-Fish 14y ago
- sageikosa 14y agoNot just binary. Any finite representation of an infinite sequence is always an approximation (4/3=1.666...). It's just novel to decimal-minded folk when they see it happen with decimal presentations of what are really binary stored values, since binary representation of 1/5 is non-terminating, whereas 1/2 does terminate. Make the world scale 0.125 (or 0.625), which is close to 0.1, but has an exact binary representation. If you only ever divide this into integers, you should be mostly OK (he speculated non-authoritatively).