NAN and INF cast to int
| From: | Robert Stoll | Date: | Thu, 02 Jan 2014 18:36:59 +0000 |
| Subject: | NAN and INF cast to int | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-70961@lists.php.net to get a copy of this message | ||
Heya,
I was quite surprised that a conversion from NAN or INF does not trigger an E_WARNING. Following
some examples:
An example:
$f = NAN;
var_dump(++$f); // float NAN
var_dump((float) NAN); // float NAN
var_dump((int) NAN); // int -2147483648 -> what?
var_dump((bool) NAN); // bool true -> makes sense
$f = INF;
var_dump(++$f); // float INF
var_dump((float) INF); // float INF
var_dump((int) INF); // int 0 -> what?
var_dump((bool) INF); // bool true -> so why int 0?
var_dump((int) (bool) INF); // int 1
Another behaviour which I find strange is that a division by 0 yields false and not NAN:
var_dump(1.5/0) ; // boolean false
I am aware of that introducing an E_WARNING would be somewhat a BC break but I still suggest to
consider it because I
rather see it as a bug. Consider the following example:
$f = INF; //somehow INF was assigned to $f at some point
$g = $f - 1; //$g is still INF -> correct IMO
$h = (int) $g; // $i = 0; -> the value as such is ok IMO but it should trigger an E_WARNING
$i = $h - 1; //$i = -1 -> especially hard to detect if one does further calculations with such a
value
Thoughts on this?
Cheers,
Robert