comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: GNAT does not consistently raise an exception to log(0.0)
Date: Sat, 13 Jul 2013 09:26:18 +0200
Date: 2013-07-13T09:26:18+02:00	[thread overview]
Message-ID: <pikl8lmiksxt$.tqqkmx1qc8x6$.dlg@40tude.net> (raw)
In-Reply-To: 74c64a2c-f062-4b59-aafb-40c0bac39203@googlegroups.com

On Fri, 12 Jul 2013 17:52:08 -0700 (PDT), Jerry wrote:

> When the following program is built with any of theses compilers:
> 
> GNATMAKE GPL 2013 (20130314) (downloaded from AdaCore)
> GNATMAKE GPL 2011 (20110419) (downloaded from AdaCore)
> GNATMAKE 4.8.0 (Simon Wright's build)
> 
> like this:
> 
> $ gnatmake -f bomb_log.adb -OX
> 
> then if X = 0 (zero) the expected exception is raised and reported thusly:
> 
> raised CONSTRAINT_ERROR : a-ngelfu.adb:744 explicit raise
> 
> else if X > 0 no exception is raised.
> 
> with Ada.Numerics.Long_Elementary_Functions; 
> use  Ada.Numerics.Long_Elementary_Functions;
> procedure Bomb_Log is
>     x : Long_Float;
> begin
>     x := log(0.0);
> end Bomb_Log;

You should always be careful about bogus IEEE 754 semantics when using
numeric operations. I presume that your machine has Long_Float implemented
by machine's IEEE 754. log returns NaN or -Inf. When you turn optimization
-O2 it slips through. I cannot tell how consistent is that with RM, I
presume it is. Maybe -O0 should not raise Constrant_Error. I am not a
language lawyer.

Anyway, in order to enforce sane numeric semantics you do:

with Ada.Numerics.Long_Elementary_Functions; 
use  Ada.Numerics.Long_Elementary_Functions;
procedure Bomb_Log is
   subtype Proper_Float is Long_Float range Long_Float'Range;
   x : Proper_Float;
begin
   x := log(0.0);
end Bomb_Log;

This should work as expected.

> However, under the same conditions as above, the following program raises
> the exception always:
> 
> with Ada.Numerics.Long_Elementary_Functions; 
> use  Ada.Numerics.Long_Elementary_Functions;
> with Ada.Text_IO;
> use  Ada.Text_IO;
> procedure Bomb_Log is
>     x : Long_Float;
> begin
>     x := log(0.0);
>     Put_Line(Long_Float'Image(x));
> end Bomb_Log;

I suppose that here you get the exception rather from Long_Float'Image,
when it stumbles on NaN or whatever garbage log returned.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2013-07-13  7:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-13  0:52 GNAT does not consistently raise an exception to log(0.0) Jerry
2013-07-13  1:47 ` Anh Vo
2013-07-13  2:12   ` Jerry
2013-07-13  2:28     ` Anh Vo
2013-07-13  3:33       ` Jerry
2013-07-14  3:28       ` Jerry
2013-07-14 13:35         ` Anh Vo
2013-07-15  8:25           ` Jerry
2013-07-13  7:26 ` Dmitry A. Kazakov [this message]
2013-07-14  3:34   ` Jerry
2013-07-14  3:51   ` Jerry
2013-07-13  7:34 ` Simon Wright
2013-07-14  3:42   ` Jerry
2013-07-13  9:37 ` AdaMagica
2013-07-14  3:44   ` Jerry
2013-07-15 17:16     ` AdaMagica
replies disabled

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