ID: 41154 Updated by: [EMAIL PROTECTED] Reported By: php at michaelho dot com -Status: Open +Status: Bogus Bug Type: Variables related Operating System: Mac OS X 10.4.9 PHP Version: 5.2.1 New Comment:
Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2007-04-20 21:25:22] php at michaelho dot com Description: ------------ Using settype() to alter FLOATs yields inconsistent values that will sometimes fail comparison operators. Reproduce code: --------------- <?php // THIS WILL WORK $fltOriginal = 3 * .5; $fltTransformed = $fltOriginal; settype($fltTransformed, 'string'); settype($fltTransformed, 'float'); var_dump($fltOriginal); var_dump($fltTransformed); if ($fltOriginal != $fltTransformed) print ('COMPARISON FAILED -- BUG'); print ("--------------------------\r\n"); // THIS WILL **NOT** WORK $fltOriginal = 3 * .6; $fltTransformed = $fltOriginal; settype($fltTransformed, 'string'); settype($fltTransformed, 'float'); var_dump($fltOriginal); var_dump($fltTransformed); if ($fltOriginal != $fltTransformed) print ('COMPARISON FAILED -- BUG'); ?> Expected result: ---------------- float(1.5) float(1.5) -------------------------- float(1.8) float(1.8) Actual result: -------------- float(1.5) float(1.5) -------------------------- float(1.8) float(1.8) COMPARISON FAILED -- BUG ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=41154&edit=1
