有没有一种标准的方法来检查Fortran 90/95中的Infinite和NaN? [英] Is there a standard way to check for Infinite and NaN in Fortran 90/95?
问题描述
我一直在努力寻找符合标准的方法来检查Fortran 90/95中的Infinite和NaN值,但事实证明比我想象的要难。
- 我尝试使用IEEE 754中描述的二进制表示手动创建Inf和NaN变量,但是我没有发现这样的功能。
- 我知道内在的
ieee_is_nan()
和ieee_is_finite()$ c $ Fortran 2003中的code> ieee_arithmetic
模块内部功能。然而,从4.9版本开始,所有的编译器都不支持它(特别是gfortran ) )。
在开始处定义无穷大和NaN,如
pinf = 1。/ 0
和nan = 0. / 0
对我来说似乎很骇人,恕我直言,可能会引发一些构建问题 - 例如,如果某些编译器在编译时检查这个问题,特殊标志。
有没有一种方法可以在标准Fortran 90/95中实现?
函数isinf(x)
!返回.true。如果x是无穷大,.false。否则
...
结束函数isinf
和
isnan()
?解决方案不使用
的简单方法ieee_arithmatic
:定义您的变量 infinity = HUGE( dbl_prec_var)
(或者,如果有,则为四元精度变量)。然后你可以通过if(my_var> infinity)
来简单地检查你的变量是否为无穷大。
<强>南:这更容易。根据定义,NAN不等于任何东西,甚至本身。简单地将变量与自身进行比较:
if(my_var / = my_var)
。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.
- 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 theieee_is_nan()
andieee_is_finite()
intrinsic functions. However it's not supported by all the compilers (notably gfortran as of version 4.9).
Defining infinity and NaN at the beginning like
pinf = 1. / 0
andnan = 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.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
and
isnan()
?解决方案The simple way without using the
ieee_arithmatic
is to do the following.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 byif(my_var > infinity)
.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屋!