3

Let's say if I want a double-precision real, what is the correct way to set the precision of the variable? But not just limited to double precision. I am trying to teach myself Fortran. I have been looking at books, websites and codes. And everywhere I look, I see contradicting stuff, which might indicate that the websites have not been updated.

I have seen:

real*8 :: var
real(8) :: var
real(kind=dp) :: var

All of these seem to do the same thing (I might be wrong). Which is the standard way of doing this for any type of variable (real, integer, complex, logical, etc.)?

Like for complex numbers:

complex(16) :: var
complex(kind(0d0)) :: var

which also seem to be the same (I might be wrong again), but do appear differently in different resources.

AR.walker
  • 71
  • 5
  • 2
    About to go to bed, but quickly real*8 is not and has never been standard Fortran and should never be used; real(8) is equivalent to real(kind=dp) if dp=8 but should not be used as the compiler is free to use any positive integer to represent a double, it does not have to be 8; real(kind=dp) is what should be used in conjunction with choosing the value of dp using either selected_real_kind, or one of the constants in iso_fortran_env. If not answered tomorrow will write this properly – Ian Bush Nov 19 '19 at 22:43
  • Are you after specifically 'double precision'? If so, what do you mean by that and how does that relate to your question about integer, complex, character and logical? If not, how do you wish to define the parameters? – francescalus Nov 19 '19 at 22:58
  • I just noticed how my question veers off course (and is incorrect in some places). I will edit it, sorry. I am not specifically looking for double-precision, but any level of precision. One of the things I was confused about, for example, was complex(kind(0d0)) in a very old code. – AR.walker Nov 19 '19 at 23:02
  • The linked question addresses the part about the three 'styles' of declaration (there about integers, but it's the same concept for reals). If that isn't relevant to what you are interested in, I'll re-examine after your edit. – francescalus Nov 19 '19 at 23:06
  • Regarding complex declarations, the kind of a complex type matches the kind if the real for the real and imaginary components. – francescalus Nov 19 '19 at 23:07
  • The linked question does solve most of my problem, thank you. I still don't really understand what you said about the complex numbers though – AR.walker Nov 19 '19 at 23:08
  • 2
    For a declaration like `complex(kind=kind(0d0)) x` then we have that `x` has real and imaginary parts which are of type real, each of kind `kind(0d0)`. A real of kind `kind(0d0)` is a `double precision`. It's a 'double precision complex'. – francescalus Nov 19 '19 at 23:19
  • Thank you for the explanation! But am I understanding this correctly that complex(16) is also the same thing or am I getting this wrong? – AR.walker Nov 19 '19 at 23:21
  • Hopefully the newly added linked question answers the subtle part about the complex declaration. – francescalus Nov 19 '19 at 23:36
  • Yes, the links are extremely helpful, I think I now understand this very clearly. Thank you for your help. I guess I got this wrong. – AR.walker Nov 19 '19 at 23:43

0 Answers0