RE: [PHP-DEV] NAN and INF cast to int
| From: | Robert Stoll | Date: | Tue, 07 Jan 2014 10:59:32 +0000 |
| Subject: | RE: [PHP-DEV] NAN and INF cast to int | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-71035@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Lester Caine [mailto:lester@lsces.co.uk]
> Sent: Monday, January 06, 2014 4:22 PM
> To: internals@lists.php.net >> PHP internals
> Subject: Re: [PHP-DEV] NAN and INF cast to int
>
> Robert Stoll wrote:
> >>> I'm +1 to make div by zero return INF.
> >>
> >> This begs the question 'how' ...
> >> If we are processing to integer values they produce an integer result and there
> >> is no provision for a 'number' INF. This is the exact sort of request that
> >> prompted my recent article on 'what is a number' ...
> >>
> >
> > PHP behaves already different. 1/2 = 0.5
> > So it would be perfectly valid to define 2/0 = INF
> >
> > Personally, I would prefer to keep false for a division with two integers (e.g. 1/0) but
> > would change the result as
> > follows if a float is involved
> > 1.0/0 = INF
> > 1/0.0 = INF
> > 0.0/0 = NAN
> > 0/0.0 = NAN
> > pretty much as other language behave (right?).
>
> But that is not 'cast to int' ...
That's right, this point of my email was already a bit off topic if you like but anyway, I just
wanted to point out that
your reasoning: int / int = int does not hold for PHP in all cases -> 1/2 = 0.5 => int / int
= float
And besides, I never suggested that a cast to int should yield NAN or INF. The main point of my
email was that I was
surprised that no E_WARNING was triggered during the conversion from NAN to int or INF to int.
> http://phpsurgery.org/wiki/integer is my take on some of this,
> and if you follow
> the results on stackoverflow asking why there is not a NAN or INF value for
> numbers in other languages you will see that your list only works for 'float'
That's half true, NAN and INF are not supported by int but very well by floating point numbers
as defined by IEEE 754.
My point was that if the division includes a floating point number (as it is the case in my list
above) and thus the
result is a floating point number anyway then it should remain a floating point number. PHP behaves
like this in most
cases (the only exception I know is division by 0). Try 27.0/3 and you will see that the result is
9.0 (and not int 9).
That's how other programming languages behave as well - C included. And in the case where a
division by 0 occurs (with a
floating point number involved) it should either be NAN or INF (according to my list) and not false
in my opinion. I
barely do something in C, so don't take the following for granted but I think C also yields INF
or NAN if a division by
0 occurs and a floating point number was involved.
> ... but mapping everything to float is wrong?
Sorry, I am not sure how I shall interpret this question, is it ironic/sarcastic? (quite hard to
tell in an email mate
^^)
I said, it is perfectly fine to map a division with two int to a float in PHP (since it is already
done this way => 1/2
= 0.5). But mapping every division to float would probably mean an unnecessary cast to float
sometimes (on the other
hand it would be consistent). I do not think it would be clever to yield always a float in favour of
performance.
That said, it would maybe be a good idea to introduce a function int_division($dividend, $divisor)
which expects two int
(or cast both variables to int) and yields an int (can use the C integer division). This would
certainly be a more
efficient way than using the following code snippet (which one could use right now) - which has
quite an overhead for
something so simple as an integer division:
function int_division($dividend, $divisor){
$result = $dividend/$divisor;
if(!is_int($results)){
$result = (int) $result;
}
return $result;
}
>
> The real problem here is what 'container' is created when calculating
> int(x) / int(y) = ???
> 27/3 can give a clean int(9), but 27/2 has a remainder and 27/0 can't produce an
> int() result? Some other container needs to be created?
That's why I adjusted my statement and said it is ok to yield false for int/0
I don't know how the source code for a division looks like in PHP, so I would just take a wild
guess if I write more
about the actual container.
>
> --
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=contact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk
> Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php