Semantics of ^^

Don nospam at nospam.com
Tue Dec 8 07:17:30 PST 2009


Steven Schveighoffer wrote:
> On Tue, 08 Dec 2009 05:32:26 -0500, Don <nospam at nospam.com> wrote:
> 
>> Based on everyone's comments, this is what I have come up with:
>>
>> --------------------
>> x ^^ y is right associative, and has a precedence intermediate between 
>> multiplication and unary operators.
>>
>> * The type of x ^^ y is the same as the type of x * y.
>> * If y == 0,  x ^^ y is 1.
>> * If both x and y are integers, and y > 0,  x^^y is equivalent to
>>     { auto u = x; foreach(i; 1..y) { u *= x; } return u; }
>> * If both x and y are integers, and y < 0, an integer divide error 
>> occurs, regardless of the value of x. This error is detected at 
>> compile time, if possible.
>> * If either x or y are floating-point, the result is pow(x, y).
> 
> If x and y are both integral and x is 2, then the operation becomes 1 << y

And if x is 4, it becomes 1 << 2*y, etc.
Google for "addition chains" if you're interested in the optimal 
sequences, it's a mathematical research area.

> Also, what happens when you do 3^^1000000?  I hope this does not result 
> in the exact loop you wrote above.

No, of course not. I was just describing semantics, not implementation. 
The little foreach loop implicitly describes what happens to overflow, etc.



More information about the Digitalmars-d mailing list