Fortran 中的长整数 [英] Long ints in Fortran

查看:41
本文介绍了Fortran 中的长整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理大数(~10^14),我需要能够存储它们并迭代该长度的循环,即

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

我尝试了常用的星号表示法,kind=8 等,但似乎没有任何效果.然后我检查了 huge 内部函数,代码:

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

在所有情况下都生成数字 2147483647.为什么是这样?我在 64 位机器上使用 gfortran (f95).

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

如果我需要一个 bignum 库,人们会推荐哪一个?

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

推荐答案

我在 Mac 上使用的 gfortran 版本,4.3、4.4 和 4.5,支持 8 字节整数.在 Fortran >= 90 中选择变量类型的最佳方法是使用内部函数来指定所需的精度.试试:

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

获取至少 18 位十进制数字,通常为 8 字节整数.

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

使用gfortran 4.3,huge(1_LargeInt_K)输出9223372036854775807.当你写huge(1)等时,默认常量是默认整数,这里显然是4字节,因为huge返回2147483647.所以有时你需要指定常量的精度,而不仅仅是变量——更常见的是,当人们在一个实数常量(默认为单精度)上丢失重要数字时,这会让人绊倒.

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.

另请参见 Fortran:整数*4 vs integer(4) vs integer(kind=4)

通常 gfortran 具有命令名称 gfortran.f95 可以是不同的编译器吗?试试gfortran -v"和f95 -v".

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

这篇关于Fortran 中的长整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