Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
1 View

List-directed Output

Jump to solution

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

Accepted Solutions
Highlighted
Black Belt
1 View

I would expect these to be

Jump to solution

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).

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran

View solution in original post

0 Kudos
9 Replies
Highlighted
Black Belt
2 Views

I would expect these to be

Jump to solution

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).

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran

View solution in original post

0 Kudos
Highlighted
New Contributor I
1 View

I see, and I have found the

Jump to solution

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
Highlighted
Black Belt
1 View

The section you want is

Jump to solution

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

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
Valued Contributor II
1 View

! This is good.

Jump to solution
      ! 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
Highlighted
1 View

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
Highlighted
Black Belt
1 View

I believe this case, where

Jump to solution
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
Highlighted
New Contributor I
1 View

So for safely use, maybe I

Jump to solution

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
Highlighted
Valued Contributor II
1 View

Quote:Blane J. wrote:So for

Jump to solution

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
Highlighted
Black Belt
1 View

There is at least one other

Jump to solution

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