Fortran Easy · Lesson 1 · On the house

Kinds and precision

real64 from iso_fortran_env, kind suffixes on literals, and precision traps. Lesson 1 is free — on the house.

Lessons · Fortran Easy · Lesson 1 of 10 · On the house

Kinds and precision

real64 from iso_fortran_env, kind suffixes on literals, and precision traps.

Illustration for Fortran
Lesson 1 is on the house.

Default real is usually 32-bit (about 7 digits). For scientific work use real(real64) from use iso_fortran_env, which is a 64-bit double.

Literals have kinds too. 0.1 is a default real even when assigned to a real64, so precision is lost before assignment. Write 0.1_real64 (or 0.1d0 in older code).

int32, int64, real32, real64 and real128 (where supported) name portable kinds. selected_real_kind(p=15) asks for at least 15 digits.

Inquiry functions: epsilon(x), huge(x), tiny(x) and precision(x) describe a kind's limits.

Integer overflow is not checked by default; use int64 for large counts such as array sizes and products of big numbers.

kinds

program kinds
  use iso_fortran_env, only: real32, real64, int64
  implicit none
  real(real64) :: a, b
  integer(int64) :: big
  a = 0.1
  b = 0.1_real64
  print '(f20.17)', a
  print '(f20.17)', b
  big = 3000000000_int64
  print '(i0)', big
  print '(i0,1x,i0)', precision(1.0_real32), precision(1.0_real64)
end program kinds
Match this when you type.

Quiz

Why does a = 0.1 lose precision even though a is real64?

Quiz

How do you write a 64-bit real literal?

Quiz

Which module provides real64 and int64?

Quiz

What does precision(1.0_real64) return?

Check

Match the still for: Kinds and precision.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.