Jason Waldrop
Jason Waldrop

Reputation: 181

Use of selected_real_kind in Fortran to achieve desired minimum precision

I am using gfortran 4.7.1 on Mac OS X 10.8 to compile a simple program using a specific precision:

program simple
  implicit none
  integer, parameter :: l = selected_real_kind(18,100)
  real(kind=l) :: myNum
  myNum = 0.123456789123456789
  print '(f18.12)', myNum
end program simple

When I compile and run I get 0.123456791043 which obviously indicates I'm not getting the precision I called for.

However if I compile with -fdefault-real-8 I get the right answer. Obviously, I could just move on and use the option, but I can't help wondering whether this is a bug or I am missing some justification. Do I really have to specify my real size outside of the code itself when it seems that selected_real_kind intent is to allow the code to specify precision?

Upvotes: 2

Views: 666

Answers (1)

Jason Waldrop
Jason Waldrop

Reputation: 181

Sorry. You have to add _l to the number assignment.

Upvotes: 2

Related Questions