Re: AW: [PHP] PHP is Zero
[email protected] (Pete Ford)
| Newsgroups | php.general |
|---|---|
| Message-ID | <[email protected]> |
On 13/06/13 10:44, BUSCHKE Daniel wrote:
> Hi,
> thanks for your answer. Especially the answer "42" made me laughing :)
>
> My "Why" questions should be understand as "Why must it be like that" questions.
>
>>> On 13/06/13 08:59, BUSCHKE Daniel wrote:
>>> 5. Thats a bug I have opend: https://bugs.php.net/bug.php?id=51739 where I also had the same problems because "8315e839da08e2a7afe6dd12ec58245d" was converted into float(INF) by throwing everything starting from "da08.." away.
>>>
>> That's a very different proposition, and probably has more to do with word size: float is 32-bit, so only the first 32 bits are used and if anything else is found the conversion falls back to INF. To handle really big integers like 8315e839da08e2a7afe6dd12ec58245d you probably need a more specialist library (or language)
>
> For me it is not. PHP throws things away during conversion. In my opinion a language (compiler, interpreter whatever) should not do that. Never ever! Either it is able to convert the value or it is not.
>
> What about of returning "null" instead of "0" if the conversion is not perfect? So intval('F') could return NULL and intval('0') could return 0.
>
> Regards
> Daniel
>
I've had a bit of a play with your big hex number, and the problem is
much more subtle: floatval("8315e839da08e2a7afe6dd12ec58245d") is
truncated at 8315e839 because it tries to parse it as an exponential
float: floatval("8315e83") becomes 8.315 x 10^86, but 8.315e839 is 8.315
x 10^842 which is (as far as PHP is concerned) an infinitely large number!
So we try replacing that first 'e' with a 'd' (for example) and then
php -r 'var_dump(floatval("8315d839da08e2a7afe6dd12ec58245d"));'
returns
float(8315)
It gives up when it finds a non-numeric character (as the documentation
would tell you)
Perhaps what you need is
php -r 'var_dump(floatval(0x8315e839da08e2a7afe6dd12ec58245d));'
float(1.7424261578436E+38)
In other words, you need to tell the interpreter that you have a number
(in base-16) rather than a string.
A proper strongly-typed language would just tell you that it's nonsense...
Cheers
Pete