Re: [PHP] Bug #51739 tricky string to float conversion

2011-09-01 Thread Simon J Welsh
On 1/09/2011, at 8:43 PM, magic-...@damage.devloop.de wrote:

> Simon J Welsh writes: 
>> On 1/09/2011, at 9:53 AM, magic-...@damage.devloop.de wrote: 
>>> Am Mittwoch, 31. August 2011, 20:48:37 schrieb Shawn McKenzie:
 On 08/31/2011 09:03 AM, magic-...@damage.devloop.de wrote:
> var_dump((float)"8315e839da08e2a7afe6dd12ec58245d");
> results in float(INF)
 The cast to float is truncating the invalid characters and since your
 string contains a float that is INF (8315e839) before the truncation at
 the "d", then it returns INF.  Makes perfect sense.
>>> If I use a string in PHP I don't want PHP to cut this string. Either it 
>>> uses this string as it is or it gives me an error/warning/false (what 
>>> ever...). But silently(!!!) using a small piece of a string is not 
>>> understandable.
>> The manual clearly states this is how a string is converted into a number: 
>> http://php.net/manual/en/language.types.string.php#language.types.string.conversion
>>  "The value is given by the initial portion of the string. If the string 
>> starts with valid numeric data, this will be the value used. Otherwise, the 
>> value will be 0 (zero). Valid numeric data is an optional sign, followed by 
>> one or more digits (optionally containing a decimal point), followed by an 
>> optional exponent. The exponent is an 'e' or 'E' followed by one or more 
>> digits." If you don't want the string turned into a number using the 
>> documented method, don't use it as one.
> 
> Ok, then let's discuss the documented behavior ;-) 
> Do you (not the document) really like this behavior? I mean if I am the only 
> one wondering about that behavior it is ok for me. I just want to help PHP. 
> cheers
> Daniel

Yes, I think the way it works is correct.

It follows similar conversions when changing from a data type that allows more 
data to one that allows less. Similar things happen when converting from a 
floating point to integral value.

It also directly correlates to the C functions for doing the same thing (atof, 
strtod, the like).
---
Simon Welsh
Admin of http://simon.geek.nz/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bug #51739 tricky string to float conversion

2011-09-01 Thread Florian Lemaitre

Le 01/09/2011 10:43, magic-...@damage.devloop.de a écrit :

Simon J Welsh writes:

On 1/09/2011, at 9:53 AM, magic-...@damage.devloop.de wrote:

Am Mittwoch, 31. August 2011, 20:48:37 schrieb Shawn McKenzie:

On 08/31/2011 09:03 AM, magic-...@damage.devloop.de wrote:

var_dump((float)"8315e839da08e2a7afe6dd12ec58245d");
results in float(INF)


The cast to float is truncating the invalid characters and since your
string contains a float that is INF (8315e839) before the 
truncation at

the "d", then it returns INF.  Makes perfect sense.


If I use a string in PHP I don't want PHP to cut this string. Either 
it uses this string as it is or it gives me an error/warning/false 
(what ever...). But silently(!!!) using a small piece of a string is 
not understandable.


The manual clearly states this is how a string is converted into a 
number: 
http://php.net/manual/en/language.types.string.php#language.types.string.conversion 

"The value is given by the initial portion of the string. If the 
string starts with valid numeric data, this will be the value used. 
Otherwise, the value will be 0 (zero). Valid numeric data is an 
optional sign, followed by one or more digits (optionally containing 
a decimal point), followed by an optional exponent. The exponent is 
an 'e' or 'E' followed by one or more digits."
If you don't want the string turned into a number using the 
documented method, don't use it as one.


Ok, then let's discuss the documented behavior ;-)
Do you (not the document) really like this behavior? I mean if I am 
the only one wondering about that behavior it is ok for me. I just 
want to help PHP.

cheers
Daniel


No professional programmer would like this kind of behavior.
But PHP want to be accessible for newbies so we have to deal with that.
Hope this will be fixed in the next generation...



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bug #51739 tricky string to float conversion

