More on semantics of opPow: return type

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Mon Dec 7 10:41:15 PST 2009


Don wrote:
> As has been mentioned in previous posts, a ^^ b should be right 
> associative and have a precedence between multiplication and unary 
> operators. That much is clear.
> 
> 
> Operations involving integers are far less obvious (and are actually 
> where a major benefit of an operator can come in).
> 
> Using the normal promotion rules, 10^^2 is an integer. The range 
> checking already present in D2 could be extended so that the compiler 
> knows it'll even fit in a byte. This gets rid of one of the classic 
> annoyances of C pow:  int x = pow(2, 10); doesn't compile without a cast.
> 
> But the difficult question is, what's the type of 10^^-2 ? Should it be 
> an error? (since the result, 0.01, is not representable as an integer). 
> Should it return zero? (just as 1/2 doesn't return 0.5). For an example 
> of these semantics, see http://www.tcl.tk/cgi-bin/tct/tip/123.html).
> Or should it return a double?


I think it should either be an error, or it should return a 
floating-point value.

I'll even go as far as saying that I think 1/2 should either be an error 
or a floating-point value -- or perhaps even a special rational type. 
Integer division is not the same as "ordinary" division, and I think it 
shouldn't use the same symbol. (A backslash would be better: 5\2 == 2)

Of course, I'm arguing the mathematician's point of view here. :)

-Lars



More information about the Digitalmars-d mailing list