Fortran 90 种参数 [英] Fortran 90 kind parameter

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

问题描述

我无法理解 Fortran 90 的 kind 参数.据我所知,它不能确定变量的精度(即浮点数或双精度),也不能确定变量的类型.

I am having trouble understanding Fortran 90's kind parameter. As far as I can tell, it does not determine the precision (i.e., float or double) of a variable, nor does it determine the type of a variable.

那么,它决定什么,它到底是干什么用的?

So, what does it determine and what exactly is it for?

推荐答案

变量的种类是一个整数标签,它告诉编译器它应该使用它支持的种类.

The KIND of a variable is an integer label which tells the compiler which of its supported kinds it should use.

请注意,虽然常见 KIND 参数与存储在该 KIND 变量中的字节数相同,但 不需要Fortran 标准.

Beware that although it is common for the KIND parameter to be the same as the number of bytes stored in a variable of that KIND, it is not required by the Fortran standard.

也就是说,在很多系统上,

That is, on a lot of systems,

REAl(KIND=4) :: xs   ! 4 byte ieee float
REAl(KIND=8) :: xd   ! 8 byte ieee float
REAl(KIND=16) :: xq   ! 16 byte ieee float

但可能有编译器,例如:

but there may be compilers for example with:

REAL(KIND=1) :: XS   ! 4 BYTE FLOAT
REAL(KIND=2) :: XD   ! 8 BYTE FLOAT
REAL(KIND=3) :: XQ   ! 16 BYTE FLOAT

对于整数和逻辑类型也是如此.

Similarly for integer and logical types.

(如果我去挖掘,我可能会找到示例.在 usenet 组 comp.lang.fortran 中搜索 kind 以找到示例.关于 Fortran 最有见地的讨论发生在那里,其中一些经验丰富贡献的人.)

(If I went digging, I could probably find examples. Search the usenet group comp.lang.fortran for kind to find examples. The most informed discussion of Fortran occurs there, with some highly experienced people contributing.)

所以,如果您不能指望某个特定类型的值在不同平台上为您提供相同的数据表示,您会怎么做?这就是内在函数 SELECTED_REAL_KINDSELECTED_INT_KIND 的用途.基本上,你告诉函数你需要能够表示什么样的数字,它会返回你需要使用的那种.

So, if you can't count on a particular kind value giving you the same data representation on different platforms, what do you do? That's what the intrinsic functions SELECTED_REAL_KIND and SELECTED_INT_KIND are for. Basically, you tell the function what sort of numbers you need to be able to represent, and it will return the kind you need to use.

我通常使用这些类型,因为它们通常给我 4 字节和 8 字节实数:

I usually use these kinds, as they usually give me 4 byte and 8 byte reals:

!--! specific precisions, usually same as real and double precision
integer, parameter :: r6 = selected_real_kind(6) 
integer, parameter :: r15 = selected_real_kind(15) 

所以我可能随后将一个变量声明为:

So I might subsequently declare a variable as:

real(kind=r15) :: xd

请注意,这可能会导致您使用混合语言程序时出现问题,并且您需要绝对指定变量占用的字节数.如果您需要确定,有查询内在函数会告诉您每种类型,您可以从中推断出变量的内存占用、其精度、指数范围等.或者,您可以恢复为非标准但常见的 real*4real*8 等声明样式.

Note that this may cause problems where you use mixed language programs, and you need to absolutely specify the number of bytes that variables occupy. If you need to make sure, there are enquiry intrinsics that will tell you about each kind, from which you can deduce the memory footprint of a variable, its precision, exponent range and so on. Or, you can revert to the non-standard but commonplace real*4, real*8 etc declaration style.

当您开始使用新编译器时,有必要查看编译器特定的种类值,以便了解您正在处理的内容.在网上搜索 kindfinder.f90 找到一个方便的程序,它会告诉你编译器可用的种类.

When you start with a new compiler, it's worth looking at the compiler specific kind values so you know what you're dealing with. Search the net for kindfinder.f90 for a handy program that will tell you about the kinds available for a compiler.

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

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