Re: Float comparison

From: Date: Fri, 02 May 2008 18:36:53 +0000
Subject: Re: Float comparison
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-37408@lists.php.net to get a copy of this message
Todd Ruth schrieb:
I'm afraid you'll find Pierre's response representative. The php devs seem to consider the fact that a theoretically perfect "==" doesn't exist to mean that the improvements that would cover 99.9% of user issues with float == shouldn't be made. It could be worse, however. At least the cast to string works as one would expect. Just imagine if those who oppose improving "==" had their logic extended to the cast to string. Php might to the following: <?php $x=.3+.4+.5; print "$x\n"; ?> might output "1.19999999999999996447286321199499070644378662109375" If someone filed a bug report, they could refer you to that paper and tell you there's no bug because you're trying to get a string from float and you never know what you might get.
Actually, depending on ini settings, that happens: $ php -dprecision=100 -r 'echo 0.3+0.4+0.5,"\n";' 1.1999999999999999555910790149937383830547332763671875 Don't echo floats, use printf() or alike. And you'll quickly see that the "cast to string before comparision" is a bad idea, because: $ php -dprecision=1 -r 'var_dump((string)1.4 == (string)1.1);' bool(true) Having display settings affect comparisions seems like a really bad idea to me ;) Regards, Stefan

« previous php.internals (#37408) next »