math with infinity and NaN

classic Classic list List threaded Threaded
24 messages Options
12
gtt
Reply | Threaded
Open this post in threaded view
|

Re: math with infinity and NaN

gtt

Zitat von James Richters <[hidden email]>:

>> For operations producing results in floating-point format, the  
>> default result of an operation that
>> signals the invalid operation exception shall be a quiet NaN that  
>> should provide some diagnostic
>> information (see 6.2).
>
> If it shall be a quiet NaN doesn't that mean it would never cause  
> the runtime error?   To my understating signaling NaN raises the  
> exception, and Quiet Nan does not.. it just quietly sets the  
> variable to NaN and continues by default.
> It states that the DEFAULT result shall be this quiet NaN so if  
> that's the case then setting SetExceptionMask([exInvalidOp]); should  
> not be required to prevent the runtime error.  The default behavior  
> should be that it's a Quiet NaN.

As far as I understand: if the exInvalidOp is masked the default result
is a quiet NaN. If exInvalidOp is not masked the excption is thrown.
(I do not know if a result is set to NaN and how this could be exploited).

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: math with infinity and NaN

Zaaphod
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.

-----Original Message-----
From: fpc-pascal [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Thursday, June 21, 2018 9:32 AM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] math with infinity and NaN


Zitat von James Richters <[hidden email]>:

>> For operations producing results in floating-point format, the
>> default result of an operation that signals the invalid operation
>> exception shall be a quiet NaN that should provide some diagnostic
>> information (see 6.2).
>
> If it shall be a quiet NaN doesn't that mean it would never cause  
> the runtime error?   To my understating signaling NaN raises the  
> exception, and Quiet Nan does not.. it just quietly sets the variable
> to NaN and continues by default.
> It states that the DEFAULT result shall be this quiet NaN so if that's
> the case then setting SetExceptionMask([exInvalidOp]); should not be
> required to prevent the runtime error.  The default behavior should be
> that it's a Quiet NaN.

As far as I understand: if the exInvalidOp is masked the default result is a quiet NaN. If exInvalidOp is not masked the excption is thrown.
(I do not know if a result is set to NaN and how this could be exploited).

_______________________________________________
fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
gtt
Reply | Threaded
Open this post in threaded view
|

Re: math with infinity and NaN

gtt

Zitat von James Richters <[hidden email]>:

> 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: math with infinity and NaN

Adriaan van Os-2
In reply to this post by gtt
[hidden email] wrote:
>
> Zitat von Adriaan van Os <[hidden email]>:
>> Even with masked exceptions, runtime errors are produced in the Math
>> unit. That is not conformant to the standard.
>
> Even with masked exInvalidOp? Can you give an example?

Math.power with a negative base would except, even with masked exceptions. That was with fpc-2.6.4.
I can't reproduce it anymore with fpc-3.0.4, so it seems to be fixed.

Regards,

Adriaan van Os

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12