php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51739 tricky string to float conversion
Submitted: 2010-05-04 15:07 UTC Modified: 2010-05-04 15:15 UTC
From: daniel dot buschke at nextiraone dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.13 OS:
Private report: No CVE-ID: None
 [2010-05-04 15:07 UTC] daniel dot buschke at nextiraone dot de
Description:
------------
If you convert a string like 8315e839da08e2a7afe6dd12ec58245d into a float the conversion results in float(INF). The conversion seems to use only 8315e839 and throws anything else away.

$ php -r 'var_dump(time()+rand()+"8315e839da08e2a7afe6dd12ec58245d");'
float(INF)

Converting this string into a float is - of course - a bug of my PHP script. But at least I would like to discuss about the conversion behavior. It is fully correct to convert this string into a float if it is just "8315e839" but throwing some data away is - from my point of view - not a valid behavior.

Test script:
---------------
var_dump((float)"8315e839da08e2a7afe6dd12ec58245d");

Expected result:
----------------
false or null or some other invalid "floats" or a warning

Actual result:
--------------
float(INF)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-04 15:15 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-05-04 15:15 UTC] aharvey@php.net
This is expected and documented behaviour: string to float conversions will use any valid numeric data at the start of the string and ignore anything thereafter. The e is being processed because it's being treated as an exponent.

More information:
http://php.net/language.types.string#language.types.string.conversion
 [2010-05-05 09:27 UTC] daniel dot buschke at nextiraone dot de
look at this: 8315e839da08e2a7afe6dd12ec58245d

- 8315 is a valid integer
- e is the sign for an exponent
- BUT: 839da08e2a7afe6dd12ec58245d is not an integer! So it is not a valid exponent!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC