On Tue, 08 Dec 2009 10:17:30 -0500, Don <[email protected]> wrote:
Steven Schveighoffer wrote:
On Tue, 08 Dec 2009 05:32:26 -0500, Don <[email protected]> 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.
2^^n would be a very common entity in programming, it's definitely much
more appealing to me than 1 << n. I just figured the compiler should
avoid making the optimizer work on optimizing out that foreach loop and do
the work up front.
But in any case, since I misunderstood what you meant (that the above loop
is not literally inserted), it doesn't matter as long as the
implementation can be optimized.
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.
ok good.
-Steve