Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:66039 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67371 invoked from network); 20 Feb 2013 09:44:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Feb 2013 09:44:16 -0000 Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.22 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.22 smtp2.ist.utl.pt Linux 2.6 Received: from [193.136.128.22] ([193.136.128.22:57185] helo=smtp2.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 70/09-19387-EEA94215 for ; Wed, 20 Feb 2013 04:44:16 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id EBE3970003DF; Wed, 20 Feb 2013 09:44:11 +0000 (WET) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp2.ist.utl.pt ([127.0.0.1]) by localhost (smtp2.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id 6TTsA4Z8wD07; Wed, 20 Feb 2013 09:44:11 +0000 (WET) Received: from nebm.ist.utl.pt (unknown [IPv6:2001:690:2100:4::58:1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 9F1AF70003DD; Wed, 20 Feb 2013 09:44:11 +0000 (WET) Received: from localhost ([127.0.0.1] helo=nebm.ist.utl.pt) by nebm.ist.utl.pt with esmtp (Exim 4.72) (envelope-from ) id 1U86Dj-0004lw-Ie; Wed, 20 Feb 2013 09:44:11 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 20 Feb 2013 10:44:11 +0100 To: Stas Malyshev Cc: PHP Internals Organization: =?UTF-8?Q?N=C3=BAcleo_de_Engenharia_Biom=C3=A9dica_do_Insti?= =?UTF-8?Q?tuto_Superior_T=C3=A9cnico?= In-Reply-To: <51248616.907@sugarcrm.com> References: <511666DE.8080809@fedoraproject.org> <51172819.4000806@sugarcrm.com> <51174790.8040805@fedoraproject.org> <511751C5.1020906@fedoraproject.org> <51175BDD.9040705@sugarcrm.com> <5117BA92.8080307@fedoraproject.org> <511890AC.7080506@fedoraproject.org> <51189847.5030506@sugarcrm.com> <5122DE69.3080901@sugarcrm.com> <51248616.907@sugarcrm.com> Message-ID: <8bb50302d7ff520ca862d112ee005f3a@nebm.ist.utl.pt> X-Sender: glopes@nebm.ist.utl.pt User-Agent: RoundCube Webmail/0.8-rc Subject: Re: [PHP-DEV] double val to long val conversion issue From: glopes@nebm.ist.utl.pt (Gustavo Lopes) Em 2013-02-20 9:15, Stas Malyshev escreveu: > Hi! > >>> Also, I'm not sure I understand what 64-bit test is supposed to >>> return >>> and why - why negative number is converted to positive one? What >>> exactly >>> this change is supposed to improve? >> >> The least significant bytes are preserved. Take int(-2056257536) and >> int(2943463994971652096): >> >> In[6]:= BitAnd[2^32 - 1, {-2056257536, 2943463994971652096}] >> >> Out[6]= {2238709760, 2238709760} > > I'm not sure I understand, what this syntax means and how it explains > why we're doing these conversions? It doesn't, of course. I was just showing why there are changes in sign. > I.e. what requires this change and > how it is beneficial? I understand that it preserves least > significant > bits, but why we should preserve them and discard sign, for example? First of all, there are no change for the ]LONG_MAX, ULONG_MAX] range. Doubles in that range (notice they're positive numbers) already got converted to negative numbers. The problem was that for numbers outside the [LONG_MIN, ULONG_MAX] range, the code relied on undefined behavior that did vary between platforms. This change does away with the undefined behavior and provides a uniform treatment of the finite double domain. (int)$dval is now round*($dval) mod 2^(PHP_INT_SIZE*8) converted to a signed integer of size PHP_INT_SIZE*8 bits assuming a two's complement representation, for all finite $dval double values. You could argue that another behavior like clipping at LONG_MAX/LONG_MIN is preferable. But we can't do it for BC reasons. And especially for 32-bit long systems, the current behavior is very useful. For instance let's say you had some network protocol that used an unsigned 32-bit int on which you needed to do some bitwise operations. You can use a double to represent the range 2^31..2^32-1 that you got from, from instance, adding two large PHP integers, cast it to int and do your bitwise operation. * Actually, I just realized the code probably needs a small adjustment to make the double always round towards zero. This is only important in 32-bit systems though; for the others, the doubles outside the long range have no fractional part. -- Gustavo Lopes