More on semantics of opPow: return type

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Dec 8 09:40:28 PST 2009


Bill Baxter wrote:
> On Tue, Dec 8, 2009 at 12:01 AM, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> What I'd like would be a solid rationale for the choice. Off the top of my
>> head and while my hat is off to your math skills and experience, I have
>> trouble understanding the soundness of making int^^int yield an int, for the
>> following reason:
>>
>> For all negative exponents, the result is zero, except when the base is 1 or
>> -1. So I guess I'd suggest you at least make the exponent  unsigned - the
>> result is of zero interest (quite literally) for all negative exponents. If
>> that behavior is interesting, it would be great if you provided a rationale
>> for it.
> 
> The rationale (exponentiale?) for it is consistency.

Consistency is a good rationale, but math simply doesn't work that way. 
Matrix multiplication could have been defined to be consistent with 
matrix addition, but it ain't, because multiplication defined that way 
is uninteresting.

>  Yeh, it's of
> zero interest.  But it's simple and consistent and doesn't require a
> bunch of new rules.  In the world of ints, small positive powers
> really are the only interesting case.  (And not just two and three --
> sometimes you'll see 2**N used to compute buffer sizes in Python.   Or
> at least that's what I observed in the code searches people posted.
> Arguably a more direct expression of intent than 1<<N.)

I agree. Then why not require the exponent to be unsigned during 
compilation? Don said he'd want negative exponent to throw a runtime 
exception. Why?

> You seem to be gunning to make some unneccesarily complicated rules up
> to handle a case that's not really important, but then at the same
> time argue that things are getting too complicated so we better axe
> the whole feature.  It doesn't really make sense.

It does. What I said was that we could be in one of two extremes: ^^ is 
a simple slam dunk, add it for convenience, versus ^^ is hard to type 
properly, but compiler support can do it. A wishy-washy middle ground 
where it's there but not doing something principled... that I don't 
like. But then again I defer the decision to Don.

> To make a comparison, << and >> aren't very useful for floating point
> numbers, does that mean they shouldn't be in the language?

Wrong argument. << and >> are closed over integrals. I see a mistaken 
argument, I point it out :o).

> In
> contrast  ^^ is useful for both floats and ints with small positive
> powers.  No, not all ints, but that's better versatility than << and
>>> at least.

I agree. Then at least why not make the type of the exponent unsigned? 
That gives the type system a fighting chance (via e.g. value range 
propagation). Give Willy a chance!


Andrei



More information about the Digitalmars-d mailing list