From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: GNAT does not consistently raise an exception to log(0.0) Date: Sat, 13 Jul 2013 09:26:18 +0200 Organization: cbb software GmbH Message-ID: References: <74c64a2c-f062-4b59-aafb-40c0bac39203@googlegroups.com> Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: xkOZ88C3T5fLavXpgyt3vA.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:16341 Date: 2013-07-13T09:26:18+02:00 List-Id: 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