|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-11-20 23:53 UTC] utilmind at gmail dot com
Description: ------------ It's probably bug with conversion from floating point number, or bug with comparision between types. We know that X*5/5 is 5. Even PHP returns 5, however, sometimes, returned value appears to be greater than 5. Test script: --------------- $n = (3.89880952381*5/3.89880952381); // result will be exactly 5 print $n.'!=5 ? '.($n>5); // here it appears that 5 is greater than 5 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 02:00:01 2025 UTC |
The "ini_set("precision", 17);" explains the origin of the problem, but does not solves it. The problem is not that floating point operations makes little inaccuracies. My point is that, if PHP with low precision setting rounds values for visual representation of inaccurate numbers, it should also round them on compare operations like < > and ==. Otherwise, we currently getting a value, which looks exactly like "5", but actually it little bit more than "5". Currently following type conversion operation fixes the problem: $n = (float)(string)(3.89880952381*5/3.89880952381); but it's not ideal. The "precision" option of php.ini should also affect on logical comparison operations.