How to use and verify quad precision in gfortran?

2019-01-27 15:44发布

问题:

I am trying to use quad precision in gfortran, but it seems like the real*16 does not work. After some fishing around, I have found that it may be implemented as real*10. Is real*10 actually quad precision?

How can I test the precision of my code? Is there a standard simple algorithm for testing precision? For example, when I want to figure out what computer zero is, I continue to divide by 2.0 until I reach 0.0. Keeping track of the values lets me know when the computer 'thinks' that my non-zero number is zero - giving me computer zero.

Is there a good way of figuring out the precision with a type of algorithm like I described?

回答1:

Adding to the existing answers... real*N is an extension to the language and is best not used. real*10 is not quad precision. Is is called "extended" -- it is a 10 byte type provided by Intel processors. real*16 may or may not be available on gfortran, depending on compiler version, hardware and availability of libquadmath. If provided in software, it will be slow.

The Fortran way of asking for the precision that you want is to use the selected_real_kind function to define a kind value for the precision that you want.

integer, parameter :: QR_K = selected_real_kind (32)
real (kind=QR_K) :: MyReal

Will obtain a quad real number, if it is available. Alternatively, with Fortran 2008 or later you can "use ISO_FORTRAN_ENV" and then have access to the kind value REAL128. The kind values will be -1 if the precision is unavailable.

A related question: What does `real*8` mean?



回答2:

Use kinds for modern Fortran code, i.e.

real(some_kind_value) :: variable

You can then use selected_real_kind() or iso_fortran_env module or c_long_double kind value from iso_c_binding module to get the kind variable. All of these have slightly different meaning.

You can use epsilon(), tiny(), huge() or nearest() intrinsics to assess the actual precision of your code.

Quad precision in gfortran generally requires libquadmath library that should be available for most platforms, but maybe not by default.



回答3:

The gfortran documentation for kind type parameters answers this question, in particular the last sentence, which reads:

The available kind parameters can be found in the constant arrays CHARACTER_KINDS, INTEGER_KINDS, LOGICAL_KINDS and REAL_KINDS in the ISO_FORTRAN_ENV module (see ISO_FORTRAN_ENV).

What you may have discovered is that real*16 is not implemented on your platform.



回答4:

real(kind=10) is the so-called extended 80-bit precision Wikipedia 80-bit.

real(kind=16) is the proper quadruple 128-bit precition Wikipedia 128-bit.

As already mentioned, you can employ selected_real_kind(), epsilon(), tiny(), huge() to select and check the precision to be used.



回答5:

Try the gfortran command

gfortran -fdefault-real-8 test.f -o test.exe

on the test file

      implicit double precision(a-h,o-z)
 1    continue
      print * , ' i:'
      read(5,*) i
      if(i.le.0) stop
      a=i
      b=sqrt(a)
      print * , b
      c=b*b
      print * , c
      goto 1
      end

On my platform (FEDORA 20) it works.