How to use and verify quad precision in gfortran?

17,387

Solution 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?

Solution 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.

Solution 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.

Solution 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.

Share:
17,387
drjrm3
Author by

drjrm3

BS - Mathematics MS - Mathematics MS - Computer Science PhD - Computational Science

Updated on June 05, 2022

Comments

  • drjrm3
    drjrm3 almost 2 years

    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?

  • M. S. B.
    M. S. B. almost 12 years
    A caution: using specific numeric values for kind parameters is not portable across compilers.
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten about 11 years
    "real*10 is not quad precision. Is is called "extended" -- it is a 10 byte type provided by Intel processors." Also available on Motorola 68k chips.
  • Nike
    Nike over 10 years
    Is that: read (kind=QR_K) :: MyReal <or> real (kind=QR_K) :: MyReal ?
  • M. S. B.
    M. S. B. over 10 years
    Yes, a typo. It should be real, not read. Answer corrected.
  • Vladimir F Героям слава
    Vladimir F Героям слава over 9 years
    This is really awful. The side-effect having ALL the double precision variables as quad precision can be very annoying. I also bet that if real*16 does not work, this actually will not promote the doubles the quad and thus won't solve the original problem.