[fpc-pascal] math with infinity and NaN

James Richters james at productionautomation.net
Tue Jun 19 23:50:17 CEST 2018


I’ve been updating my old programs to use the MATH unit in freepascal and while testing things I came across a runtime error 217  Invalid floating point operation.  Here is my test program

 

Uses math;

var

   variable1:double;

   variable2:double;

Begin

variable1:= Infinity;

variable2:= -1*Infinity;

Writeln(variable1,' ',Variable2,' ',Variable1+Variable2);

End.

 

My output is:

Running "i:\programming\test\testmath.exe "

An unhandled exception occurred at $004015F6:

EInvalidOp: Invalid floating point operation

  $004015F6  main,  line 8 of i:/programming/test/testmath.pas

 

                    +Inf                     -Inf

 

 

According to the link here: https://en.wikipedia.org/wiki/NaN 

NaN should be produced:

The additions (+∞) + (−∞), (−∞) + (+∞) and equivalent subtractions (+∞) − (+∞) and (−∞) − (−∞).

 

I can do things like +infinity*2 and +infinity-3 and even sqr(infinity) and power(infinity,10) the results are still +Inf which is expected…   but I can’t do anything involving subtracting infinity from infinity.  Here is another test program that illustrates this more clearly:

 

Uses math;

var

   variable1:double;

   variable2:double;

Begin

variable1:= 1/0;

variable2:= -1/0;

Writeln('          V1',variable1);

Writeln('          V2',variable2);

Writeln('    SQRT(-1)',Sqrt(-1));

Writeln('       V1*V1',variable1*variable1);

Writeln('       V1*V2',variable1*variable2);

Writeln('        V1+3',variable1+3);

Writeln('       V1+V1',variable1+variable1);

Writeln('        V1-3',variable1-3);

Writeln('     Sqr(V1)',Sqr(Variable1));

Writeln('     Sqr(V2)',Sqr(Variable2));

Writeln('    Sqrt(-1)',Sqrt(-1));

Writeln('    Sqrt(V1)',Sqrt(Variable1));

Writeln('Power(V1,10)',Power(Variable1,10));

Writeln('     ABS(V2)',ABS(Variable2));

Writeln('    Sqrt(-1*V1)',Sqrt(-1*Variable1));//runtime error 217  Should be NAN

Writeln('    Sqrt(V2)',Sqrt(Variable2));      //runtime error 217  Should be NAN

Writeln('       V1-V1',variable1-variable1);  //runtime error 217  Should be NAN

Writeln('       V1+V2',variable1+variable2);  //runtime error 217  Should be NAN

End.

 

Running "i:\programming\test\testmath.exe "

          V1                    +Inf

          V2                    -Inf

    SQRT(-1)                          Nan

       V1*V1                    +Inf

       V1*V2                    -Inf

        V1+3                    +Inf

       V1+V1                    +Inf

        V1-3                    +Inf

     Sqr(V1)                    +Inf

     Sqr(V2)                    +Inf

    Sqrt(-1)                          Nan

    Sqrt(V1)                    +Inf

Power(V1,10)                         +Inf

     ABS(V2)                    +Inf

An unhandled exception occurred at $00401A43:

EInvalidOp: Invalid floating point operation

  $00401A43  main,  line 22 of I:/Programming/Test/testmath.pas

 

    Sqrt(-1*V1)

It seems to me that the whole purpose of +Inf, -Inf, and NaN was so you could evaluate complex formulas and NOT get a runtime error… Is this behavior just a bug that should be reported?  Testing was done with V3.0.4

 

James

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20180619/787a6bbc/attachment.html>


More information about the fpc-pascal mailing list