2

I want to see integer kind number of gfortran

so I write this line

write(*,"(1X,I20,'correspond to kind  ',I2)"),11111111111,kind(11111111111)

There will be compilation error says

precision of type test.f90:67:57: Error: Integer too big for its kind at (1). Th is check can be disabled with the option -fno-range-check

So I tried recompile with -fno-range-check. But it gives result

-1773790777correspond to kind 4

What is wrong? On the other hand, intel fortran gives no error and correct answer

user15964
  • 2,507
  • 2
  • 31
  • 57
  • 2
    It's a duplicate, yes. However, Vladimir F's answer is much better than the accepted answer to the other question. – janneb Jan 24 '16 at 11:48

2 Answers2

5

An integer literal without any kind designation is always of the default kind no matter what value you enter1. There is therefore no much sense to even inquire

kind(111111111111)

the kind of any such literal is always the default kind, provided the value is valid. So the same as kind(1).

All integer kinds have a limited range of values. The largest one you can get using

write(*,*) HUGE(1)

Here instead of 1 you can use any other constant or variable of the integer kind you examine. Most often, the default value of HUGE will be 2147483647 which corresponds to 32-bit integers.

To use larger integer literal constants, use larger non-default kinds.

It doesn't matter if you use the methods from Fortran 90:

integer, parameter :: lk = selected_int_kind(15)

write(*,*) 11111111111_lk

or from Fortran 2008

use iso_fortran_env

integer, parameter :: lk = int64

write(*,*) 11111111111_lk

Both will work. Of course kind(11111111111_lk) will return the value of lk.


1 That is in standard Fortran. Some compilers may promote a large value to a larger kind for you, but only as a non-standard extension. You may be unpleasantly surprised when moving to a compiler, which keeps the standard behaviour.

  • Thank you Vladimir F. Very sorry for late comment. But I think "An integer literal without any kind designation is always of the default kind" is compiler dependent. Because intel fortran automatically support any integer literal without kind marks – user15964 Feb 03 '16 at 02:25
  • I don't understand. Of course you can use integer literals without a kind designation, but they are of the default kind. The value of the default kind is processor dependent. – Vladimir F Героям слава Feb 03 '16 at 07:59
  • I mean for intel fortran kind(1) gives 1, kind(111) gives 2, kind(111111) gives 4 etc, But for gfortran kind(1),kind(111),kind(111111) all gives 4. I mean they are different, processing of integer literal is compiler dependent – user15964 Feb 03 '16 at 08:17
  • You results are wrong. Kind(1) returns 4 on intel fortran too. – Vladimir F Героям слава Feb 03 '16 at 08:48
  • Oh, my god. I am sorry, I look into the wrong place. But I tried again, though kind(1) gives 4, but kind( 11111111111) will gives 8. So intel fortran automatically switches to kind=8 – user15964 Feb 03 '16 at 09:39
  • Yes, this is possible, it is a non-standard extension similar to the behaviour in C. In standard Fortran every literal without kind designation is of the default kind even if it is too large. I will edit the answer. – Vladimir F Героям слава Feb 03 '16 at 09:49
2

There may be a better explanation, but this is how I understand it. The short answer is the compiler defaults to a 4 byte integer.

Fortran is statically typed and you have not declared a variable type. The compiler is forced to use the default integer kind, 4 bytes in this case. The kind function simply returns the 'kind' of integer used. The compiler is letting you know that you are trying to assign a value too large for a 4 byte integer. When you apply -fno-range-check the compiler ignores this fact and the value overflows, thus the negative value returned. You can specify that the default integer kind be 8 bytes, -fdefault-integer-8. See the gfortran docs

Example foo.f90:

program foo
    write(*,"(1X,I20,' correspond to kind  ',I2)"),111111111111,kind(111111111111)
    write(*,"(1X,I20,' correspond to kind  ',I2)"),11,kind(11)
end program foo

Compiled with:

$ gfortran -fdefault-integer-8 -o foo.exe foo.f90
$ foo

Results in:

111111111111 correspond to kind 8
11 correspond to kind 8

So you can see the compiler is indifferent to the actual value you are testing.

However, I don't think this gets at the root of what you are trying to do, which I assume is to discover the minimum size of integer necessary for a specific numeric value. I don't know of a way to do this off hand with fortran. See this here and here for solutions you might be able to port from C. The second approach looks promising. In a dynamically typed language like Python the type assignment is handled for you.

>>> type(111111111111)
<type 'long'>
>>> type(11111)
<type 'int'>
Community
  • 1
  • 1
tharen
  • 1,262
  • 10
  • 22
  • *which I assume is to discover the minimum size of integer necessary for a specific numeric value. I don't know of a way to do this off hand with fortran* If that is indeed OP's intention then the intrinsic `selected_int_kind` provides the way to do it. – High Performance Mark Jan 24 '16 at 19:18
  • Yes, I had overlooked that one. I think Vladimir's answer explains it well. – tharen Jan 24 '16 at 20:05
  • thank you, tharen. Your explanation is convincing. – user15964 Feb 03 '16 at 02:27