Floating Point Demystified, Part 2: Why Doesn't 0.1 and 0.2 == 0.3?
blog.reverberate.org
blog.reverberate.org
That's no longer the state-of-the-art. The latest approach - and possibly the end of the road for research her - is Andrysco et al.: http://cseweb.ucsd.edu/~lerner/papers/fp-printing-popl16.pdf
It's been on the front page previously: https://news.ycombinator.com/item?id=10915182
0.3
~> nqp -e "say(0.1 + 0.2)"
0.3
~> perl6 -e "say (0.1 + 0.2).WHAT"
(Rat)
$ lua Lua 5.3.2 Copyright (C) 1994-2015 Lua.org, PUC-Rio > print(0.1 + 0.2) 0.3
(For completely different reasons. I think it's worth being explicit that NQP and Perl 6 get the right result because they are doing this calculation with exactly correct numbers and operations rather than "using printf() with a %.14g format string".)
Having variable-sized mantissa and exponent fields, along with error tracking, we'll get rid of that baloney for real.
http://sites.ieee.org/scv-cs/files/2013/03/Right-SizingPreci... http://www.amazon.com/The-End-Error-Computing-Computational/...
Because, in binary, I don't think you'll be able to answer.
"Technically correct" isn't the kind of correct we, as humans, want here. Plenty of languages get it right the way you would naively expect already, as shown here http://0.30000000000000004.com
Yes, some languages print floating point numbers in a friendly way by default (really if you are going with the default format, anything will do). In what way does it change equality tests?
Rational arithmetic has some upsides also, but it also has some downsides.
I don't do graphics anymore so not much floating point needed but I am still not sure how to do floating point comparisons in a stable and efficient way.
abs(a-b)/(avg(abs(a),abs(b)).
[0] http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/n...
$ php -r 'var_dump(1.00000000000002, 1.00000000000001, 1.00000000000001 === 1.00000000000002);'
float(1)
float(1)
bool(false)
I'm trying to change that, though: http://news.php.net/php.internals/91363For comparison, you can't do anything about representing 1/3 + 1/7 accurately in finite decimal representations
(+ 1/3 1/7)
→ 10/21