REAL_PRECISION
Choosing the Precision of Real Arithmetic


REAL_PRECISION is a directory of FORTRAN90 programs which illustrate the procedure for choosing the precision with which real variables and constants are stored.

FORTRAN90 added a number of arithmetic inquiry functions:

The function ID = SELECTED_REAL_KIND(P,R) can be used to request a "kind" (which will be an integer) that identifies a real type with the given number of decimal digits of precision and exponent range.

Sadly, the reality is that each compiler is free to choose the values of ID that are valid, as well as the number and properties of its real data types. The only requirement is that there be at least two real types, with one more accurate than the other! Moreover, for historical reasons, any compiler may support nonstandard types, such as REAL*16. Thus, if you are used to hardcoding declarations such as "real ( kind = 8 ) x", the fact that they work on your system is no guarantee they will even compile on another compiler.

Only the use of the selected_real_kind function allows you to write code that will at least compile, if not run. (It might not run because the precision you requested might not be available. My compiler returns a -1 in that case for the identifier, and if I use that identifier as the kind value, the program aborts.)

Real variables can be declared to have a particular type using a declaration such as real ( kind = id ) r

Constants can be specified to beof the given type by appending an underscore and the kind identifier:

        1.0_id
        3.14159_id
        1.0E+17_id
      

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

REAL_PRECISION is available in a FORTRAN90 version.

Related Data and Programs:

F90, FORTRAN90 programs which illustrate features of the FORTRAN90 language.

G95_QUADMATH, a FORTRAN90 program which illustrates the use of quadruple precision real arithmetic provided on some systems by the G95 compiler for FORTRAN.

GFORTRAN_QUADMATH, a FORTRAN90 program which illustrates the use of quadruple precision real arithmetic provided on some systems by the Gnu GFORTRAN compiler for FORTRAN90.

Reference:

  1. Jeanne Adams, Walter Brainerd, Jeanne Martin, Brian Smith, Jerrold Wagener,
    Fortran90 Handbook,
    Complete ANSI/ISO Reference,
    McGraw Hill, 1992,
    ISBN: 0-07-000406-4,
    LC: QA76.73.F28.F67.
  2. Ian Chivers, Jane Sleightholme,
    Introduction to Programming with Fortran,
    Springer, 2005,
    ISBN: 1846280532,
    LC: QA76.73.F29.C48.
  3. Miles Ellis, Ivor Philips, Thomas Lahey,
    Fortran90 Programming,
    Addison-Wesley, 1994,
    ISBN: 0-201-54446-6,
    LC: QA76.73.F25E435.
  4. Michael Metcalf,
    Fortran95/2003 Explained,
    Oxford, 2004,
    ISBN: 0198526938,
    LC: QA76.73.F235.M48.
  5. Larry Nyhoff, Sanford Leestma,
    Introduction to Fortran90 for Engineers and Scientists,
    Prentice-Hall, 1996,
    ISBN: 0135052157,
    LC: QA76.73.F25N925.
  6. James Ortega,
    An Introduction to FORTRAN90 for Scientific Computing,
    Oxford, 1994,
    ISBN: 0-19-517213-2,
    LC: QA76.73.O75.
  7. GNU,
    GFORTRAN Reference Manual
  8. GNU,
    G95 Reference Manual
  9. IBM Corporation,
    XLF Language Reference Manual
  10. Intel Corporation,
    Intel Fortran Language Reference.

Examples and Tests:

REAL_KIND16 attempts to declare single, double and quadruple precision real data using the NONSTANDARD statements REAL ( KIND = 4 ), REAL ( KIND = 8 ), and REAL ( KIND = 16 ). The G95 compiler seems to support this language, while GFORTRAN does not.

REAL_KIND_DEFAULT declares a real variable of default type and then uses inquiry functions to determine the characteristics of that type.

REAL_KIND_PRECISION6 requests a real type that guarantees a precision of 6 decimal digits, and uses inquiry functions to determine the characteristics of that type.

REAL_KIND_PRECISION12 requests a real type that guarantees a precision of 12 decimal digits, and uses inquiry functions to determine the characteristics of that type.

REAL_KIND_PRECISION18 requests a real type that guarantees a precision of 18 decimal digits, and uses inquiry functions to determine the characteristics of that type.

REAL_KIND_PRECISION24 requests a real type that guarantees a precision of 24 decimal digits, and uses inquiry functions to determine the characteristics of that type. This request FAILS with the GFORTRAN compiler, but succeeds with the G95 compiler.

You can go up one level to the FORTRAN90 source codes.


Last revised on 17 February 2010.