Why does not my program is not running?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 20 14:35:01 PDT 2015


On 8/20/15 5:15 PM, anonymous2 wrote:
> On Thursday, 20 August 2015 at 21:11:07 UTC, anonymous wrote:
>> I severely limited the range of integer. I don't know off the top of
>> my head how large you can make it without hitting overflow.
>>
> with integer == 66 the factorial overflows and becomes 0 (on my machine)
> => integer division by 0...
> The overflow happens at lower values, too.
>

I did this in my test of his code:

long factorial(long i){
     if (i == 0){
         return 1;
     }else{
         auto r = factorial(i-1);
         assert(r * i / r == i, i.to!string);
         return r * i;
     }
}

And it printed 21 for the exception. 20! fits in a long, 21! doesn't.

-Steve


More information about the Digitalmars-d-learn mailing list