• declaring short integer arrays

    From Pedro Valdez@21:1/5 to All on Wed Oct 13 04:30:13 2021
    I need to use a large array, where short integers would suffice.
    I believe the correct form (in f90) would be something like this:
    integer(kind=2) :: short(500,300)
    But I can't recall seeing this ever used in code. Will this work across gfortran,
    Intel, PGI whatever?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gary Scott@21:1/5 to Pedro Valdez on Wed Oct 13 09:17:25 2021
    On 10/13/2021 6:30 AM, Pedro Valdez wrote:
    I need to use a large array, where short integers would suffice.
    I believe the correct form (in f90) would be something like this:
    integer(kind=2) :: short(500,300)
    But I can't recall seeing this ever used in code. Will this work across gfortran,
    Intel, PGI whatever?

    The kind value of 2 may (or may not) be consistent across compilers. If
    you use ISO_Fortran_env in your procedure, then you can replace the "2"
    with "int16", if all of the target compilers are new enough to support ISO_Fortran_env.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Koenig@21:1/5 to Pedro Valdez on Wed Oct 13 15:52:45 2021
    Pedro Valdez <pedro1492@lycos.com> schrieb:
    I need to use a large array, where short integers would suffice.
    I believe the correct form (in f90) would be something like this:
    integer(kind=2) :: short(500,300)
    But I can't recall seeing this ever used in code. Will this work across gfortran,
    Intel, PGI whatever?

    Not guaranteed.

    You can use either (if your compiler supports C interup)

    use, intrinsic :: iso_c_binding, only: c_int16_t
    integer(kind=c_int16_t) :: short(500,300)

    or

    integer, parameter :: ip = selected_int_kind(4)
    integer(kind=ip) :: short(500,300)

    The second variant gives you four decimal digits, which fits a short.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robin Vowels@21:1/5 to Pedro Valdez on Wed Oct 13 14:13:08 2021
    On Wednesday, October 13, 2021 at 10:30:14 PM UTC+11, Pedro Valdez wrote:
    I need to use a large array, where short integers would suffice.
    I believe the correct form (in f90) would be something like this: integer(kind=2) :: short(500,300)
    But I can't recall seeing this ever used in code. Will this work across gfortran,
    Intel, PGI whatever?
    .
    You may request the smallest integer storage unit with something like
    integer, parameter :: small = selected_int_kind(2)
    integer (kind=small) :: k(100,100)
    .
    where the number "'2" is the number of decimal digits that you would like stored. On systems that support this, you will get a byte of storage.
    If you want larger integers in magnitude than 127, change the "2" to "4".
    On systems that support this, you will get a two-byte integer.
    If none of those is supported, you will get an ordinary 4-byte integer.
    This is portable across every system.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gah4@21:1/5 to Pedro Valdez on Wed Oct 13 14:48:48 2021
    On Wednesday, October 13, 2021 at 4:30:14 AM UTC-7, Pedro Valdez wrote:
    I need to use a large array, where short integers would suffice.
    I believe the correct form (in f90) would be something like this: integer(kind=2) :: short(500,300)
    But I can't recall seeing this ever used in code. Will this work across gfortran,
    Intel, PGI whatever?

    There are no guarantees in the standard about specific KIND values.

    It is not unusual for it to be the number of bytes, (except for COMPLEX,
    where it is, then, half the number of bytes). There are also some that
    number them sequentially.

    In the latter case, you would likely get a reasonable sized small integer.

    Mention of short might indicate that you are used to the short type
    in C or Java, in which case the C interoperability KIND values would
    be a good choice. (As shown by others.) Also, they have been around
    long enough to be supported by many compilers.

    The ISO_Fortran_end values are newer, and may have less support
    in older compilers.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robin Vowels@21:1/5 to All on Thu Oct 14 05:10:09 2021
    On Thursday, October 14, 2021 at 8:48:50 AM UTC+11, gah4 wrote:
    On Wednesday, October 13, 2021 at 4:30:14 AM UTC-7, Pedro Valdez wrote:
    I need to use a large array, where short integers would suffice.
    I believe the correct form (in f90) would be something like this: integer(kind=2) :: short(500,300)
    But I can't recall seeing this ever used in code. Will this work across gfortran,
    Intel, PGI whatever?
    There are no guarantees in the standard about specific KIND values.

    It is not unusual for it to be the number of bytes, (except for COMPLEX, where it is, then, half the number of bytes). There are also some that
    number them sequentially.

    In the latter case, you would likely get a reasonable sized small integer.

    Mention of short might indicate that you are used to the short type
    in C or Java, in which case the C interoperability KIND values would
    be a good choice.

    Only selected_int_kind is portable across all Fortran compilers
    from Fortran 90.

    (As shown by others.) Also, they have been around
    long enough to be supported by many compilers.

    Only selected_int_kind is portable across all Fortran compilers
    from and including Fortran 90.

    The ISO_Fortran_end values are newer, and may have less support
    in older compilers.

    Older compilers do not support it at all.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)