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

Tom Verhoeff T.Verhoeff at tue.nl
Sun Oct 18 17:52:26 CEST 2009


On Sat, Oct 17, 2009 at 01:57:28PM +0200, Bart wrote:
> 
> Is there a reason why in fpc both floating point and integer division
> by zero raise an EDivByZero exception?
> 
> See: http://docwiki.embarcadero.com/VCL/en/SysUtils.EZeroDivide
> 
> SysUtils.EZeroDivide exception is raised when an application attempts
> to divide a floating-point value by zero.
> Note:  Integer divide-by-zero errors raise the SysUtils.EDivByZero exception.

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.  This is e.g. useful when one needs to evaluate rational
functions.  Without such an infinity, you need to make a nasty case
analysis, which furthermore depends on the rational function.

A simple example is the situation where one needs to calculate
the replacement resistor value R for parallel resistors having
values R1, R2, ..., Rk.  The formula is R = 1/(1/R1 + 1/R2 + ... + 1/Rk).
The formula gives a divide-by-zero if one of the resistors has value 0.
But in that case, the replacement value R also equals 0.  When allowing
infinities, it just works out fine (infinity + x = infinity, 1/infty = 0).
That is precisely why IEEE 754 has infinities.  Also see

	<http://www.cs.berkeley.edu/~wkahan/ieee754status/why-ieee.pdf>

for other examples and further motivation.

Unfortunately, many compilers still do not fully support the IEEE 754
floating-point standard.

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