Re^2: floating point addition
by morgon (Priest) on Jan 12, 2015 at 17:59 UTC
|
perl -Mbignum -e ' printf "%20.17f\n", 20.1;'
20.10000000000000142
perl -Mbigrat -e ' printf "%20.17f\n", 20.1;'
20.10000000000000142
perl -Mbigrat -e ' printf "%20.17f\n", 20+1/10;
20.00000000000000000
| [reply] [d/l] |
|
You are right about bignum. Your bigrat example, though, doesn't work. One needs
perl -Mbigrat -le 'print 20/1 + 1/10'
| [reply] [d/l] |
|
perl -Mbigrat -e ' printf "%20.17f\n", 20/1+1/10;'
20.10000000000000142
perl -Mbigrat -e ' printf "%20.17f\n", 201/10;'
20.10000000000000142
Does not seem to work either - I don't know why not... | [reply] [d/l] |
|
|
|
Re^2: floating point addition
by kresike (Initiate) on Jan 12, 2015 at 16:49 UTC
|
That I've stumbled upon myself, and it seems like an interesting article, however I'm a bit reluctant to read 67 pages of that just to reach the same conclusion I already know, avoid computers touching floating point numbers ...
A bit naive of me to think that 20.1 written like that means the same for a computer as it does to me. | [reply] |
|
I think I detect a note of intended irony in that last sentence, but basically that's what you get when you try to do base 10 math in a base 2 machine. The sooner you become of aware of it, the sooner you can deal with it.
This part probably sounds like one of those tirades from old farts that begin "back in my day.." .. but .. back in my day, one of the required courses was "Programming for Numerical Methods" or some such title, and we spent the class trying to predict how poorly our computations would proceed based on understanding the limits of the algorithms. There were two results to each computation, the result and the error estimate, and the second result was more important because it told you if could take the first one seriously or not. I took the class in the 80's and our textbook dated back to the 60's. This has become less important over time because improvements in computational speed and memory allow us to use floating point numbers of sufficiently high resolution that we avoid a lot of these problems .. but here we are in 2015 and 1/10th is still giving us fits.
| [reply] |
|
I wouldn't say that its very old fart-ish. I finished uni relatively recently (about 4 years ago) and floating point errors (and the inherent inaccuracies with storing base 10 values in base 2 format) were certainly covered in our courses. Indeed, I would expect this to be basic knowledge covered in any good Software Dev / Programming course.
| [reply] |
|
| [reply] |
|
| [reply] |