ID: 30069 Updated by: [EMAIL PROTECTED] Reported By: php at holycamel dot com -Status: Verified +Status: Closed Bug Type: Math related Operating System: * PHP Version: 4CVS, 5CVS New Comment:
This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. Previous Comments: ------------------------------------------------------------------------ [2004-09-15 22:47:52] php at holycamel dot com Thanks for checking into this so quickly and for confirming the report. Sorry my case was not in the most simple form. ------------------------------------------------------------------------ [2004-09-15 14:06:15] [EMAIL PROTECTED] This works: # php -r 'echo "0.1" * "2"; This does not: # php -r 'echo ".1" * "2"; (both work fine in < 4.3.9 versions) ------------------------------------------------------------------------ [2004-09-15 13:45:15] [EMAIL PROTECTED] This script shows it better, $foo is 0 with 4.3.9RC4, when it was okay with 4.3.8-dev (I don't remember what date that compile had and now it's gone ;( ) <?php $astr = "2|.9456|.0066778|17"; $numarray = explode("|",$astr); $foo = $numarray[0] * $numarray[2]; var_dump($foo); $result = round($foo,4); echo $result; ?> ------------------------------------------------------------------------ [2004-09-12 20:03:31] php at holycamel dot com Description: ------------ After exploding a string into its components which contain type float, subsequent multiplication and rounding with those strings doesn't work unless specifically casting them as type float - ie. (float)$strarray[0]. This particularly seems to be the case if the result is < 1. Where the result is > 1, the code below seems to work. This appears to be a recent change in the behavior of php, since previous versions automatically and correctly interpreted strings as floats where necessary for related math operations (such as multiplication and rounding). This may be my own misconception about how things should work, but I am reporting it in the event that there has been some problem with the string type handling introduced in recent versions of php. If not, sorry for wasting your time and I'd appreciate an explanation of why our code has broken. Thanks. Reproduce code: --------------- failing code: $astr = "2|.9456|.0066778|17" $numarray = explode("|",$astr); $result = round($numarray[0] * $numarray[2],4); echo $result; working code: $astr = "2|.9456|.0066778|17" $numarray = explode("|",$astr); $result = round((float)$numarray[0] * (float)$numarray[2],4); echo $result; (I have had to apply this workaround to code which previously worked with earlier versions of php without the additional type-casting.) Expected result: ---------------- .0134 Actual result: -------------- 0 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=30069&edit=1