Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70772 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32428 invoked from network); 19 Dec 2013 22:43:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Dec 2013 22:43:04 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.177 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.177 mail-wi0-f177.google.com Received: from [209.85.212.177] ([209.85.212.177:45008] helo=mail-wi0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F4/8F-42949-77673B25 for ; Thu, 19 Dec 2013 17:43:04 -0500 Received: by mail-wi0-f177.google.com with SMTP id cc10so2912970wib.10 for ; Thu, 19 Dec 2013 14:43:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=Qq53eLNtjtnmIKDJYLA7uMiIbtEjBal2mvPPbiDwQDU=; b=ht8tfbwFMgDk7doOcE5A5FczGkTjEEcIYL9vsVVItBZMHvNHZ3NuU3wfBeqihswTto ZXlqWeP1hMM5gwwjpn/BXEfuF4CZXfUVoSEEerLM4epbUtFO2guwyBEnbdTZJNpn8D+Q Nh243INpMWdAyEPuxr8RMGDXmNSxbJdc67myHMcg4mvtGljaGLiUDjURZtnBknLAk5jY huGGVlyYZ9GMlwJmk+iGhTkp8dTSDyufgX/4HBiOwKqQzqvyGyzT0fgH62Um3Qtt5ihz qV2KcScP+npHo5nFejGcmcHOyCXzXAikv/dtkl0MXWd1LF8l7wIqTmRUNHpyiSKIPk+r Seag== X-Received: by 10.180.95.105 with SMTP id dj9mr5163313wib.22.1387492981118; Thu, 19 Dec 2013 14:43:01 -0800 (PST) Received: from [192.168.0.2] (cpc19-brig17-2-0-cust25.3-3.cable.virginm.net. [81.101.201.26]) by mx.google.com with ESMTPSA id w20sm14231040wia.5.2013.12.19.14.42.59 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 19 Dec 2013 14:42:59 -0800 (PST) Message-ID: <52B37663.5070308@gmail.com> Date: Thu, 19 Dec 2013 22:42:43 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: internals@lists.php.net References: <52B36BFC.4080708@lerdorf.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: power operator (again) From: rowan.collins@gmail.com (Rowan Collins) On 19/12/2013 22:16, Daniel Macedo wrote: >> No, the average PHP developer is going to do $var ** 2 and it will >> >behave exactly as they expect with negative values. Yes, in the very > I didn't quite understand if you're making the case for assuming a type > casting of $var and keep with the expected math or if operator precedence > would mean the result could be negative in some cases. I understood Rasmus's point to be rather simpler than that: PHP code will rarely contain the actual hard-coded sum "-3 ** 2", it will much more likely look something like this: $var = some_function(); $square_of_var = $var ** 2; Now if some_function() returns -3 (the integer), $square_of_var will end up with a value of 9. No need to worry about associativity, no type casting, just application of maths, and the result is as expected. Now, you could imagine a more complex example like this: $cube_of_inverted_var = -$var ** 3; But that's hard enough to read that I'd probably introduce brackets anyway, if not avoid the unary operator altogether: $cube_of_inverted_var = (-1 * $var) ** 3; In my head, the reason "-3 ** 2" looks like (-3) ** 2 is that I tend to think of "-3" as a single literal value, not the application of the unary operator "-" to the literal value "3". Once it becomes "-$var ** 2", that impression evaporates, and I would, personally, avoid assuming precedence one way or another, and add some brackets. Regards, -- Rowan Collins [IMSoP]