2011-09-01 Thread magic-php
Simon J Welsh writes: 

On 1/09/2011, at 9:53 AM, magic-...@damage.devloop.de wrote: 


Am Mittwoch, 31. August 2011, 20:48:37 schrieb Shawn McKenzie:

On 08/31/2011 09:03 AM, magic-...@damage.devloop.de wrote:

var_dump((float)"8315e839da08e2a7afe6dd12ec58245d");
results in float(INF)


The cast to float is truncating the invalid characters and since your
string contains a float that is INF (8315e839) before the truncation at
the "d", then it returns INF.  Makes perfect sense.


If I use a string in PHP I don't want PHP to cut this string. Either it uses 
this string as it is or it gives me an error/warning/false (what ever...). But 
silently(!!!) using a small piece of a string is not understandable.


The manual clearly states this is how a string is converted into a number: http://php.net/manual/en/language.types.string.php#language.types.string.conversion 

"The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits." 


If you don't want the string turned into a number using the documented method, 
don't use it as one.


Ok, then let's discuss the documented behavior ;-) 

Do you (not the document) really like this behavior? I mean if I am the 
only one wondering about that behavior it is ok for me. I just want to help 
PHP. 


cheers
Daniel

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bug #51739 tricky string to float conversion

2011-09-01 Thread Simon J Welsh
On 1/09/2011, at 9:53 AM, magic-...@damage.devloop.de wrote:

> Am Mittwoch, 31. August 2011, 20:48:37 schrieb Shawn McKenzie:
>> On 08/31/2011 09:03 AM, magic-...@damage.devloop.de wrote:
>>> Hi,
>>> I have opend Bug #51739 in 2010. It was closed as bogus before my last
>>> question was answered. It would be fine to know what you think about
>>> that bug.
>>> In short:
>>> var_dump((float)"8315e839da08e2a7afe6dd12ec58245d");
>>> results in float(INF)
>>> This is because "8315" is treated as base and
>>> "e839da08e2a7afe6dd12ec58245d" is treated as an exponent. My hint that
>>> "e839da08e2a7afe6dd12ec58245d" is not a valid exponent was not answered.
>>> What do you think about?
>>> cheers
>>> Daniel
>> 
>> The cast to float is truncating the invalid characters and since your
>> string contains a float that is INF (8315e839) before the truncation at
>> the "d", then it returns INF.  Makes perfect sense.
> 
> This is what drives me crazy.
> 
> If I use a string in PHP I don't want PHP to cut this string. Either it uses 
> this string as it is or it gives me an error/warning/false (what ever...). 
> But 
> silently(!!!) using a small piece of a string is not understandable.
> 
> cheers
> Daniel

The manual clearly states this is how a string is converted into a number: 
http://php.net/manual/en/language.types.string.php#language.types.string.conversion

"The value is given by the initial portion of the string. If the string starts 
with valid numeric data, this will be the value used. Otherwise, the value will 
be 0 (zero). Valid numeric data is an optional sign, followed by one or more 
digits (optionally containing a decimal point), followed by an optional 
exponent. The exponent is an 'e' or 'E' followed by one or more digits."

If you don't want the string turned into a number using the documented method, 
don't use it as one.
---
Simon Welsh
Admin of http://simon.geek.nz/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Bug #51739 tricky string to float conversion

2011-08-31 Thread magic-php

Hi,
I have opend Bug #51739 in 2010. It was closed as bogus before my last 
question was answered. It would be fine to know what you think about that 
bug. 


In short:
var_dump((float)"8315e839da08e2a7afe6dd12ec58245d"); 

results in float(INF) 

This is because "8315" is treated as base and 
"e839da08e2a7afe6dd12ec58245d" is treated as an exponent. My hint that 
"e839da08e2a7afe6dd12ec58245d" is not a valid exponent was not answered. 

What do you think about? 


cheers
Daniel

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php