From: Dmitry A. Kazakov on
On Thu, 15 Jul 2010 23:07:51 +0100, Simon Wright wrote:

> Warren <ve3wwg(a)gmail.com> writes:
>
>> Is there any is not-a-number or is-infinity test support in Ada05+
>> (for floats)? Is there any being planned?

with Ada.Text_IO; use Ada.Text_IO;
procedure IEEE is -- Only if Float is IEEE!
Zero : Float := 0.0;
Inf : Float := 1.0 / Zero;
NaN : Float := 0.0 / Zero;
begin
Put_Line ("Valid " & Boolean'Image (Inf'Valid));
Put_Line ("In range " & Boolean'Image (Inf <= Float'Last));
Put_Line ("Self NaN " & Boolean'Image (NaN = NaN));
end IEEE;

On an IEEE machine it could print 3x FALSE.

> With GNAT, using a subtype
>
> subtype Checked_Float is Float range Float'Range;
>
> will give you a Constraint_Error for NaN or +/-Inf.

Yes, one of the reasons not to use built-in types is that the floating
point ones most likely are IEEE with all nasty consequences.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Warren on
Dmitry A. Kazakov expounded in news:1tbp3geoa5yna$.171cmlfdrbm98$.dlg@
40tude.net:

> with Ada.Text_IO; use Ada.Text_IO;
> procedure IEEE is -- Only if Float is IEEE!
> Zero : Float := 0.0;
> Inf : Float := 1.0 / Zero;
> NaN : Float := 0.0 / Zero;
> begin
> Put_Line ("Valid " & Boolean'Image (Inf'Valid));
> Put_Line ("In range " & Boolean'Image (Inf <= Float'Last));
> Put_Line ("Self NaN " & Boolean'Image (NaN = NaN));
> end IEEE;
>
> On an IEEE machine it could print 3x FALSE.

On a non IEEE machine, is there going to be an exception
raised when dividing by zero?

Warren
From: Dmitry A. Kazakov on
On Fri, 16 Jul 2010 17:06:38 +0000 (UTC), Warren wrote:

> Dmitry A. Kazakov expounded in news:1tbp3geoa5yna$.171cmlfdrbm98$.dlg@
> 40tude.net:
>
>> with Ada.Text_IO; use Ada.Text_IO;
>> procedure IEEE is -- Only if Float is IEEE!
>> Zero : Float := 0.0;
>> Inf : Float := 1.0 / Zero;
>> NaN : Float := 0.0 / Zero;
>> begin
>> Put_Line ("Valid " & Boolean'Image (Inf'Valid));
>> Put_Line ("In range " & Boolean'Image (Inf <= Float'Last));
>> Put_Line ("Self NaN " & Boolean'Image (NaN = NaN));
>> end IEEE;
>>
>> On an IEEE machine it could print 3x FALSE.
>
> On a non IEEE machine, is there going to be an exception
> raised when dividing by zero?

I would say yes. But theoretically if a non-IEEE machine has special
representations of x/0, as IEEE does, then the compiler vendor is permitted
not to raise Constraint_Error.

Language lawyers?

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Warren on
Dmitry A. Kazakov expounded in
news:1tbp3geoa5yna$.171cmlfdrbm98$.dlg(a)40tude.net:

> with Ada.Text_IO; use Ada.Text_IO;
> procedure IEEE is -- Only if Float is IEEE!
> Zero : Float := 0.0;
> Inf : Float := 1.0 / Zero;
> NaN : Float := 0.0 / Zero;
> begin
> Put_Line ("Valid " & Boolean'Image (Inf'Valid));
> Put_Line ("In range " & Boolean'Image (Inf <= Float'Last));
> Put_Line ("Self NaN " & Boolean'Image (NaN = NaN));
> end IEEE;
>
> On an IEEE machine it could print 3x FALSE.

To detect NaN then, this seems to work:

function Is_Nan(F : Float) return Boolean is
begin
if not F'Valid then
return not ( Is_Infinity(F) or Is_Neg_Infinity(F) );
else
return False;
end if;
end Is_Nan;

where Is_Infinity(F) etc. is implemented as suggested. Essentially
if not valid, but not one of the infinities(+ or -), then it
must be NaN. This works with Gnat on Cygwin. I'll need
to test it on other platforms, but hopefully most if not all
IEEE platforms will support this.

Warren
From: Simon Wright on
Warren <ve3wwg(a)gmail.com> writes:

> To detect NaN then, this seems to work:

See above, where Jean-Pierre said

> If X /= X, then X is a NaN (see http://en.wikipedia.org/wiki/NaN)