[fpc-pascal] math with infinity and NaN

gtt at wolfgang-ehrhardt.de gtt at wolfgang-ehrhardt.de
Thu Jun 21 21:52:59 CEST 2018


Zitat von James Richters <james at productionautomation.net>:

> The fact that it raises the exception at all makes it a signaling  
> NaN not a quiet Nan, but they are supposed to be Quiet Nan which  
> never throw the exception according to the specification which  
> clearly states they are Quiet Nans, not Signaling Nans.    
> Suppressing the exception makes them act like quiet nans, but the  
> fact that they need suppressing of the exception makes them  
> signaling nans.

This is not correct. Signal and quit NaNs ar precisly defined:

Quote from  
https://en.wikipedia.org/wiki/IEEE_floating_point#Interchange_formats

"For NaNs, quiet NaNs and signaling NaNs are distinguished by using the
most significant bit of the trailing significand field exclusively
(the standard recommends 0 for signaling NaNs, 1 for quiet NaNs, so
that a signaling NaNs can be quieted by changing only this bit to 1,
while the reverse could yield the encoding of an infinity),
and the payload is carried in the remaining bits."

In the IEEE-754 standard it is "3.4 Binary interchange format encodings"

You can test, that the Nan is a quiet NaN with

Uses
   math, sysutils;
var
   variable1, variable2:double;
   iv1: int64 absolute variable1;
Begin
   SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,  
exOverflow, exUnderflow, exPrecision]);
   variable1:= sqrt(-1);
   variable2 := Pi-4;
   variable1 := sqrt(variable2);
   Writeln(variable1, '  ', IntToHex(iv1, 16));
End.

C:\TMP>fpc64304 xx1.pas

C:\TMP>D:\FPC304\bin\i386-win32\ppcrossx64.exe xx1.pas
Free Pascal Compiler version 3.0.4 [2017/10/06] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Win64 for x64
Compiling xx1.pas
Linking xx1.exe
13 lines compiled, 0.2 sec, 71824 bytes code, 4964 bytes data

C:\TMP>xx1.exe
                      Nan  FFF8000000000000

The 8 nibble shows that the highest bit of the mantissa is 1 and
therefore you have a quiet NaN.




More information about the fpc-pascal mailing list