|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
StamK
Joined: 12 Oct 2016 Posts: 159
|
Posted: Wed Nov 23, 2016 2:18 pm Post subject: Casting of an integer as real |
|
|
This doesn't seem to work for large enough values
Code: | 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 |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1886
|
Posted: Wed Nov 23, 2016 4:18 pm Post subject: |
|
|
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.
Last edited by mecej4 on Sat Nov 26, 2016 8:44 am; edited 2 times in total |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Sat Nov 26, 2016 3:39 am Post subject: |
|
|
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.
Code: | ! 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. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|