comp.lang.ada
 help / color / mirror / Atom feed
* Catching NaN .. not a number
@ 2001-05-05 17:46 Staffan Dittmer
  2001-05-05 17:58 ` Preben Randhol
  2001-05-05 21:50 ` Keith Thompson
  0 siblings, 2 replies; 6+ messages in thread
From: Staffan Dittmer @ 2001-05-05 17:46 UTC (permalink / raw)


Did some calcualtions during the night and
ended up with an output file full of NaN - due to 
overflow when calculating a factorial.

A bit surprised by this since I thought a Constraint Error
would be raised.

So, how do I catch a NaN result ?

? Staffan



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching NaN .. not a number
  2001-05-05 17:46 Catching NaN .. not a number Staffan Dittmer
@ 2001-05-05 17:58 ` Preben Randhol
  2001-05-05 18:28   ` Staffan Dittmer
  2001-05-05 21:50 ` Keith Thompson
  1 sibling, 1 reply; 6+ messages in thread
From: Preben Randhol @ 2001-05-05 17:58 UTC (permalink / raw)


On 5 May 2001 17:46:58 GMT, Staffan Dittmer wrote:
> Did some calcualtions during the night and
> ended up with an output file full of NaN - due to 
> overflow when calculating a factorial.
> 
> A bit surprised by this since I thought a Constraint Error
> would be raised.
> 
> So, how do I catch a NaN result ?

Which compiler? When you say calculating factorial, do you use integer?
If so and If you use GNAT you have to compile your program with this
switch:

   `-gnato'
        Enables overflow checking for integer operations.  This causes
        GNAT to generate slower and larger executable programs by adding
        code to check for both overflow and division by zero (resulting in
        raising `Constraint_Error' as required by Ada semantics).  Note
        that the `-gnato' switch does not affect the code generated for
        any floating-point operations; it applies only to integer
        operations. For floating-point, GNAT has the `Machine_Overflows'
        attribute set to `False' and the normal mode of operation is to
        generate IEEE NaN and infinite values on overflow or invalid
        operations (such as dividing 0.0 by 0.0).

-- 
Preben Randhol ------------------- http://www.pvv.org/~randhol/ --
                 �For me, Ada95 puts back the joy in programming.�



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching NaN .. not a number
  2001-05-05 17:58 ` Preben Randhol
@ 2001-05-05 18:28   ` Staffan Dittmer
  0 siblings, 0 replies; 6+ messages in thread
From: Staffan Dittmer @ 2001-05-05 18:28 UTC (permalink / raw)


In article <slrn9f8fv0.17m.randhol+abuse@kiuk0156.chembio.ntnu.no>,
	randhol+abuse@pvv.org (Preben Randhol) writes:

>When you say calculating factorial, do you use integer?
 
No, long_float. Works up to ~ 170! for me, at least I believe so....


>         For floating-point, GNAT has the `Machine_Overflows'
>         attribute set to `False' and the normal mode of operation is to
>         generate IEEE NaN and infinite values on overflow 
> 

Ok, so since I'm using Gnat and long_floats, I would expect a NaN on
overflow. 
Is NaN a value in itself? Is it possible to do a check like 
if result = NaN then ...  ?

/ Staffan Dittmer



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching NaN .. not a number
  2001-05-05 17:46 Catching NaN .. not a number Staffan Dittmer
  2001-05-05 17:58 ` Preben Randhol
@ 2001-05-05 21:50 ` Keith Thompson
  2001-05-07 21:40   ` Stephen Leake
  1 sibling, 1 reply; 6+ messages in thread
From: Keith Thompson @ 2001-05-05 21:50 UTC (permalink / raw)


f97stdi@dd.chalmers.se (Staffan Dittmer) writes:
> Did some calcualtions during the night and
> ended up with an output file full of NaN - due to 
> overflow when calculating a factorial.
> 
> A bit surprised by this since I thought a Constraint Error
> would be raised.

An implementation isn't required to raise Constraint_Error on
floating-point overflow or division by zero.  See the
Machine_Overflows attribute.

> So, how do I catch a NaN result ?

I don't think there's a standard way to do so; see the documentation
for your implementation.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching NaN .. not a number
  2001-05-05 21:50 ` Keith Thompson
@ 2001-05-07 21:40   ` Stephen Leake
  2001-05-08  1:37     ` David C. Hoos, Sr.
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Leake @ 2001-05-07 21:40 UTC (permalink / raw)


Keith Thompson <kst@cts.com> writes:

> f97stdi@dd.chalmers.se (Staffan Dittmer) writes:
> > Did some calcualtions during the night and
> > ended up with an output file full of NaN - due to 
> > overflow when calculating a factorial.
> > 
> > A bit surprised by this since I thought a Constraint Error
> > would be raised.
> 
> An implementation isn't required to raise Constraint_Error on
> floating-point overflow or division by zero.  See the
> Machine_Overflows attribute.
> 
> > So, how do I catch a NaN result ?
> 
> I don't think there's a standard way to do so; see the documentation
> for your implementation.

X'Valid should catch it; see ARM 13.9.2

And it works in GNAT 3.14a:

with Ada.Text_IO; use Ada.Text_IO;
procedure NAN
is
   Zero : Float := 0.0;
   A_NAN : Float := 1.0 / Zero;
begin
  Put_Line ("A_NAN'Valid => " & Boolean'Image (A_Nan'Valid));
end NAN;

generates:

A_NAN'Valid => FALSE

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Catching NaN .. not a number
  2001-05-07 21:40   ` Stephen Leake
@ 2001-05-08  1:37     ` David C. Hoos, Sr.
  0 siblings, 0 replies; 6+ messages in thread
From: David C. Hoos, Sr. @ 2001-05-08  1:37 UTC (permalink / raw)


As I posted Saturday, May 5:

Catching NaN requires a three-way test, viz.:

if Value /= 0.0 and then (not (Value < 0.0)) and then (not (Value > 0.0))
then
   <Value is NaN>;
end if;

"Stephen Leake" <stephen.a.leake.1@gsfc.nasa.gov> wrote in message
news:uelu0on1n.fsf@gsfc.nasa.gov...
> Keith Thompson <kst@cts.com> writes:
>
> > f97stdi@dd.chalmers.se (Staffan Dittmer) writes:
> > > Did some calcualtions during the night and
> > > ended up with an output file full of NaN - due to
> > > overflow when calculating a factorial.
> > >
> > > A bit surprised by this since I thought a Constraint Error
> > > would be raised.
> >
> > An implementation isn't required to raise Constraint_Error on
> > floating-point overflow or division by zero.  See the
> > Machine_Overflows attribute.
> >
> > > So, how do I catch a NaN result ?
> >
> > I don't think there's a standard way to do so; see the documentation
> > for your implementation.
>
> X'Valid should catch it; see ARM 13.9.2
>
> And it works in GNAT 3.14a:
>
> with Ada.Text_IO; use Ada.Text_IO;
> procedure NAN
> is
>    Zero : Float := 0.0;
>    A_NAN : Float := 1.0 / Zero;
> begin
>   Put_Line ("A_NAN'Valid => " & Boolean'Image (A_Nan'Valid));
> end NAN;
>
> generates:
>
> A_NAN'Valid => FALSE
>
> --
> -- Stephe




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-05-08  1:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-05 17:46 Catching NaN .. not a number Staffan Dittmer
2001-05-05 17:58 ` Preben Randhol
2001-05-05 18:28   ` Staffan Dittmer
2001-05-05 21:50 ` Keith Thompson
2001-05-07 21:40   ` Stephen Leake
2001-05-08  1:37     ` David C. Hoos, Sr.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox