Fortran:处理大小的整数值:〜700000000000 [英] Fortran: handling integer values of size: ~700000000000
问题描述
目前我正在学习Fortran95的知识(不要问为什么)......
尽管我遇到了一个问题。如何处理大整数,例如。大小为:〜700000000000
INTEGER(KIND = 3)不能容纳这个数字。
如果任何人有兴趣,我可用的编译器是Silverfrost FTN95。
我使用整数来运行更大的一组数据。
您有什么建议吗?
标准解决方案因为Fortran 95,所以我认为你的编译器支持它)是使用 SELECTED_INT_KIND
内在探索有效的整数种类(其值依赖编译器)和 HUGE
内在。 -
SELECTED_INT_KIND(R)
返回一个整数类型的种类类型参数,它表示所有整数值n,其中-10 ^ R < n< 10 ^ R(如果不存在此类型,则返回-1)。
-
HUGE(K)
返回整数中最大的可表示数例如,在我的Mac上安装了x86_64处理器(print *,selected_int_kind(4)
print *,selected_int_kind(8)
print *,selected_int_kind(16)
print *,selected_int_kind(32)
print *,selected_int_kind(64)
print *,huge(0_1)
print *,huge(0_2)
print *,huge(0_4)
print *,huge(0_8)
print *,huge(0_16)
end
输出:
1
2
4
8
16
-1
127
32767
2147483647
9223372036854775807
170141183460469 231731687303715884105727
告诉我我会使用
整数(kind = 8 )
为您的工作。Currently I'm brushing up on my Fortran95 knowledge (don't ask why)...
I'm running in to a problem though. How does one handle large integers, eg. the size of: ~700000000000
INTEGER(KIND=3) cannot hold this number. If anyone is interested the compiler I have available is Silverfrost FTN95.
I am using the integer to run through a larger set of data.
Do you have any suggestions?
解决方案The standard solution (since Fortran 95, so I assume your compiler supports it) is to use the
SELECTED_INT_KIND
intrinsic to probe for valid integer kinds (whose values are compiler dependent) and theHUGE
intrinsic.SELECTED_INT_KIND (R)
returns the kind type parameter of an integer type that represents all integer values n with −10^R < n < 10^R (and returns -1 if no such type exist).HUGE (K)
returns the largest representable number in integer type of kind K.
For example, on my Mac with an x86_64 processor (gfortran compiler, 64-bit mode), the following program:
print *, selected_int_kind(1) print *, selected_int_kind(4) print *, selected_int_kind(8) print *, selected_int_kind(16) print *, selected_int_kind(32) print *, selected_int_kind(64) print *, huge(0_1) print *, huge(0_2) print *, huge(0_4) print *, huge(0_8) print *, huge(0_16) end
outputs:
1 2 4 8 16 -1 127 32767 2147483647 9223372036854775807 170141183460469231731687303715884105727
which tells me that I'd use an
integer(kind=8)
for your job.这篇关于Fortran:处理大小的整数值:〜700000000000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!