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.

asked Nov 19, 2019 at 22:28
10
  • 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 Commented Nov 19, 2019 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? Commented Nov 19, 2019 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. Commented Nov 19, 2019 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. Commented Nov 19, 2019 at 23:06
  • 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'. Commented Nov 19, 2019 at 23:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.