[fpc-pascal] math with infinity and NaN

C Western l at c-m-w.me.uk
Wed Jun 20 23:28:41 CEST 2018


On 20/06/18 22:16, C Western wrote:
> On 20/06/18 21:58, Florian Klämpfl wrote:
>> Am 19.06.2018 um 23:50 schrieb James Richters:
>>> 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:
>>
>> No. The invalid operation is not masked by default. If you do so, FPC 
>> just write Nan.
> 
> My initial testing indicates that, even with exceptions masked, the 
> exception was raised: (or am I using the wrong routine?)
> 
> Uses math;
> var
>      variable1:double;
>      variable2:double;
> Begin
> SetExceptionMask([exDenormalized,exZeroDivide,exOverflow,exUnderflow,exPrecision]); 
> 
> variable1:= Infinity;
> variable2:= -1*Infinity;
> Writeln(variable1,' ',Variable2,' ',Variable1+Variable2);
> end.
> 
> ./t
> An unhandled exception occurred at $00000000004002A1:
> EDivByZero: Division by zero
>    $00000000004002A1  main,  line 9 of t.pas
> 
>                      +Inf                     -Inf
> 
> Colin

Using:
SetExceptionMask([exDenormalized,exZeroDivide,exOverflow,exUnderflow,exPrecision,exInvalidOp]);
gives:
                     +Inf                     -Inf                      Nan

though I would have to say the requirement to mask invalid operation 
when the exception reported is divide by zero is confusing....

Colin





More information about the fpc-pascal mailing list