in reply to Surviving 'Illegal division by zero'

My first thought was that you should be able to use signals to catch these types of floating point exceptions...
$SIG{FPE}=sub{...};
...but it turns out that perl is too smart and catches these types of errors before passing them on to the hardware. Does anyone know if there is a compiler flag for perl which would disable these checks? Just for the fun of it I compiled up a version of perl which commented out the divide by zero check (in pp.c)...
/*if (right == 0.0) DIE(aTHX_ "Illegal division by zero");*/
...make, compile, run...
$ ./perl -e '$a=1.23/0.00;print "\$a=$a\n"' $a=inf
...looks like we're going down the right track...
if (right == 0.0) PUSHs(&PL_sv_undef);/*DIE(aTHX_ "Illegal division by zero");*/ else PUSHn( left / right );
...make, compile, rerun...
$ ./perl -e 'print "UNDEFINED\n" unless (defined(1.23/0.00))' UNDEFINED
Of course you'd probably want to take care of the integer division case also.


-- All code is 100% tested and functional unless otherwise noted.

Replies are listed 'Best First'.
Re: Signals and floating point exceptions
by theorbtwo (Prior) on Jun 25, 2004 at 02:00 UTC

    Actually, having a pragmata that changed the behavior on division by zero from death to just letting the floating point hardware do with it as it will would be rather interesting. I think it should even be possible to do it without slowing up the works except when division by zero actually happens.