在 Fortran 90/95 中是否有检查 Infinite 和 NaN 的标准方法? [英] Is there a standard way to check for Infinite and NaN in Fortran 90/95?

查看:22
本文介绍了在 Fortran 90/95 中是否有检查 Infinite 和 NaN 的标准方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到一种符合标准的方法来检查 Fortran 90/95 中的 Infinite 和 NaN 值,但事实证明它比我想象的要难.

I've been trying to find a standards-compliant way to check for Infinite and NaN values in Fortran 90/95 but it proved harder than I thought.

  • 我尝试使用 IEEE 754 中描述的二进制表示手动创建 Inf 和 NaN 变量,但我发现没有这样的功能.
  • 我知道 Fortran 2003 中带有 ieee_is_nan()ieee_is_finite() 内部函数的内部 ieee_arithmetic 模块.但是,并非所有编译器都支持它(特别是 gfortran 版本 4.9).
  • I tried to manually create Inf and NaN variables using the binary representation described in IEEE 754, but I found no such functionality.
  • I am aware of the intrinsic ieee_arithmetic module in Fortran 2003 with the ieee_is_nan() and ieee_is_finite() intrinsic functions. However it's not supported by all the compilers (notably gfortran as of version 4.9).

pinf = 1./0nan = 0./0 之类的开头定义无穷大和 NaN 对我来说似乎很骇人听闻,恕我直言,可能会引发一些构建问题 -例如,如果某些编译器在编译时检查这一点,则必须提供一个特殊标志.

Defining infinity and NaN at the beginning like pinf = 1. / 0 and nan = 0. / 0 seems hackish to me and IMHO can raise some building problems - for example if some compilers check this in compile time one would have to provide a special flag.

有什么方法可以在标准 Fortran 90/95 中实现吗?

Is there a way I can implement in standard Fortran 90/95?

function isinf(x)
! Returns .true. if x is infinity, .false. otherwise
...
end function isinf

isnan()?

推荐答案

不使用 ieee_arithmatic 的简单方法是执行以下操作.

The simple way without using the ieee_arithmatic is to do the following.

Infinity:定义您的变量 infinity = HUGE(dbl_prec_var)(或者,如果有,也可以是四精度变量).然后你可以简单地通过 if(my_var > infinity) 来检查你的变量是否为无穷大.

Infinity: Define your variable infinity = HUGE(dbl_prec_var) (or, if you have it, a quad precision variable). Then you can simply check to see if your variable is infinity by if(my_var > infinity).

NAN:这更容易.根据定义,NAN 不等于任何东西,甚至它本身.只需将变量与自身进行比较:if(my_var/= my_var).

NAN: This is even easier. By definition, NAN is not equal to anything, even itself. Simply compare the variable to itself: if(my_var /= my_var).

这篇关于在 Fortran 90/95 中是否有检查 Infinite 和 NaN 的标准方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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