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

List-directed Output

Blane_J_
New Contributor I
630 Views

Hi. Since I am doing some fundamental calculation, this problem came up, as:

! This is good.
integer(8), parameter :: max_int64 = int(16#8000000000000000, kind = 8)
write(*,*) max_int64

! This is bad.
write(*,*) int(16#8000000000000000, kind = 8)

The upper case gives an correct output of ‭9151314442816847872‬ but the lower one prints only 0. why is that? Thanks for any help.

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
630 Views

I would expect these to be the same, but I can imagine why the compiler has a problem with it. Nevertheless, you are on extremely thin ice with your use of this non-standard syntax from a time when 64-bit didn't exist on the platform.  If I interpret the documentation correctly, the expected value is zero because the # syntax for "integer constants" has a kind of "default integer". I won't even start to ask why you are calling this hex value MAX_INT64 since it is not at all the maximum 64-bit integer value - indeed, it is the minimum integer value. If you truly want the largest integer value for kind 8, use HUGE(0_8).

View solution in original post

0 Kudos
9 Replies
Steve_Lionel
Honored Contributor III
631 Views

I would expect these to be the same, but I can imagine why the compiler has a problem with it. Nevertheless, you are on extremely thin ice with your use of this non-standard syntax from a time when 64-bit didn't exist on the platform.  If I interpret the documentation correctly, the expected value is zero because the # syntax for "integer constants" has a kind of "default integer". I won't even start to ask why you are calling this hex value MAX_INT64 since it is not at all the maximum 64-bit integer value - indeed, it is the minimum integer value. If you truly want the largest integer value for kind 8, use HUGE(0_8).

0 Kudos
Blane_J_
New Contributor I
630 Views

I see, and I have found the page about "Determining the Data Type of Nondecimal Constants" in the manual. Thanks Steve and BTW, max is a mistake.

0 Kudos
Steve_Lionel
Honored Contributor III
630 Views

The section you want is "Integer Constants" - the # syntax is treated differently from the BOZ syntax.

0 Kudos
andrew_4619
Honored Contributor II
630 Views
      ! This is good.
      integer(8), parameter :: max_int64=int(Z'8000000000000000', kind = 8 )
      write(*,*) max_int64
      ! This is good
      write(*,*) int(Z'8000000000000000', kind = 8 )

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
630 Views

FWIW

Z'8000000000000000'

Has sign bit set and remainder bits are 0 thus representing min_int64.

(Z'8000000000000000' - 1_8) would result in sign bit 0 and remainder bits set to 1's would represent max_int64

And , -HUGE(0_8) would produce a number 1 greater than the smallest negative number.

It is disputable as to if Z'8000000000000000' represents -0, or potentially arguable as an integer NaN, though the hardware will treat this as .lt. 0.

Jim Dempsey

 

0 Kudos
TimP
Honored Contributor III
630 Views
I believe this case, where there is a number 1 less than -huge(selected_int_kind..), isn't covered by Fortran standard. It does occur for all the usual implementations of 2's complement integer, but presents an opportunity for non-portability. Of course, the requested use of the 16# extension and the specific kind value 8 affirm the intent of non-portability.
0 Kudos
Blane_J_
New Contributor I
630 Views

So for safely use, maybe I should turn to the boz form all the time for constants based other than 2,8,16 are not frequently used anyway. Thanks for all.

0 Kudos
andrew_4619
Honored Contributor II
630 Views

Blane J. wrote:
So for safely use, maybe I should turn to the boz form all the time for constants based other than 2,8,16 are not frequently used anyway. Thanks for all.

The BOZ form is standards compliant so is the best way even though it seems a little clumsy at times. I aim to be able to compile any code I work on with standards checking on, it is a good thing to aim for.

 

 

0 Kudos
mecej4
Honored Contributor III
630 Views

There is at least one other non-standard convention for specifying byte values for CHARACTER*1 variables:

C
      CHARACTER*1 CCR, CLF
      DATA CCR/'0D'X/, CLF/'0A'X/

Until I saw this in some old F77 code a few months ago (taken from an OS-2 code repository), I had never seen it.

0 Kudos
Reply