Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70326 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61339 invoked from network); 23 Nov 2013 15:48:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Nov 2013 15:48:04 -0000 Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.215.10 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.215.10 mail.experimentalworks.net Received: from [217.114.215.10] ([217.114.215.10:53598] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C7/11-53898-13EC0925 for ; Sat, 23 Nov 2013 10:48:02 -0500 Received: from [192.168.2.20] (ppp-88-217-68-144.dynamic.mnet-online.de [88.217.68.144]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id 94021404CC; Sat, 23 Nov 2013 16:48:18 +0100 (CET) To: Tjerk Meesters Cc: PHP Internals In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Sat, 23 Nov 2013 16:47:23 +0100 Message-ID: <1385221643.7200.25.camel@guybrush> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Power function as operator From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) Hi, On Sat, 2013-11-23 at 00:58 +0800, Tjerk Meesters wrote: > It introduces the pow() function as an operator ** (double asterisk), as > can be found in languages such as Python (with perhaps the notable > difference that it's right associative there). As a comment: In my view we try to keep the language understandable even for people who aren't as deep into it as we are. There having power as function serves two purposes. For one it is a normal function and can easily identified as such with clear precedence etc. Secondly people can stick it in their favorite search engine and can understand what it means. Searching for ** however isn't as trivial. Is the power operation really that common so we have to have it as an operator and opcode? I don't know what the threshold is, but -/ might be a nice unary (square) root operator ;-) Looking at github search results https://github.com/search?l=PHP&p=1&q=pow&source=cc&type=Code I see quite a few search results but most of them duplication (pow.phpt from our test suite comes very often) and in others I inspected it didn't see to be critical code paths gaining much from performance in (in my personal opinion) loosing readability. Randomly picked example: } elseif ($dec - pow(2,$i) >= 0) { vs. } elseif ($dec - (2 ** $i) >= 0) { https://github.com/pkeane/exercises/blob/68b58fb11abfbe8301c202b94906a4ec7e041c10/binpal.php#L59 I consider the former to be better to parse with my brain. And yes, I understand that there are cases where this will boost performance by quite a bit, but then we'd rather make strlen() an opcode. ;-) johannes