Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71035 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51991 invoked from network); 7 Jan 2014 10:59:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Jan 2014 10:59:40 -0000 Authentication-Results: pb1.pair.com header.from=php@tutteli.ch; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=php@tutteli.ch; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain tutteli.ch designates 80.74.154.78 as permitted sender) X-PHP-List-Original-Sender: php@tutteli.ch X-Host-Fingerprint: 80.74.154.78 ns73.kreativmedia.ch Linux 2.6 Received: from [80.74.154.78] ([80.74.154.78:54253] helo=hyperion.kreativmedia.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E4/12-40447-91EDBC25 for ; Tue, 07 Jan 2014 05:59:38 -0500 Received: (qmail 32035 invoked from network); 7 Jan 2014 11:59:34 +0100 Received: from heim-032-99.raab-heim.uni-linz.ac.at (HELO RoLaptop) (193.171.32.99) by ns73.kreativmedia.ch with (AES128-SHA encrypted) SMTP; 7 Jan 2014 11:59:34 +0100 To: "'Lester Caine'" , References: <001801cf07e9$a07af1e0$e170d5a0$@tutteli.ch> <52C5B5E1.5050609@lerdorf.com> <003c01cf07ee$123e08e0$36ba1aa0$@tutteli.ch> <52C5BE84.1050904@ajf.me> <52CABAA2.8090805@lsces.co.uk> <001001cf0aea$dfab1530$9f013f90$@tutteli.ch> <52CACA26.8080407@lsces.co.uk> In-Reply-To: <52CACA26.8080407@lsces.co.uk> Date: Tue, 7 Jan 2014 11:59:32 +0100 Message-ID: <001201cf0b97$8bc64db0$a352e910$@tutteli.ch> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQJ/fRdWvDY7Kzfg/9Kc358Lr8TUtwHAAYsWAdUtbC4Ay/10KwMgJGQJAWiw+NICAOt91wKGjhFlmKyo8oA= Content-Language: de-ch Subject: RE: [PHP-DEV] NAN and INF cast to int From: php@tutteli.ch ("Robert Stoll") > -----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