[fpc-pascal] Division by Zero - not raised exception

Florian Klaempfl florian at freepascal.org
Sun Apr 16 17:01:11 CEST 2006

Sasa Zeman wrote:
>> The program won't compile with -Cr.
> Program with -Cr will compile and link if all is correct. Eventually range
> check error compiler can predict, will stop compilation as usually.
> Otherwise, program itself will check range on each attemp to work with
> arrays or calculations, which is clearly waste of time.
>> ... operating system or CPU ...
>> but not the compiler ;)
> "... As well globaly, in consistency terms." I can not see reasonability of
> different threaded division by zero by CPU/FPU or compiler. 

The compiler doesn't know at compile time how the exception mask would
look at run time at this point, so it's not threaded differently, div by
zero might be masked at this point and throwing an error would be wrong
because at run time the code would evalute to Inf.

> Delphi have
> several inconveniently basically solved problems, among of which is this
> one. Handling by inheritance Delphi solution in FPC mode is unfortunate.
>> The point is: this div by zero is encountered at compilation time.
>> The other option is to stop compilation always. Executing the operation
>> at run time is useless imo and a waste of CPU cycles.
>> No. These constants are defined in const blocks. A const block couldn't
>> throw an exception.
> Even better. In compilation time, if compiler detect division by zero with
> constants it should stop compilation - by default, without any switch. I can
> not see reason to be threaded differently.

There is code out there defining Inf by 1/0. There was a bug report
about this, see also below.

> In that case there is no problems: 'Writeln (1.0/0.0)' or 'a:=a*b+1/0' and
> similar constructions should be threaded by default. It is mathematical
> nosence continuing calculating ratinals - 

No, it's IEEE behavior: calculating with Inf etc. is completely valid.

> during complex calculations,
> simple error will lead to infinity result instead to simply stop during
> compilation and show the error.
> I see no problem in implementation.

People will complain, if FPC isn't delphi compatible. I implemented it
this way because people complained that FPC behaves differently than
Delphi in this case. And I see nothing wrong with it: the compiler can't
know the fpu exception mask at execution time when compiling the program
so throwing an error might be as wrong as returning Inf. So the only
real solution would be to carry out the 1/0 at run time, however, this
leads also to inconsistent code regarding const inf = 1/0; while
writeln(1/0); could throw an exception, writeln(Inf); wouldn't. So imo
any possible solution is wrong in some way.

More information about the fpc-pascal mailing list