Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

loops - Long ints in Fortran

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,-1

I've tried the usual star notation, kind=8 etc. but nothing seems to work. Then I checked the huge intrinsic function, 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

produces the number 2147483647 in all cases. Why is this? I'm using gfortran (f95) on a 64-bit machine.

If I'm going to need a bignum library, which one do people suggest?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You've misunderstood the precise definition of the HUGE function. HUGE(num) returns the largest number with the same kind and type as num. The value returned also has the same kind and type as num. Since all your input values are (default) integers HUGE, correctly, returns the largest default-size integer.

HUGE(num) does not return the largest integer with kind=num. Nor does HUGE(num) return the largest number representable in num bytes. While many compilers use integer(kind=4) and integer(kind=8) etc for 4- and 8-byte integers, this is not guaranteed by the language standard and cannot be relied upon to be portable.

@MSB's answer tells you how to do what you want, I'm just butting in with some clarification.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...