Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any platform where Fortran `double precision` is different from C `double`?

For interoperability with C programs, the 2003 revision of the Fortran standard introduced a module iso_c_binding, which allows one to write things like this:

USE iso_c_binding, ONLY: c_int, c_float
INTEGER(KIND=c_int) :: x
REAL(KIND=c_float) :: y

which is guaranteed to map exactly to a int x and a float y on the C-side. Now for integers, this is crucial since the default INTEGER type may (depending on compiler and platform) very well wrap to a 64-bit integer type even when int is 32-bit.

However, for floating point types, the following mappings seem almost unavoidable:

REAL*4  (or)  REAL               ->  float
REAL*8  (or)  DOUBLE PRECISION   ->  double

So here's the question: Is there any practical platform or compiler where this is not satisfied, e.g., where sizeof(REAL*4) != sizeof(float)?

like image 652
summentier Avatar asked Nov 30 '25 04:11

summentier


1 Answers

Consider gfortran which has the compile-time options -freal-4-real-8, -freal-8-real-16, etc. These change the size of a Fortran real, including those declared with the non-standard real*4.

Consider also ifort:

  use, intrinsic :: iso_c_binding
  print*, C_SIZEOF(0._c_double).eq.C_SIZEOF(0d0)
end program

compiled with/without -double-size 128 I see F/T.

Of course, as Vladimir F has commented, even excluding the size of the types there is more to consider in terms of interoperability. It really is desirable to use real(c_double) ... to give an interoperable real.

like image 185
francescalus Avatar answered Dec 02 '25 22:12

francescalus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!