Long ints in Fortran

后端 未结 3 1045
孤城傲影
孤城傲影 2020-12-11 20:40

I\'m trying to work with large numbers (~10^14), and I need to be able to store them and iterate over loops of that length, i.e.

n=SOME_BIG_NUMBER
do i=n,1,-         


        
3条回答
  •  生来不讨喜
    2020-12-11 21:35

    The gfortran versions that I use, 4.3, 4.4 and 4.5 on a Mac, support 8-byte integers. The best way to select a variable type in Fortran >= 90 is to use an intrinsic function to specify the precision that you need. Try:

    integer, parameter :: LargeInt_K = selected_int_kind (18)
    integer (kind=LargeInt_K) :: i, n
    

    to obtain at least 18 decimal digits, which will typically be a 8-byte integer.

    With gfortran 4.3, huge (1_LargeInt_K) outputs 9223372036854775807. When you wrote huge (1), etc., by default the constant was a default integer, here evidently 4-bytes since huge returned 2147483647. So sometimes you need to specify the precision of constants, not just variables -- more commonly this trips people up when they lose significant figures on a real constant, which defaults to single precision.

    Also see Fortran: integer*4 vs integer(4) vs integer(kind=4)

    Usually gfortran has the command name gfortran. Could f95 be a different compiler? Try "gfortran -v" and "f95 -v".

提交回复
热议问题