Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28480 Discussions

strange behaviour from 64-bit integer

Alireza_Forghani
Beginner
526 Views

I'm seeing a strange behaviour from int64 integer. Following code: 

    use iso_fortran_env
    implicit none

    integer (int64) :: i
    
    i=2**32-2
    
write (*,*) "i, btest (i,0)" , i, btest (i,0)
    write (*,*) "i, btest (i,31)" , i, btest (i,31)
    write (*,*) "i, btest (i,32)" , i, btest (i,32)
    write (*,*) "i, btest (i,33)" , i, btest (i,33)
    write (*,*) "huge (i)" , huge (i)

returns: 

 i, btest (i,0)                    -2 F
 i, btest (i,31)                    -2 T
 i, btest (i,32)                    -2 T
 i, btest (i,33)                    -2 T
 huge (i)   9223372036854775807

which shows that it calculates 2^32-2 as -2. Whereas limit on int64 is  9223372036854775807 that is way bigger than 2^32!

Wonder if I'm doing something wrong!

Thanks

 

0 Kudos
1 Solution
IanH
Honored Contributor II
526 Views

The expression on the right hand side of the assignment to i is not using 64 bit integers.

Consider the difference between the print statements in the following:

  USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: INT64
  IMPLICIT NONE
  PRINT *, 2**32-2
  PRINT *, 2_INT64**32-2
END

 

View solution in original post

0 Kudos
2 Replies
IanH
Honored Contributor II
527 Views

The expression on the right hand side of the assignment to i is not using 64 bit integers.

Consider the difference between the print statements in the following:

  USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: INT64
  IMPLICIT NONE
  PRINT *, 2**32-2
  PRINT *, 2_INT64**32-2
END

 

0 Kudos
Alireza_Forghani
Beginner
526 Views

Thanks Ian. the problem is fixed!

0 Kudos
Reply