Fortran: handling integer values of size: ~700000000000

后端 未结 6 578
忘掉有多难
忘掉有多难 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:59

    The standard solution (since Fortran 95, so I assume your compiler supports it) is to use the SELECTED_INT_KIND intrinsic to probe for valid integer kinds (whose values are compiler dependent) and the HUGE intrinsic.

    • SELECTED_INT_KIND (R) returns the kind type parameter of an integer type that represents all integer values n with −10^R < n < 10^R (and returns -1 if no such type exist).
    • HUGE (K) returns the largest representable number in integer type of kind K.

    For example, on my Mac with an x86_64 processor (gfortran compiler, 64-bit mode), the following program:

      print *, selected_int_kind(1)
      print *, selected_int_kind(4)
      print *, selected_int_kind(8)
      print *, selected_int_kind(16)
      print *, selected_int_kind(32)
      print *, selected_int_kind(64)
      print *, huge(0_1)
      print *, huge(0_2)
      print *, huge(0_4)
      print *, huge(0_8)
      print *, huge(0_16)
      end
    

    outputs:

               1
               2
               4
               8
              16
              -1
      127
      32767
      2147483647
      9223372036854775807
     170141183460469231731687303715884105727
    

    which tells me that I'd use an integer(kind=8) for your job.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题