What @fortran4r wrote works for 3d9, but I wonder how to recover the correct integer when the floating point value is near huge(i). I was going to suggest using the nint function, but the output of the program
program convert
use iso_fortran_env, only: int64
implicit none
integer, parameter :: dp = kind(1.0d0)
real(kind=dp) :: x,x0
integer(kind=int64) :: i,j,k
do j=0,512
k = huge(i) - j
x = real(k,kind=dp)
if (j == 0) x0 = x
i = x
if (modulo(j,100_int64) == 0 .or. i > 0) print*,j,k,x0==x,x,i,nint(x,kind=int64)
end do
end program convert
is
0 9223372036854775807 T 9.2233720368547758E+018 -9223372036854775808 -9223372036854775808
100 9223372036854775707 T 9.2233720368547758E+018 -9223372036854775808 -9223372036854775808
200 9223372036854775607 T 9.2233720368547758E+018 -9223372036854775808 -9223372036854775808
300 9223372036854775507 T 9.2233720368547758E+018 -9223372036854775808 -9223372036854775808
400 9223372036854775407 T 9.2233720368547758E+018 -9223372036854775808 -9223372036854775808
500 9223372036854775307 T 9.2233720368547758E+018 -9223372036854775808 -9223372036854775808
512 9223372036854775295 F 9.2233720368547748E+018 9223372036854774784 9223372036854774784
so the signs of the integer variables are reversed for x equal to huge(i) but not huge(i)-512.