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

Timon Gehr timon.gehr at gmx.ch
Tue Dec 17 13:11:35 UTC 2019


On 16.12.19 16:33, jmh530 wrote:
> On Monday, 16 December 2019 at 15:13:44 UTC, jmh530 wrote:
>> [snip]
>> ...
> 
> Simple work-around for the (-1)^^i:
> 
> import std;
> 
> void main() {
>      auto x = iota(10).map!(a => a % 2 ? 1 : -1);
> }
> 

Yes, of course. The expression (-1)^^i is even special-cased by DMD so 
it can actually be used. Does this mean pow(-1,-1) should cause a divide 
by zero error? Of course not.

Just to elaborate on how ridiculous the current behavior is:

import std.math;

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
}


More information about the Digitalmars-d mailing list