Semantics of ^^, Version 3 (Final?)

Don nospam at nospam.com
Wed Dec 9 02:03:57 PST 2009


Simen kjaeraas wrote:
> On Wed, 09 Dec 2009 09:36:56 +0100, Don <nospam at nospam.com> wrote:
> 
>> CHANGES BASED ON FURTHER COMMENTS
>> --------------------
>> x ^^ y is right associative, and has a precedence intermediate between
>>   unary and postfix operators.
>> The type of x ^^ y is the same as the type of x * y.
>>
>> * If either x or y are floating-point, the result is pow(x, y).
>>
>> If both x and y are integers, the following rules apply:
>>
>>   * If x is the compile-time constant 0, x^^y is rewritten as (y==0)? 
>> 1 : 0
>>   * If x is the compile-time constant 1, x^^y is rewritten as (y,1)
>>   * If x is the compile-time constant -1 and y is an integer, x^^y is 
>> rewritten as (y & 1) ? -1 : 1.
>>
>>   * If y == 0, x ^^ y is 1.
>>   * If y > 0,  x ^^ y is functionally equivalent to
>>      { auto u = x; foreach(i; 1..y) { u *= x; } return u; }
>>   * If y < 0, an integer divide error occurs, regardless of the value 
>> of x.
>>
>> -----------
>> Note that by definining the 0,1, -1 cases as "rewriting" rules rather 
>> than return values, it should be clearer that they don't apply to 
>> variables having those values.
>> I think this covers everything useful, while avoiding nasty surprises 
>> like
>>
>> double y = x ^^ -1; // looks like reciprocal, but isn't!
>> // Yes, this IS the same problem you get with double y = 1/x.
>> // But that's doesn't make it acceptable. I have a possible solution 
>> to that one, too.
>>
>> I don't think we can afford to spend much more time on this.
>> Is everyone happy now?
> 
> Might I enquire as to why the exponent should be allowed to be signed
> for integer exponentiation? It's been covered several times - it makes
> no sense.

Because I think it would be too restrictive. Consider code like this:

for (int n=0; n<10; ++n)
{
    v[n] = x ^^ n;
}

Requiring it to be a uint would be OK if the compiler's range 
propagation was near-perfect, but I don't think that's going to happen.

> 
> Apart from that - great job!
> 



More information about the Digitalmars-d mailing list