What shall be used in Modern Fortran to specify a 8 bytes real variable if iso_fortran_env is not supported?

Viewed 93

I want to specify as type of a subroutine a floating point value (real) of 8 bytes precision.

I have read here that the modern way to do it would be:

real(real64), intent(out)       :: price_open(length)

However, iso_fortran_env is not supported by f2py (same as it does not support iso_c_bindings either).

I get errors of this type:

   94 |       real(kind=real64) price_open(length)
      |                1
Error: Parameter 'real64' at (1) has not been declared or is a variable, which does not reduce to a constant expression

The link referenced before states that using kind would be the proper way if iso_fortran_env is not available and that real*8 shall be avoided.

  • I have been using real(8) is that equivalent to using kinds? If not, what shall I use?
  • What is wrong with real*8 if I want to always enforce 8 bytes floating point values?
1 Answers

You say you are specifically interested in interoperability with C. However, iso_c_binding, nor iso_fortran_env are supported. These modules have constants that help you to set the right kind constant for a given purpose. The one from iso_fortran_env would NOT be the right one to choose anyway, it would be c_double.

If these constants meant to help you in your choice are not available, you are on your own. Now you can choose any other method freely.

It is completely legitimate to use just kind(1.d0) and just check that the connection to C works. Automake had been doing that for ages.

Or use selected_real_kind(), it does not matter, it is really up to you. Just check that double in C ended up being the same numeric type.

The traditional thing in automatic build processes was to do many tests about which (mainly C) constant ended up having which value. You just need to check that double precision in Fortran does indeed correspond to double in C. It is very likely, but just check it. You can have a macro that changes the choice if not, but probably it is a needless work untill you actually meet such a system.

Related