Fortran中的长整数 [英] Long ints in Fortran
问题描述
我正在尝试使用大数(〜10 ^ 14),我需要能够存储它们并遍历该长度的循环,即
n = SOME_BIG_NUMBER
do i = n,1,-1
我尝试了通常的星形符号, kind = 8
等,但似乎没有任何效果。
然后我检查了巨大的
内在函数和代码:
<$ c $ b $ print $,$($)
print *,huge(2)
print *,huge(2)
print *,huge (8)
print *,huge(16)
print *,huge(32)
end program inttest
在所有情况下产生数字2147483647。为什么是这样?我在64位机器上使用gfortran(f95)。
如果我需要一个bignum库,那么人们建议哪一个?
我在Mac上使用的gfortran版本,4.3,4.4和4.5支持8字节整数。在Fortran> = 90中选择变量类型的最佳方法是使用内部函数来指定所需的精度。尝试:
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ,n
以获得至少18位十进制数字,通常是一个8字节整数。 / b>
使用gfortran 4.3时,巨大的(1_LargeInt_K)输出9223372036854775807.当你写巨大的(1)等时,默认情况下常量是一个默认的整数,在这里显然是4字节因为巨大的返回2147483647.所以有时你需要指定常量的精度,而不仅仅是变量 - 更常见的是,当人们失去了一个实数常量的有效数字时,人们往往会失去意义,默认为单精度。
另请参阅 Fortran:整数* 4与整数(4) vs integer(kind = 4)
通常,gfortran的命令名是gfortran。可以f95是一个不同的编译器?尝试使用gfortran -v和f95 -v。
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?
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".
这篇关于Fortran中的长整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!