Size of long array

With gfortran and ifort there are options to set real, double precision, and default integer variables to 64, 128, and 64 bits, so the user can easily change the defaults or use the kind constants from iso_fortran_env. For the code

program main
use iso_fortran_env
implicit none
real :: x
integer :: i
logical :: b
double precision :: d
print "(*(1x,i0))", storage_size(x), storage_size(i), storage_size(b), storage_size(d)
print "(*(1x,i0))", storage_size([real(real32) ::]), storage_size([real(real64) ::]), &
                    storage_size([real(real128) ::]) 
print "(*(1x,i0))", storage_size([integer(int16) ::]), storage_size([integer(int32) ::]), &
                    storage_size([integer(int64) ::])
end program main

the output with ifort /integer-size:64 /real-size:64 /double-size:128 or gfortran -fdefault-integer-8 -fdefault-real-8 is

 64 64 64 128
 32 64 128
 16 32 64

I don’t see any magic there. That number, 16777216, is 2^{24}, and can be represented exactly as the IEEE 32 bit real = Z’4B800000’. If you add 1 to this real number, the corresponding 32-bit real, with its 23+1 bit mantissa, will no longer represent the integer exactly. The second next number, 16777218, has the 32-bit real representation of Z’4B800001’. In other words, the consecutive integers 2^{24} and 2^{24}+1 both have the same IEEE-32 representation Z’4B800000’ .

1 Like

I thoroughly agree. You should put all variable type selections in the code.
Selecting warnings, optimisation and even –fopenmp (which probably should be in the code) by compile options but not default integer and real kinds. It really is not a sufficiently robust approach.

I didn’t mean any wizard with a wand. Really :slight_smile: