Semantics of ^^, Version 3 (Final?)

Don nospam at nospam.com
Wed Dec 9 01:55:19 PST 2009


KennyTM~ wrote:
> On Dec 9, 09 16:36, Don 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?
> 
> 0^^-1 == 0 ?

Good catch. That line is completely wrong.



More information about the Digitalmars-d mailing list