Eliad
Eliad

Reputation: 926

Are the specific double precision functions in Fortran useful?

I have noticed using gfortran that the tan function returns real values of the same kind as its argument; e.g. one can pass real(kind=4), real(kind=8) or real(kind=16) to it and get results in real(kind=4), real(kind=8) or real(kind=16) respectively.

Double precision functions like dtan, on the other hand, are not as flexible. If the default double kind is 8[1] they only accept and return real(kind=8) and if the default kind is 16[2], they only accept and return real(kind=16) .

Are these defined behaviors and if so, what is the use case for the double precision functions or are they obsolete?


[1] if compiled with -fdefault-double-8 or without -fdefault-real-8

[2] if compiled with -fdefault-real-8 and without -fdefault-double-8

Upvotes: 5

Views: 1286

Answers (1)

The specific precision function such as dtan are effectively unnecessary since FORTRAN 77. That's a long time ago. Some still use them, but they should be used as little as possible (I'd say never). FORTRAN 77 brought generic intrinsic functions and that is what should normally be used. You just have to be sure to check the kind of the argument. With the d... functions the compiler will complain if the argument is not double precision.

But they are completely valid Fortran 2008. There was some change regarding their obsoleteness in Fortran 2018, but I am not sure what exactly. In Fortran 2018, however, they are declared obsolescent (see also Steve's comment). I think it will also not be possible to pass them as procedure arguments to other procedures.

As a final note, real(kind=4), real(kind=8) or real(kind=16) is NOT a portable notation. Kind numbers 4, 8 and 16 do not have to exist at all or can mean something different in various compilers. The kind number is NOT the number of bytes. Indeed certain compilers use kind numbers 1, 2 and 3. See Fortran 90 kind parameter

Upvotes: 6

Related Questions