forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Casting of an integer as real

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> 64-bit
View previous topic :: View next topic  
Author Message
StamK



Joined: 12 Oct 2016
Posts: 159

PostPosted: Wed Nov 23, 2016 2:18 pm    Post subject: Casting of an integer as real Reply with quote

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
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1886

PostPosted: Wed Nov 23, 2016 4:18 pm    Post subject: Reply with quote

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
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Sat Nov 26, 2016 3:39 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> 64-bit All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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