Re: Float comparison
| From: | Andrey Hristov | Date: | Fri, 02 May 2008 17:24:43 +0000 |
| Subject: | Re: Float comparison | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-37404@lists.php.net to get a copy of this message | ||
Hi,
Jonathan Bond-Caron wrote:
Hi, I'm new to the PHP internals list and I'm posting an issue I'm sure has been mentioned before -- float comparison I'd like to know if there are reasons not to change the default behavior when comparing floats. i.e. This would seem logical to me:Never compare directly floats but subtract one from the other and check whether the difference is bigger or smaller than a small predefined number. Regards, Andrey -- Andrey Hristov, Connectors Software Developer Sun Microsystems GmbH, Sonnenallee 1, 85551 Kirchheim-Heimstetten Amtsgericht Muenchen: HRB161028 Geschaeftsfuehrer: Thomas Schroeder, Wolfgang Engels, Dr. Roland Boemer Vorsitzender des Aufsichtsrates: Martin Haering$test = 19.6*100;if((float)(string)$test == (double)1960)echo "GOOD BEHAVIOR";// Implicitely convert the float to string then float (en_USlocale)if($test == (double)1960)echo "THIS SHOULD PASS by casting(float)(string)$test internally...";elseecho "NOT GOOD BEHAVIOR";// Exact comparison would still failif($test !== (double)1960)echo "GOOD BEHAVIOR";Any reason why $test == (double)1960 should fail? I realized we are comparing two floats, but couldn't php internally convert to string then float - for the non strict (===) case Thanks