issue 7006 - std.math.pow (integral, integral) crashes on negative exponents

jmh530 john.michael.hall at gmail.com
Tue Dec 17 18:32:23 UTC 2019


On Tuesday, 17 December 2019 at 13:30:48 UTC, Timon Gehr wrote:
> [snip]
>
> Note that in existing compiler releases, the specific notation 
> (-1)^^x is currently supported as a special rewrite rule in the 
> frontend. This is going away though: 
> https://github.com/dlang/dmd/commit/0f2889c3aa9fba5534e754dade0cae574b636d55
>
> I.e., I will raise the severity of the issue to regression.


On Tuesday, 17 December 2019 at 13:11:35 UTC, Timon Gehr wrote:
> [snip]
>
> void main(){
>     int x=-1;
>     writeln((-1)^^(-1)); // ok, writes -1
>     writeln(pow(-1,-1)); // divide by zero error
>     writeln((-1)^^(-2)); // ok writes 1
>     writeln(pow(-1,-2)); // divide by zero error
>     writeln(x^^(-1));    // compile error
>     writeln(pow(x,-1));  // divide by zero error
>     writeln((-1)^^x);    // ok, writes -1
>     writeln(pow(-1,x));  // divide by zero error
>     writeln(x^^x);       // divide by zero error
>     writeln(pow(x,x));   // divide by zero error
> }

I think these are both really good points*. If constant folding 
weren't being removed, I wouldn't have a strong feeling on this, 
but I think it is probably important to prevent the regression.

The current behavior clearly has some compiler magic going on 
before this recent change to constant folding. In some sense, you 
would be enshrining this "special" behavior so that
writeln((2)^^(-1)); //compiler error
writeln((2)^^(-2)); //compiler error
writeln((-2)^^(-1)); //compiler error
writeln((-2)^^(-2)); //compiler error
would no longer be errors and would print 0. After thinking on 
it, it probably makes sense to make these changes for language 
consistency. I'm certainly the type of person who could get 
tripped up by these changes, but it still is starting to make 
sense to me.

Can anyone remind me again why ^^ depends on a phobos function?

* Your example with x^^-1 is perhaps overblown as an issue 
because ^^-1 works through constant folding, if you make it enum 
int x = -1, then it's not an error.


More information about the Digitalmars-d mailing list