[fpc-pascal] Division by Zero: EDivByZero and EZeroDivide

Tom Verhoeff T.Verhoeff at tue.nl
Mon Oct 19 14:14:35 CEST 2009


On Mon, Oct 19, 2009 at 10:21:01AM +0200, Jonas Maebe wrote:
>
> On 18 Oct 2009, at 17:52, Tom Verhoeff wrote:
>
>> It would be nicer if one had the ability to make floating-point  
>> division
>> by zero return an IEEE 754 plus/minus infinity, without raising an
>> exception.
>
> http://www.freepascal.org/docs-html/rtl/math/setexceptionmask.html
>
> Fully cross-platform, even.

How new is that?

It works 'partly'.  Consider the program

program DivideByZero;
  { to see whether division by zero can result in Infinite values }

uses Math;

var one, zero, result: Double;

begin
  SetExceptionMask ( [ exZeroDivide{, exPrecision} ] );
  one := 1.0;
  zero := 0.0;
  result := one / zero;
  writeln ( result ); { Expected output: +Inf }
  result := one / result;
  writeln ( result ); { Expected output: +0.0 }
end.

Outputs:
                  +Inf
An unhandled exception occurred at $000060B0 :
EDivByZero : Division by zero
  $000060B0
  $00005850
  $0001889B

In order to allow 1 / +Inf, you also need to mask exPrecision
(i.e. uncomment it from the set passed to SetExceptionMask).
I am surprised that division by an Infinity raises EDivByZero: Division by zero

Best regards,

	Tom
-- 
E-MAIL: T.Verhoeff @ TUE.NL     | Dept. of Math. & Comp. Science
PHONE:  +31 40 247 41 25        | Technische Universiteit Eindhoven
FAX:    +31 40 247 54 04        | PO Box 513, NL-5600 MB Eindhoven
http://www.win.tue.nl/~wstomv/  | The Netherlands



More information about the fpc-pascal mailing list