math with infinity and NaN

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

math with infinity and NaN

Zaaphod

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


_______________________________________________
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
This is compatible with IEEE-754.  Section 7.2 says

<quote>
7.2 Invalid operation

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).
These operations are:

...
b) multiplication: multiplication(0, ∞) or multiplication(∞, 0)

...

d) addition or subtraction or fusedMultiplyAdd: magnitude subtraction  
of infinities, such as:
addition(+∞, −∞)

e) division: division(0, 0) or division(∞, ∞)

...

g) squareRoot if the operand is less than zero
</quote>

You get the expected quite NaN results if you add the line

SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,  
exOverflow, exUnderflow, exPrecision]);

to your test programs.

_______________________________________________
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

C Western-2
In reply to this post by Zaaphod
On 19/06/18 22:50, James Richters wrote:

> 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
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
My first reaction is that
SetExceptionMask([exDenormalized,exZeroDivide,exOverflow,exUnderflow,exPrecision])
would be required (I can't remember what the default is), but it doesn't
stop the error (x86_64/linux). I suspect a bug, though I am slightly
surprised it hasn't surfaced before.

Colin
_______________________________________________
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 C Western <[hidden email]>:

> My first reaction is that
> SetExceptionMask([exDenormalized,exZeroDivide,exOverflow,exUnderflow,exPrecision])
> would be required (I can't remember what the default is), but it  
> doesn't stop the error (x86_64/linux). I suspect a bug, though I am  
> slightly surprised it hasn't surfaced before.


You forgot the most important item: exInvalidOp! At least on Win7 the code
works as exptected.


_______________________________________________
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 Zaaphod
James Richters wrote:
>
>
> 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

I suggest to file a bug report. It is very unfortunate that the Math unit doesn't conform to IEEE-758.

Regards,

Adriaan van Os

_______________________________________________
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

Marco van de Voort
In reply to this post by C Western-2
In our previous episode, C Western said:
> > 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.?

Isn't that normal? Otherwise, what would inf+inf-inf be if you know that
(inf+inf) =inf


_______________________________________________
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
No, that’s not normal, it's not supposed to give you a runtime error.
Anything that results in an answer that is Not a Number is supposed to be set to NaN, not give you a runtime error 217 and terminate.
Infinity minus itself is undefined and therefore Not a Number and should be set to NaN, but I don't get NaN, I get a fatal runtime error 217.   To answer your question:  if you know Inf + Inf = Inf and you know Inf - Inf = Nan then Properly evaluating from left to right: Inf + Inf - Inf = Inf - Inf = NAN     or if you want to do it the other way:  inf - inf + inf  = NaN + Inf = NaN

(+inf) + (+inf) should and does give you (+inf)
(-inf) + (-inf) should and does give you (-inf)
(+inf) - (+inf) should give you (NAN)  but it gives you Runtime Error 217
(-inf) - (-inf) should give you (NAN)  but it gives you Runtime Error 217
(+inf) + (-inf) should give you (NAN)  but it gives you Runtime Error 217
(-inf) + (+inf) should give you (NAN)  but it gives you Runtime Error 217

Snip from refrence at: https://en.wikipedia.org/wiki/NaN 
Operations generating NaN:
There are three kinds of operations that can return NaN:

1. Operations with a NaN as at least one operand.

2. Indeterminate forms:
      a. The divisions (±0) / (±0) and (±∞) / (±∞).
      b. The multiplications (±0) × (±∞) and (±∞) × (±0).
      c. The additions (+∞) + (−∞), (−∞) + (+∞) and equivalent subtractions (+∞) − (+∞) and (−∞) −  (−∞).
      d. The standard has alternative functions for powers:
            The standard pow function and the integer exponent pown function define 0⁰, 1∞, and ∞⁰ as 1.
            The powr function defines all three indeterminate forms as invalid operations and so returns NaN.

3. Real operations with complex results, for example:
      The square root of a negative number.
      The logarithm of a negative number.
      The inverse sine or cosine of a number that is less than −1 or greater than 1.

#2c clearly defines that NaN is set for the above conditions that are failing with a runtime error 217

Also reference:
https://www.philforhumanity.com/Infinity_Minus_Infinity.html


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

In our previous episode, C Western said:
> > 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.?

Isn't that normal? Otherwise, what would inf+inf-inf be if you know that
(inf+inf) =inf


_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: math with infinity and NaN

Florian Klämpfl
In reply to this post by Zaaphod
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.
_______________________________________________
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

Florian Klämpfl
In reply to this post by Adriaan van Os-2
Am 20.06.2018 um 10:59 schrieb Adriaan van Os:
> James Richters wrote:
>>
>>
>> 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
>
> I suggest to file a bug report. It is very unfortunate that the Math unit doesn't conform to IEEE-758.
>

Please read the standard, exceptions are part of it.
_______________________________________________
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
In reply to this post by Florian Klämpfl
Is there a way to prevent getting the runtime error?

James
-----Original Message-----
From: fpc-pascal [mailto:[hidden email]] On Behalf Of Florian Klämpfl
Sent: Wednesday, June 20, 2018 4:59 PM
To: [hidden email]
Subject: Re: [fpc-pascal] math with infinity and NaN

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.
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: math with infinity and NaN

C Western-2
In reply to this post by Florian Klämpfl
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


_______________________________________________
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
In reply to this post by Florian Klämpfl
>Please read the standard, exceptions are part of it.

Not much of a 'standard' if it's full of exceptions so everyone just does it however they want and therefore nothing is actually standard.
_______________________________________________
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

Free Pascal - General mailing list
In reply to this post by Zaaphod
Am 20.06.2018 um 23:15 schrieb James Richters:
> Is there a way to prevent getting the runtime error?

=== code begin ===

SetExceptionMask(GetExceptionMask + [exInvalidOp]);

=== code end ===

Regards,
Sven
_______________________________________________
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

C Western-2
In reply to this post by C Western-2
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


_______________________________________________
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
In reply to this post by Free Pascal - General mailing list
SetExceptionMask(GetExceptionMask + [exInvalidOp]);   Works!    Thank  you for the help!

I'm curious why things like SQRT(-1) just produce NAN without needing to change the exception mask and (+inf) - (+inf) does not behave the same way.  They both are invalid,  why treat one method of generating an invalid number one way and another method of getting just as invalid of a number another way?  If there is flexibility in the standard, then why not choose to always be consistent?    I have a lot of examples of things that do produce NAN without needing to change the exception mask... like ln(-1) or 0/0  why do some cause the exception and others do not?

If someone wanted to volunteer time to adjust the math unit to always behave the same way, would it be something that would be accepted or is there some fundamental reason why it is like this?

James


-----Original Message-----
From: fpc-pascal [mailto:[hidden email]] On Behalf Of Sven Barth via fpc-pascal
Sent: Wednesday, June 20, 2018 5:23 PM
To: [hidden email]
Cc: Sven Barth <[hidden email]>
Subject: Re: [fpc-pascal] math with infinity and NaN

Am 20.06.2018 um 23:15 schrieb James Richters:
> Is there a way to prevent getting the runtime error?

=== code begin ===

SetExceptionMask(GetExceptionMask + [exInvalidOp]);

=== code end ===

Regards,
Sven
_______________________________________________
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

Quoting James Richters <[hidden email]>:

> SetExceptionMask(GetExceptionMask + [exInvalidOp]);   Works!    
> Thank  you for the help!
>
> I'm curious why things like SQRT(-1) just produce NAN without  
> needing to change the exception mask and (+inf) - (+inf) does not  
> behave the same way.  They both are invalid,  why treat one method  
> of generating an invalid number one way and another method of  
> getting just as invalid of a number another way?  If there is  
> flexibility in the standard, then why not choose to always be  
> consistent?    I have a lot of examples of things that do produce  
> NAN without needing to change the exception mask... like ln(-1) or  
> 0/0  why do some cause the exception and others do not?

The case SQRT(-1) seems to be handled by the compiler, just like the  
definition
Nan = 0/0 in math.

If you have an expression, which is not handled at compile time you get
the EInvalidOp exception, try

variable2 := Pi-4;
variable1 := sqrt(variable2);
Writeln(variable1);

_______________________________________________
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 Florian Klämpfl
Florian Klämpfl wrote:

> Am 20.06.2018 um 10:59 schrieb Adriaan van Os:
>> James Richters wrote:
>>>
>>>
>>> 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
>>
>> I suggest to file a bug report. It is very unfortunate that the Math
>> unit doesn't conform to IEEE-758.
>>
>
> Please read the standard, exceptions are part of it.

Even with masked exceptions, runtime errors are produced in the Math unit. That is not conformant
to the standard.

Regards,

Adriaan van Os
_______________________________________________
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 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?

_______________________________________________
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
In reply to this post by gtt
>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.


-----Original Message-----
From: fpc-pascal [mailto:[hidden email]] On Behalf Of [hidden email]
Sent: Wednesday, June 20, 2018 3:42 AM
To: [hidden email]
Subject: Re: [fpc-pascal] math with infinity and NaN

This is compatible with IEEE-754.  Section 7.2 says

<quote>
7.2 Invalid operation

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).
These operations are:

...
b) multiplication: multiplication(0, ∞) or multiplication(∞, 0)

