Dan,
I'd like to see the example which fails with bad exponent value, including declarations.
I think you have to be careful with constant definition, to ensure the implied precision. The following example code fails for the last two lines, but the previous 3 do work.
module kind_precision
integer, parameter :: int_1byte = selected_int_kind (2) ! 1
integer, parameter :: int_2byte = selected_int_kind (4) ! 2
integer, parameter :: int_4byte = selected_int_kind (9) ! 3
integer, parameter :: int_8byte = selected_int_kind (18) ! 4
integer, parameter :: real_float = selected_real_kind (6,37) ! 1
integer, parameter :: real_double = selected_real_kind (15,307) ! 2
integer, parameter :: real_long = selected_real_kind (18,4931) ! 3
end module kind_precision
use kind_precision
!
integer ( kind=int_4byte ) :: i,k, last_k
character kind_name*10
real ( kind=real_double ) :: x
real ( kind=real_long ) :: y
!
! confirm integer precision
write (*,*) ' '
write (*,*) 'Test of integer KIND'
last_k = -99
do i = 50,-50,-1
k = selected_int_kind (i)
if (k == last_k) cycle
kind_name = ' undefined'
if ( k == int_1byte ) kind_name = '1 byte'
if ( k == int_2byte ) kind_name = '2 byte'
if ( k == int_4byte ) kind_name = '4 byte'
if ( k == int_8byte ) kind_name = '8 byte'
write (*,*) kind_name, ' precision =', i, ' kind =',k
last_k = k
end do
!
! confirm real precision
write (*,*) ' '
write (*,*) 'Test of Real precision KIND'
last_k = -99
do i = 50,-50,-1
k = selected_real_kind (i,1)
if (k == last_k) cycle
kind_name = ' undefined'
if ( k == real_float ) kind_name = '4 byte'
if ( k == real_double ) kind_name = '8 byte'
if ( k == real_long ) kind_name = '10 byte'
write (*,*) kind_name, ' precision =', i, ' kind =',k
last_k = k
end do
!
! confirm real exponent
write (*,*) ' '
write (*,*) 'Test of Real exponent KIND'
last_k = -99
do i = 5000,-5000,-1
k = selected_real_kind (1,i)
if (k == last_k) cycle
kind_name = ' undefined'
if ( k == real_float ) kind_name = '4 byte'
if ( k == real_double ) kind_name = '8 byte'
if ( k == real_long ) kind_name = '10 byte'
write (*,*) kind_name, ' exponent =', i, ' kind =',k
last_k = k
end do
!
! test real constants
!
write (*,*) ' '
x = 1.0e300_real_double ; write (*,*) 'x=',x
y = 1.0e3000_real_long ; write (*,*) 'y=',y
x = 1.0d300 ; write (*,*) 'x=',x
! x = 1.0e300 ; write (*,*) 'x=',x
! y = 1.0e3000 ; write (*,*) 'y=',y
!
end
You might like to consider defining your own Fortran 2008 ISO_C_BINDING module, as this provides some standardisation to naming kinds.
We have to suffer because Fortran 90 did not recommend that KIND = number_of_bytes. You can try to be too smart !!
I'd like to know the Fortran 90+ implementations where this could not have been done. Were there any 60-bit Cyber computers still around in 1990 ?
John