Fortran 中的长整型
我正在尝试处理大数字(~10^14),并且我需要能够存储它们并迭代该长度的循环,即
n=SOME_BIG_NUMBER
do i=n,1,-1
我尝试了通常的星号表示法,kind=8< /code> 等,但似乎没有任何作用。 然后我检查了巨大的内部函数,代码:
program inttest
print *,huge(1)
print *,huge(2)
print *,huge(4)
print *,huge(8)
print *,huge(16)
print *,huge(32)
end program inttest
在所有情况下都会产生数字 2147483647。这是为什么呢?我在 64 位机器上使用 gfortran (f95)。
如果我需要一个 bignum 库,人们会推荐哪一个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 Mac 上使用的 gfortran 版本是 4.3、4.4 和 4.5,支持 8 字节整数。在 Fortran >= 90 中选择变量类型的最佳方法是使用内部函数来指定所需的精度。尝试:
获取至少 18 位十进制数字,通常是 8 字节整数。
在gfortran 4.3中,巨大(1_LargeInt_K)输出9223372036854775807。当您编写巨大(1)等时,默认情况下常量是默认整数,这里显然是4个字节,因为巨大返回2147483647。所以有时您需要指定常量,而不仅仅是变量——更常见的是,当人们丢失实常量的有效数字时,这会让人感到困惑,实常量默认为单精度。
另请参阅 Fortran:整数*4 与整数(4) 与整数(kind=4)< /a>
通常 gfortran 的命令名称为 gfortran。 f95 可以是不同的编译器吗?尝试“gfortran -v”和“f95 -v”。
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:
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".
您误解了 HUGE 函数的精确定义。
HUGE(num)
返回与num
具有相同种类和类型的最大数字。返回的值也具有与num
相同的种类和类型。由于所有输入值都是(默认)整数HUGE
,因此正确地返回最大的默认大小整数。HUGE(num)
不会返回kind=num
的最大整数。HUGE(num)
也不会返回以num
字节表示的最大数字。虽然许多编译器使用integer(kind=4)
和integer(kind=8)
etc 来表示 4 字节和 8 字节整数,但这是不受语言标准的保证,并且不能依赖其可移植性。@MSB 的答案告诉你如何做你想做的事,我只是插话一些澄清。
You've misunderstood the precise definition of the
HUGE
function.HUGE(num)
returns the largest number with the same kind and type asnum
. The value returned also has the same kind and type asnum
. Since all your input values are (default) integersHUGE
, correctly, returns the largest default-size integer.HUGE(num)
does not return the largest integer withkind=num
. Nor doesHUGE(num)
return the largest number representable innum
bytes. While many compilers useinteger(kind=4)
andinteger(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.
摘要:考虑查看编译器选项。
自从我做FORTRAN以来已经很长时间了,我不记得使用过HUGE(),但我稍微看了一下这个。我的 Intel Linux 机器有 gfortran 4.1.2。我发现我必须在打开 -fdefault-integer-8 选项的情况下进行编译才能使其适用于 64 位整数。具体来说,使用以下代码:
运行
创建了一个可执行文件,打印:
但是,运行:
生成一个可执行文件,其输出如下:
另外,当我将变量声明为整数*8 并在不使用 -fdefault-integer-8 选项的情况下进行编译时,出现错误。代码:
运行
结果为
但是,当我使用 -fdefault-integer-8 选项编译时,一切正常,并且我得到了一个打印的可执行文件
也许还有其他有用的 gfortran 选项,但我没有进一步调查。
当然,这仍然无法得到 10^14,但它可能有助于解释您所看到的结果。
Summary: Consider looking at compiler options.
It's been a l-o-n-g time since I've done FORTRAN, and I don't remember using HUGE(), but I looked at this a little. My Intel Linux machine has gfortran 4.1.2. I found I had to compile with the -fdefault-integer-8 option turned on to make it work for 64 bit integers. Specifically, with this code:
running
created an executable which printed:
However, running:
resulted in an executable which gave the output:
Also, when I declared a variable as integer*8 and compiled without the -fdefault-integer-8 option, I got an error. The code:
running
resulted in
However, things worked fine when I compiled with the -fdefault-integer-8 option and I got an executable which printed
Maybe there are other gfortran options which would be useful, but I didn't investigate further.
Granted, this still doesn't get you 10^14, but it may help explain the results you saw.