Semantics of ^^, Version 3 (Final?)

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Dec 9 10:32:18 PST 2009


KennyTM~ wrote:
> On Dec 9, 09 17:25, 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.
>>
>> Apart from that - great job!
>>
> 
> Because 3^^-1 would become 3^^4294967295 (note: int can be implicitly 
> converted to uint) which then you spend 17 seconds to get 2863311531 and 
> wonder what's going on.

On a non-degenerate base, any exponent greater than 32 (for int) and 64 
(for long) would generate overflow. That can be figured with one test.

That's why I'm saying the exponential grows fast. The range of useful 
exponents is extremely limited.


Andrei



More information about the Digitalmars-d mailing list