#30069 [Opn->Ver]: Change in behavior with strings containing floats...

From: Date: Wed, 15 Sep 2004 11:45:27 +0000
Subject: #30069 [Opn->Ver]: Change in behavior with strings containing floats...
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-66197@lists.php.net to get a copy of this message
ID: 30069 Updated by: sniper@php.net Reported By: php at holycamel dot com -Status: Open +Status: Verified Bug Type: Math related -Operating System: linux / apache2 +Operating System: * -PHP Version: 4.3.9RC2 +PHP Version: 4CVS-20040915 (4.3.9RC4-dev) New Comment: 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; ?> Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#66197) next »