php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30069 Change in behavior with strings containing floats...
Submitted: 2004-09-12 20:03 UTC Modified: 2004-09-16 20:23 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: php at holycamel dot com Assigned:
Status: Closed Package: Math related
PHP Version: 4CVS, 5CVS OS: *
Private report: No CVE-ID: None
 [2004-09-12 20:03 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-15 14:06 UTC] sniper@php.net
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 22:59 UTC] iliaa@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 13:01:28 2024 UTC