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!mx02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Nasser M. Abbasi" Newsgroups: comp.lang.ada Subject: Re: getting same output as gfortran, long_float Date: Fri, 01 May 2015 02:32:27 -0500 Organization: Aioe.org NNTP Server Message-ID: References: <1kxou0nloqg9c$.1x0itzgdrlosm$.dlg@40tude.net> <1i8x3r1feyzkt$.j85il7e3wpv9.dlg@40tude.net> Reply-To: nma@12000.org NNTP-Posting-Host: CV72NQ0GT7rQd8cP1ZYi/A.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:25682 Date: 2015-05-01T02:32:27-05:00 List-Id: On 5/1/2015 1:52 AM, Dmitry A. Kazakov wrote: > On Thu, 30 Apr 2015 20:12:47 -0500, Nasser M. Abbasi wrote: > >> I found that gfortran can do 128 bit floating point without >> the use of the compiler switch -fdefault-real-8. Which will >> map to similar thing as the above Ada construct: >> >> ------------------- >> PROGRAM foo >> IMPLICIT NONE >> REAL(KIND = 16) :: x !-- kind=16 tells it is double quad >> x = 12.0D0 * 0.0001D0/(1.0D0 * (1.0D0 - 0.1D0)**4 ) >> PRINT *, x >> END PROGRAM >> ------------------ > > I didn't use FORTRAN for decades, but in good old FORTRAN-IV you declare > specific lengths using T*n, e.g. > > INTEGER*2 > REAL*4 > REAL*8 > > so, I would suggest, maybe naively, > > REAL*16 > I am no Fortran expert by any means, but yes, REAL*16 will also work. However, modern Fortran suggests to use KIND. Actually, it is much more complicated. The way I wrote it above is not the optimal way. One is supposed to call SELECTED_REAL_KIND(n,e) requesting n significant digits and e number of digits in the exponent (e is optional). i.e. one is supposed to write SELECTED_REAL_KIND(8,3) specifies real type of (+-) 0.xxxxxxxx * 10 ^(+-)xxx If the compiler does not support this, then the compile will fail. This is in a way similar to Ada's type my_type is digits n; And it is supposed to be portable way of doing things, vs. using Real*16. Here is an example: --------------------- PROGRAM foo IMPLICIT NONE INTEGER, PARAMETER :: quad = SELECTED_REAL_KIND(30,10) REAL(KIND = quad) :: x x = 12.0D0 * 0.0001D0/(1.0D0 * (1.0D0 - 0.1D0)**4 ) PRINT *, x END PROGRAM ------------------------ >gfortran -Wall foo2.f90 >./a.out 1.82898948331047112025871115292829927E-0003 If one would to ask for more digits than the compiler can support, one gets an -1 from SELECTED_REAL_KIND() call, and this generates a compile error in the next statement, since KIND=-1 is compile error. There is much more to this, and it can get really complicated to understand it all. The bottom line is that using KIND is the recommended way to define real variables in modern Fortran to obtain same precision on different systems. Here is one link on the subject http://fortranwiki.org/fortran/show/Real+precision --Nasser