Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 39 ยท Language Basics

Kinds and Precision

Control the size and precision of Fortran numbers with kind parameters: selected_real_kind, selected_int_kind, iso_fortran_env, double precision and literal suffixes.

Numerical programs care about how many digits a number carries. A weather model may need 15 digits where a game needs 6. Fortran handles this with kind parameters: every intrinsic type comes in several kinds, and you pick the one you need. This is one of the language's strengths, and getting it right early prevents the classic "why did my answer change on a different compiler?" bug.

What a kind is

A kind is a small integer attached to a type, written in parentheses: real(kind=8) or real(8). The integer value is compiler-specific. With gfortran, kind 4 is a 32-bit real and kind 8 is a 64-bit real, but another compiler might number them differently. That is why you should never hard-code numbers like 8 in portable code. Instead, ask for what you need and let the compiler pick the right kind.

FORTRAN
program kinds_demo
  implicit none
  print *, "default real kind   :", kind(1.0)
  print *, "default double kind :", kind(1.0d0)
  print *, "default integer kind:", kind(1)
end program kinds_demo

With gfortran this prints 4, 8 and 4 respectively (each with a leading space and spacing between the text and the number).

Choosing precision portably

There are three good ways to choose a kind.

1. selected_real_kind

Ask for a minimum number of decimal digits (and optionally an exponent range):

FORTRAN
integer, parameter :: sp = selected_real_kind(6, 37)    ! at least 6 digits
integer, parameter :: dp = selected_real_kind(15, 307)  ! at least 15 digits

real(sp) :: a
real(dp) :: b

2. iso_fortran_env

Since Fortran 2008, a standard module provides named kinds:

FORTRAN
use, intrinsic :: iso_fortran_env, only: sp => real32, dp => real64, i8 => int64

real(dp) :: x
integer(i8) :: big

real32, real64, real128, int8, int16, int32 and int64 are the number of bits. This is the cleanest approach today.

3. Kinds of literals

A literal needs its kind too. 1.0 is a default (single) real; 1.0d0 is a double precision literal; 1.0_dp uses the named kind:

FORTRAN
real(dp) :: third
third = 1.0_dp / 3.0_dp

The _dp suffix is the portable way. Forgetting it is a famous bug: real(dp) :: x = 0.1 stores 0.1 in single precision (the literal is single) and then widens it, giving 0.100000001490116... instead of the true double nearest to 0.1.

flowchart LR
  A["0.1<br/>default real literal"] -->|widen| B["real(dp) variable<br/>0.10000000149..."]
  C["0.1_dp<br/>double literal"] -->|store| D["real(dp) variable<br/>0.10000000000000001"]

The suffix decides how many digits the literal carries, regardless of the variable's kind.

Double precision

Old code uses the keyword double precision, which is a real with at least twice the precision of the default real:

FORTRAN
double precision :: pi_approx
pi_approx = 3.141592653589793d0

The d0 exponent form makes a double literal. It still works, but new code should prefer real(dp) with _dp literals.

Python Stand-in (Python): single vs double digits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Integer kinds

Integers have kinds too. selected_int_kind(r) returns a kind that can hold every integer with r decimal digits:

FORTRAN
integer, parameter :: i4 = selected_int_kind(9)    ! up to 999999999
integer, parameter :: i8 = selected_int_kind(18)   ! up to 10**18 - 1

integer(i8) :: population
population = 7900000000_i8

Without the _i8 suffix, a literal beyond the default integer range is a compile error or overflows. Use 64-bit integers for counters that can pass two billion, such as array indices of huge data sets, file sizes or factorials.

Python Stand-in (Python): where 32-bit integers run out

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Inquiry functions

These intrinsic functions describe a kind. Pass any value of that kind:

Function Returns
kind(x) the kind number of x
precision(x) decimal digits of precision of a real
range(x) decimal exponent range (or integer digits)
epsilon(x) smallest number that changes 1.0 when added to it
huge(x) the largest representable value
tiny(x) the smallest positive normal real
digits(x) number of binary digits in the model

A typical single-precision real has precision 6, epsilon about 1.19e-07 and huge about 3.4e38. A double has precision 15, epsilon about 2.2e-16 and huge about 1.8e308.

Python Stand-in (Python): epsilon and huge for double

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A kinds module for your projects

Most projects start with a tiny module that defines the working precision once:

FORTRAN
module precision
  use, intrinsic :: iso_fortran_env, only: real64, int64
  implicit none
  integer, parameter :: wp = real64     ! working precision
  integer, parameter :: ip = int64
end module precision

program use_precision
  use precision
  implicit none
  real(wp) :: x
  x = 2.0_wp ** 0.5_wp
  print *, x
end program use_precision

If you ever need to switch the whole program to single precision (for speed on a GPU, say) you change one line. This is the biggest practical benefit of kinds.

Common mistakes

  • Using bare real for scientific work and trusting the digits.
  • Writing real(8) and hoping it means double on every compiler.
  • Mixing literals without _dp: x = 0.1 into a real(dp) loses digits.
  • Passing real(dp) to a procedure expecting real(sp); with modules the compiler catches this.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the most portable way to ask for at least 15 digits of precision?

    selected_real_kind asks for what you need and lets the compiler choose the kind number, while 8 is compiler-specific.
  2. 2. What does the literal 0.1_dp do differently from 0.1?

    The _dp suffix gives the literal the dp kind, so all its digits are kept; plain 0.1 is single precision.
  3. 3. Which module provides real64 and int64?

    iso_fortran_env provides named kinds such as real32, real64, int32 and int64.
  4. 4. Which function returns the largest value of a type?

    huge(x) returns the largest number of x's type and kind; epsilon gives the relative spacing near 1.

Summary

  • A kind selects the size and precision of a type; kind numbers differ between compilers.
  • Choose kinds with selected_real_kind, selected_int_kind or iso_fortran_env.
  • Give literals the right kind with a suffix such as 1.0_dp.
  • Inquiry functions (kind, precision, epsilon, huge, tiny) describe a kind.
  • Define the working precision once in a module.

Next: Operators and Expressions