...

d) addition or subtraction or fusedMultiplyAdd: magnitude subtraction  
of infinities, such as:
addition(+∞, −∞)

e) division: division(0, 0) or division(∞, ∞)

...

g) squareRoot if the operand is less than zero
</quote>

You get the expected quite NaN results if you add the line

SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,  
exOverflow, exUnderflow, exPrecision]);

to your test programs.

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: math with infinity and NaN

Zaaphod
In reply to this post by gtt
The compiler does seem to be filtering out the conditions that hard coded to resolve to NaN. That explains the seemingly inconsistent results, when I set variables and try the operations that seemed to work before they now produce the error.
Thanks for pointing that out.


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


Quoting James Richters <[hidden email]>:

> SetExceptionMask(GetExceptionMask + [exInvalidOp]);   Works!    
> Thank  you for the help!
>
> I'm curious why things like SQRT(-1) just produce NAN without needing
> to change the exception mask and (+inf) - (+inf) does not behave the
> same way.  They both are invalid,  why treat one method of generating
> an invalid number one way and another method of getting just as
> invalid of a number another way?  If there is flexibility in the
> standard, then why not choose to always be
> consistent?    I have a lot of examples of things that do produce  
> NAN without needing to change the exception mask... like ln(-1) or
> 0/0  why do some cause the exception and others do not?

The case SQRT(-1) seems to be handled by the compiler, just like the definition Nan = 0/0 in math.

If you have an expression, which is not handled at compile time you get the EInvalidOp exception, try

variable2 := Pi-4;
variable1 := sqrt(variable2);
Writeln(variable1);

_______________________________________________
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
12