Semantics of ^^, Version 3 (Final?)

Don nospam at nospam.com
Wed Dec 9 06:16:23 PST 2009


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?

Stuff it, it's too hard to explain.
No negative exponents.
Anyone who wants (-1) ^^ n where n is negative, will have to write
(-1) ^^ abs(n). Sorry, Bill. It ain't worth the complexity just to save 
5 characters of syntax sugar.





More information about the Digitalmars-d mailing list