Silverfrost Forums

Welcome to our forums

Casting of an integer as real

23 Nov 2016 1:18 #18455

This doesn't seem to work for large enough values

PROGRAM MAIN
	INTEGER(KIND=7) ITEST
	REAL*8 RTEST
	ITEST = 2069434590944
	RTEST = ITEST
	PRINT*,'ITEST=',ITEST
	PRINT*,'KIND(ITEST)=',KIND(ITEST)
	PRINT*,'RTEST=',RTEST
	PRINT*,'KIND(RTEST)=',KIND(RTEST)

END PROGRAM

Output: ITEST= 2069434590944 KIND(ITEST)= 4 RTEST= 2.069434590944E+12 KIND(RTEST)= 2

23 Nov 2016 3:18 (Edited: 26 Nov 2016 7:44) #18456

The constant 2069434590944 is too big for default kind integer, so is 4069434590944, and similarly any integer greater than 2^31 - 1. Change the format to 4069434590944_ip, where 'ip' has been previously declared as a kind parameter (e.g. 4. or using the intrinsic SELECTED_INT_KIND()) for large integers, or append '_4'. Some compilers may not check the expression for range checks unless a suitable compiler option is in effect.

Note that kind numbers are not necessarily portable, so _4 may give problems when you use a different compiler than FTN95.

26 Nov 2016 2:39 #18480

There is no simple way to define an integer*8 constant > 2147483647 other than using a kind suffix, using either _4, _i8 or _int64. The fortran standard says that 2147483648 is a standard integer value and so would be truncated or an error reported. You could try modifying the following program to see the error messages.

!  program to test integer constants
!    integer*4 :: i = 2147483648
!    integer*8 :: j = 2147483648_4
    integer*4 i
    integer*8 j
    integer*8 :: k = 2147483648_7
!
    i = k
!    j = 2147483648
    j = k+1
    write (*,*) i,j,k
    end

I would recommend reviewing ISO_FORTRAN_ENV and replicating some of these constants when using FTN95 to define 64 bit integers.

Please login to reply.