Re: Power function as operator
| From: | Johannes Schlüter | Date: | Sat, 23 Nov 2013 15:47:23 +0000 |
| Subject: | Re: Power function as operator | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-70326@lists.php.net to get a copy of this message | ||
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