More on semantics of opPow: return type

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Mon Dec 7 14:09:33 PST 2009


Andrei Alexandrescu wrote:
> Lars T. Kyllingstad wrote:
>> Andrei Alexandrescu wrote:
>>> Nice analysis. IMHO this should lead us to reconsider the necessity 
>>> of "^^" in the first place. It seems to be adding too little real 
>>> value compared to the complexity of defining it.
>>>
>>> Andrei
>>
>>
>> It adds a lot of value to the ones that actually use it, even though 
>> you may not be one of them. Exponentiation is extremely common in 
>> numerics.
> 
> Well I write numerics and I do use exponentiation occasionally, but 
> never to the extent of yearning for ^^. "Extremely common" would be, I 
> think, quite difficult to argue.

Yeah, I get a little carried away. The point I was trying to make is 
that it's not as uncommon as some would have it.

The fundamental reason why I want opPow so badly is in fact not even how 
often I use it. If that was the case, I'd want a special "writefln" 
operator as well. The main reason is that exponentiation is such a basic 
mathematical operation, right up there with addition and multiplication, 
that it deserves an operator of its own.

I also seem to remember someone (was it Don or bearophile, perhaps?) 
listing various optimisation possibilities that are more readily 
available if ^^ is a built-in operator.


>> Here are some statistics for you: A Google code search (see below) for 
>> FORTRAN code using the power operator **, which until recently didn't 
>> have a D equivalent, yields roughly 56100 results.
>>
>> A search for the FORTRAN equivalents of << yield 400 results for ILS() 
>> and 276 results for LSHIFT(). Yet, left shift apparently deserves a 
>> place in D.
>>
>>
>> (I've used http://www.google.com/codesearch, with the following search 
>> strings for **, ILS and LSHIFT, respectively:
>>
>>     [0-9a-zA-Z)]\*\*[0-9a-zA-Z(] lang:fortran
>>     ils\([0-9a-zA-Z] lang:fortran
>>     lshift\([0-9a-zA-Z] lang:fortran
>>
>> As statistics go, this is probably not a prime example, but it is at 
>> least an indication.)
> 
> Thanks for collecting the evidence. To make it more meaningful, you may 
> want to report it to the total number of lines of code searched. I don't 
> know how to do that with codesearch.
> 
> FWIW, this search:
> 
> [0-9a-zA-Z)]\*\*[013456789a-zA-Z(] lang:fortran
> 
> yields 30,300 results, meaning that almost half the uses of 
> exponentiation is to square things.

...which is still a valid use case, since foo*foo evaluates foo twice 
and square(foo) is no better than pow(foo, 2).


> So I'm not sure what this all is supposed to argue for or against. What 
> I can say is that Don's analysis suggested to me, let's leave all that 
> aggravation to overloads of pow() and call it a day. I was much more in 
> favor of ^^ before I saw how quickly it gets complicated. That kind of 
> stuff just doesn't strike me as the kind of things you put straight in 
> the core language.

You could say the same for arrays. T[new] and ~=, anyone? ;)

-Lars



More information about the Digitalmars-d mailing list