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 X-Google-Thread: 103376,8c0fcfc0a87e61fc X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder.news-service.com!novso.com!nerim.net!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: simple question on long_float/short_float Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Sat, 2 Oct 2010 11:48:47 +0200 Message-ID: <1fwh82m3de16h$.7aw204c9j2tj.dlg@40tude.net> NNTP-Posting-Date: 02 Oct 2010 11:48:43 CEST NNTP-Posting-Host: 260eac26.newsspool4.arcor-online.net X-Trace: DXC=[;FHgC=:^8o]BlmkiiU@Bi4IUK On Sat, 02 Oct 2010 02:11:10 -0700, Nasser M. Abbasi wrote: > I wrote a small Ada program to print a value of a floating number which > has the type Double_Precision, and printed the value to the screen. > > I did the same in Fortran, same number, and printed the value to screen. > In Fortran, the value printed is that of IEEE754, and in Ada it also > printed the same as Fortran. So I am happy to see that. Here is the code > and the output: > > ---- Ada v1 ---- > with ada.text_io; > with Interfaces.Fortran; use Interfaces.Fortran; > > procedure test is > package my_float_IO is new Ada.Text_IO.float_IO(Double_Precision); > use my_float_IO; > a : Double_Precision := 2.3; > begin > Put( item=>a,Fore=>25,Aft=>16) ; > end test; > > ---- fortran ---- > program main > implicit none > double precision :: c=2.3D0 > > write (*,'(F25.16)') c > end program main > > ---- output of the above in same order ---- > 2.2999999999999998E+00 ---- Ada > 2.2999999999999998 ---- Fortran > > Then I changed the Ada code to the following: > > ---- Ada v2 ----- > with ada.text_io; > procedure test is > type my_float_type is digits 16; > package my_float_IO is new Ada.Text_IO.float_IO(my_float_type); > use my_float_IO; > a : my_float_type := 2.3; > begin > Put( item=>a,Fore=>25,Aft=>16) ; > end test; > > and now the output is > > 2.3000000000000000E+00 > > ------------------------------ > > 1) So, it seems to me that Ada did use Fortran double in v1. since > output is different than in v2. Unless I made a mistake in v2. Hence, I > do not understand why the webpage above said that Gnat would use the > same types. Can I change v2 to make it output the same as Fortran? I > assume not, since it is not double to start with. Hmm, try this: with Ada.Text_IO; use Ada.Text_IO; with Interfaces.Fortran; use Interfaces.Fortran; procedure Test is type My_Float_Type is digits 16; type Long_Float_Type is digits 18; A : My_Float_Type := 2.3; B : Double_Precision := 2.3; C : Long_Float_Type := 2.3; begin Put_Line ("A'Size=" & Integer'Image (A'Size) & " A=" & My_Float_Type'Image (A)); Put_Line ("B'Size=" & Integer'Image (B'Size) & " B=" & Double_Precision'Image (B)); Put_Line ("C'Size=" & Integer'Image (C'Size) & " C=" & Long_Float_Type'Image (C)); end Test; On my Intel machine Double_Precision is 96 bits. Then note that the output may differ not because the Ada type uses another machine type. Even if they are same, since you have specified 16 *decimal* digits, the output rounds the underlying binary representation to 16 decimal digits. > 3) I need to read more about Ada Float type. Standard is IEEE754, so if > Ada float does not use this, That depends on the machine. Float and Long_Float standard types are most likely IEEE 754. > would this not make the analysis of > floating point computation in Ada harder? No, it makes it easier. It is advisable not to use IEEE 754 semantics, even if the machine is IEEE 754. E.g. if you declare your type as type My_Float is new Float; -- Chances are high to get IEEE 754 here To kill IEEE 754 semantics do it this way: type My_Float is new Float range Float'Range; -- Only numbers! > Most textbooks and numerical > stuff, assume IEEE754 computation done on floating points. I doubt it much. IEEE 754 is a normal floating-point + some non-numbers like NaN. As the name suggests, numeric computations are performed on *numbers*. IEEE 754 non-numbers might be useful for the languages with no means to deal with numeric errors. But in Ada you don't need that because Ada has numeric exceptions. >From the software design point of view, it is a very bad idea of IEEE 754 to postpone error handling till the end of computations or even forever. You might compute something awfully long and complex just to get NaN in the end, not knowing what (and where) was wrong. In a closed loop system you might deliver NaN on actuators, what would they do? Ada's numeric model follows the fundamental software design principle: you shall detect errors *early*. > I also need to study more about Ada floats and how they work. What I do > not understand yet, does Ada does use IEEE754 for its floats? It likely uses IEEE 754 if the machine is IEEE 754, which is almost always. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de