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

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

问题描述

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



所以,它是什么决定的?它到底是什么?



请注意,虽然它是常见的

/ em>中,KIND参数与存储在该KIND变量中的字节数相同,Fortran标准不需要



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

  REAl(KIND = 4):: xs! 4字节ieee浮动
REAl(KIND = 8):: xd! 8字节ieee浮动
REAl(KIND = 16):: xq! 16字节ieee float

但是可能有编译器,例如:

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

类似于整数和逻辑类型。

(如果我去挖掘,我可能会找到示例,搜索用于 kind 的usenet组comp.lang.fortran以查找示例。发生在那里,有一些经验丰富的人出力。)

因此,如果你不能指望某种特定的价值在不同的平台上给你相同的数据表示,你做?这就是内部函数 SELECTED_REAL_KIND SELECTED_INT_KIND 的用途。基本上,你告诉功能你需要能够代表什么类型的数字,它会返回你需要使用的类型。


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

pre > ! - !通常与真实和双精度相同
integer,parameter :: r6 = selected_real_kind(6)
integer,parameter :: r15 = selected_real_kind(15)



因此,我可能会随后声明一个变量:

  real(kind = r15):: xd 

请注意,语言程序,并且您需要绝对指定变量占用的字节数。如果你需要确定,有些查询内在函数会告诉你每种类型,从中可以推断出变量的内存占用量,精度,指数范围等等。或者,您可以恢复为非标准但常见的 real * 4 real * 8 等声明风格。 / p>

当您使用新的编译器开始时,应该查看编译器特定的类型值,以便了解您正在处理的内容。在一个方便的程序中搜索 kindfinder.f90 ,它会告诉你有关编译器可用的类型。


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.

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.

(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.)

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.

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

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.

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天全站免登陆