Fortran:处理大小的整数值:~700000000000
目前我正在温习我的 Fortran95 知识(不要问为什么)...
不过我遇到了一个问题。如何处理大整数,例如。大小:~700000000000
INTEGER(KIND=3) 无法容纳此数字。 如果有人感兴趣,我提供的编译器是 Silverfrost FTN95。
我正在使用整数来运行更大的数据集。
您有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
标准解决方案(从 Fortran 95 开始,所以我假设您的编译器支持它)是使用 < code>SELECTED_INT_KIND 内在探测有效整数类型(其值取决于编译器)和
巨大
内在。SELECTED_INT_KIND (R)
返回整数类型的 kind 类型参数,该整数类型表示所有具有 −10^R < 的整数值 n。 n < 10^R(如果不存在此类类型,则返回 -1)。HUGE (K)
返回 K 类型整数类型中可表示的最大数字。例如,在我的配备 x86_64 处理器的 Mac 上 (gfortran 编译器,64 位模式),以下程序:
输出:
它告诉我我将使用
integer(kind=8)
为了你的工作。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:
outputs:
which tells me that I'd use an
integer(kind=8)
for your job.声明至少有 12 位十进制数字的整数“索引”的可移植方法是:
“kind=”可以省略。
使用特定值(例如 3)是完全不可移植的,因此不建议使用。有些编译器使用连续的类型号,有些则使用字节数。 “selected_int_kind”将返回编译器可用的最小整数类型的类型号,该类型可以表示所请求的位数。如果不存在该类型,则返回-1,并且使用kind值声明整数时该值将失败。
gfortran 和 ifort 都返回输入到 selected_int_kind 的十进制数字的种类,最多为 18。较大的值(例如 18)通常会选择最大正值为 9223372036854775807 的 8 字节整数。这有 19 位数字,但如果编译器支持此值类型但不是更长的类型,selected_int_kind (19) 将为 -1,因为并非所有 19 位整数都可以表示。
The portable to declare an integer "index" that will have at least 12 decimal digits is:
The "kind=" may be omitted.
Using specific values such as 3 is completely non-portable and not recommended. Some compilers use the type numbers consecutively, others use the number of bytes. The "selected_int_kind" will return the kind number of the smallest integer kind available to the compiler that can represent that requested number of digits. If no such type exists, -1 will be returned, and the value will fail when used kind value to declare an integer.
Both gfortran and ifort return a kind for decimal digits input to selected_int_kind up up to 18. Large values such as 18 will typically select an 8-byte integer with a largest positive value of 9223372036854775807. This has 19 digits, but if a compiler supports this type but not a longer one, selected_int_kind (19) will be -1, because not all 19 digit integers are representable.
有许多免费的 Fortran 库可以解决这个问题。 。 FMLIB 就是其中之一。 从此页面链接还有五到六个替代方案。
There are a number of free arbitrary-precision libraries available for Fortran which would deal with this problem. FMLIB is one. Five or six more alternatives are linked from this page.
如果您将它用作循环控制变量,但不直接使用整数(我猜您不能这样做,因为您不能声明一个大于可表示的最大索引的数组,对吧?),那么我猜测要做的事情是将小狗除以 100000 之类的东西,并将其循环嵌套在另一个迭代那么多次的循环中。
If you are using it as a loop control variable, but aren't using the integer directly (which I guess you can't be, as you can't declare an array larger than the largest index representable, right?), then I guess the thing to do is divide that puppy by something like 100000 and nest its loop in another loop that iterates that many times.
你试过INTEGER(KIND=4)吗?
Have you tried INTEGER(KIND=4)?
我们对此的回答是将值放入双精度变量中,并对其执行 DINT 以消除任何小数部分。
结果是放置在双精度变量中的整数。
函数 DINT 并不总是适用于所有 FORTRAN。该函数是双精度整数函数,这将允许非常大的整数(最多 17 位)。
Our answer to that was to put the value in a double precision variable and do a DINT on it to get rid of any fractional parts.
The results are an integer placed in a double precision variable.
The function DINT is not always available to all FORTRANs. The function is a double precision integer function and this will allow for very large integers (up to 17 digits).