More on semantics of opPow: return type

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Dec 8 00:01:23 PST 2009


Don wrote:
> Lionello Lunesu wrote:
>> On 8-12-2009 1:43, 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.
>>
>> No, Don's just being awefully thorough. If you'd revisit all existing
>> operators with the same thoroughness you would probably give up the
>> whole idea of writing a language. Apart from the int/int division
>> there's the int<<int, "What if I shift more than 31?" "What if I shift
>> with <0?" and of course int+int "What to do on overflow?" etc..
>>
>> I think ^^ can be made to work just fine. Let's not get carried away
>> trying to make it perfect.
>>
>> L.
> Exactly. The fallback case is to just make int^^int an int.
> It's only because I was looking at the range propagation stuff inside 
> the compiler, that I got the idea that we could better than that. But, 
> as Andrei says, maybe it's just not worth any more thought at this stage.

Walter and I decided to leave the decision of ^^ up to you, Don. You are 
the best positioned to make it. I hope you will piggyback a decision 
about ^^= to it, too.

What I'd like would be a solid rationale for the choice. Off the top of 
my head and while my hat is off to your math skills and experience, I 
have trouble understanding the soundness of making int^^int yield an 
int, for the following reason:

For all negative exponents, the result is zero, except when the base is 
1 or -1. So I guess I'd suggest you at least make the exponent  unsigned 
- the result is of zero interest (quite literally) for all negative 
exponents. If that behavior is interesting, it would be great if you 
provided a rationale for it.

One small nit is that the exponential function is increasing very 
rapidly, much faster than multiplication. So yielding a long may 
actually be justified. (Some people would argue 32-bit multiplication is 
in the same league and ought to yield 64-bit results.) But then again, 
most uses of ^^ only raise things to small powers such as 2 and 3.

Anyhow, the power is yours - again quite literally :o).


Andrei



More information about the Digitalmars-d mailing list