Long in Fortran

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

n=SOME_BIG_NUMBER
do i=n,1,-1

I tried the usual musical notation, kind=8etc., but nothing works. Then I checked the inner function huge, and the code:

program inttest

print *,huge(1)
print *,huge(2)
print *,huge(4)
print *,huge(8)
print *,huge(16)
print *,huge(32)

end program inttest

displays the number 2147483647 in all cases. Why is this? I am using gfortran (f95) on a 64 bit machine.

Should I need the bignum library that people advise about?

+3
source share
3 answers

gfortran, , 4.3, 4.4 4.5 Mac, 8- . Fortran >= 90 - , . :

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

18 , 8- .

gfortran 4.3, (1_LargeInt_K) 9223372036854775807. (1) .., , , , 4 , 2147483647. , - , , .

. Fortran: integer * 4 vs integer (4) vs integer (kind = 4)

gfortran gfortran. f95 ? "gfortran -v" "f95 -v".

+7

HUGE. HUGE(num) num. , num. ( ) HUGE, , .

HUGE(num) kind=num. HUGE(num) , num . integer(kind=4) integer(kind=8) .. - 8- , .

@MSB , , , .

+8

: .

l-o-n-g, FORTRAN, , HUGE(), . Intel Linux gfortran 4.1.2. , -fdefault-integer-8, 64- . , :

      program inttest
      print *, huge(1)
      end program inttest

$gfortran inttest.for

, :

2147483647

:

$gfortran -fdefault-integer-8 inttest.for

, :

9223372036854775807

, integer * 8 -fdefault-integer-8, . :

  program inttest2
  integer*8  test_int
  test_int = 9223372036854775807
  print *, test_int
  end program inttest2

$gfortran inttest2.for

inttest.for: 4

  test_int = 9223372036854775807  
                               1 

: (1)

However, it worked when I compiled with the -fdefault-integer-8 option and I got an executable that printed

9223372036854775807

There may be other gfortran options that would be helpful, but I have not explored yet.

Of course, this still does not give you 10 ^ 14, but it can help explain the results that you saw.

0
source

All Articles