Re: Power function as operator

From: Date: Sat, 23 Nov 2013 16:10:44 +0000
Subject: Re: Power function as operator
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-70327@lists.php.net to get a copy of this message
Hi Johannes, On Sat, Nov 23, 2013 at 11:47 PM, Johannes Schlüter <johannes@schlueters.de>wrote: > 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. > I agree with both comments; it's easy to forget the practicality of being able to search on something you don't know, although I would hope that people recognize it as an operator and go here: http://php.net/manual/en/language.operators.arithmetic.php :) > 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 ;-) > Actually, I was going to suggest creating a Unicode token √ for that ;-) > > 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. > Actually the latter could read: } elseif ($dec - 2 ** $i >= 0) { I can't judge on that, because my opinion will obviously be biased :) > > 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. ;-) > Well, why don't we? :) > > johannes > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- -- Tjerk

« previous php.internals (#70327) next »