Fortran: handling integer values of size: ~700000000000

后端 未结 6 546
忘掉有多难
忘掉有多难 2021-01-05 10:52

Currently I\'m brushing up on my Fortran95 knowledge (don\'t ask why)...

I\'m running in to a problem though. How does one handle large integers, eg. the size of: ~7

6条回答
  •  再見小時候
    2021-01-05 10:55

    The portable to declare an integer "index" that will have at least 12 decimal digits is:

    integer, parameter :: MyLongIntType = selected_int_kind (12)
    integer (kind=MyLongIntType) :: index
    

    The "kind=" may be omitted.

    Using specific values such as 3 is completely non-portable and not recommended. Some compilers use the type numbers consecutively, others use the number of bytes. The "selected_int_kind" will return the kind number of the smallest integer kind available to the compiler that can represent that requested number of digits. If no such type exists, -1 will be returned, and the value will fail when used kind value to declare an integer.

    Both gfortran and ifort return a kind for decimal digits input to selected_int_kind up up to 18. Large values such as 18 will typically select an 8-byte integer with a largest positive value of 9223372036854775807. This has 19 digits, but if a compiler supports this type but not a longer one, selected_int_kind (19) will be -1, because not all 19 digit integers are representable.

提交回复
热议问题