- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, all! How can I compute 2**31 in Fortran? I set 'integer(kind=8)::i', but there seems always an overflow!
The code:
[fortran]program ex implicit none integer(kind=8)::i i=2**31-1 write(*,*) i i=i+1 write(*,*) i i=2**31 write(*,*) i stop end[/fortran]The result:
[bash]xwu@mac:/tmp> ifort -m64 ex.f90 xwu@mac:/tmp> ./a.out 2147483647 2147483648 -2147483648[/bash]
Here's another related question. Could the correct result be guaranteed,if I am doing arithmetic or comparison of different _kinds_ of integers? _kind_ means the number of bytes. It seems that integer is always singed in Fortran (Intel Fortran Compiler User and Reference Guides, Document Number: 304970-006US, page 174).
Thank you in advance!
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The compiler option '-i8' is a workaround. Is there anything eles (in the code) can help?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As the data type for evaluation of an expression is determined from its components, you may have meant 2_8**31 or equivalent. As both operands will be promoted to the wider type, that is the same as 2**31_8 or 2_8**31_8, or ishft(2_8,31) etc. Likewise 2_8**31 > 0 is the same as 2_8**31 > 0_8.
Of course, it's better form to define a parameter constant for the next kind bigger than 32-bit size which happens to be 8 in ifort and gfortran but could also work with compilers such as SilverFrost.
Of course, it's better form to define a parameter constant for the next kind bigger than 32-bit size which happens to be 8 in ifort and gfortran but could also work with compilers such as SilverFrost.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you, Tim!

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page