Fortran中的正/负无穷大常数 [英] Positive/Negative Infinity Constants in Fortran

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

问题描述

在Fortran 2008中,如何获得正负无穷大的常数(我想应该是parameter?)?我尝试了以下代码:

How could I get constants (or parameters, I suppose) that are negative and positive infinity in Fortran 2008? I tried the following code:

program inf
  use, intrinsic :: ieee_arithmetic

  real(8), parameter :: inf_pos = ieee_value(0d0, ieee_positive_inf)
  real(8), parameter :: inf_neg = ieee_value(0d0, ieee_negative_inf)
end program inf

但是,出现以下错误:

$ gfortran inf.f08
inf.f08:4:22:

   real(8) :: inf_pos = ieee_value(0d0, ieee_positive_inf)
                  1
Error: Function ‘ieee_value’ in initialization expression at (1) must be an intrinsic function
inf.f08:5:22:

   real(8) :: inf_neg = ieee_value(0d0, ieee_negative_inf)
                  1
Error: Function ‘ieee_value’ in initialization expression at (1) must be an intrinsic function

尽管文档另有说明,但gfortran似乎认为ieee_value()不是内在的.

Despite documentation saying otherwise, it seems that gfortran thinks that ieee_value() isn't intrinsic.

反正有得到我想要做的事情吗?

Is there anyway to get what I'm trying to do?

推荐答案

我将首先看看为什么不能使用ieee_value来提供所需的命名常量的值,然后再给出一个坏消息.两者都对我来说很有趣.

I'll first look at why you can't use ieee_value to give the value for your desired named constant, then I'll give bad news. Both are interesting (to me).

ieee_value 不是内在过程.这是一个固有模块中的过程,但正如Fortran 2008标准注释(注13.25)所示:

ieee_value isn't an intrinsic procedure. It's a procedure in an intrinsic module but as the Fortran 2008 standard notes (Note 13.25):

标准内部模块中定义的类型和过程本身不是内部的.

The types and procedures defined in standard intrinsic modules are not themselves intrinsic.

gfortran正确地指出ieee_value可能不能用于初始化(恒定)表达式中.

gfortran is correct to note that ieee_value may not be used in an initialization (constant) expression.

现在,如果您需要使用无限"值初始化命名常量,则有不可移植的选项: 1 .

Now, if you need to initialize a named constant with an "infinite" value there are non-portable options:1.

  • 类似于有关NaN的问题,您可以计算出所需的位模式并使用transfer进行初始化;
  • 您可能可以编写溢出"的初始化表达式.
  • similar to this question about NaNs you can work out the required bit pattern and initialize with transfer;
  • you may be able to write an "overflowing" initialization expression.

您可以使用构建系统和预处理器解决此问题的不可移植性.

You can work around the non-portable nature of this with your build system and pre-processor.

总而言之,您可能不需要有一个无限的命名常量. IEEE模块可以轻松地提供此值是否无限大"或将该值设置为无限大"的过程.您的编译器还可以将将变量初始化为无穷大"作为编译时选项.

That all said, you may not need to have an infinite named constant. The IEEE modules easily provide procedures for "is this value infinite?", or "set this value to be infinite". Your compiler may also have "initialize variable to infinity" as a compile-time option.

1 外部初始化表达式对使用ieee_value的限制要宽松得多.

1 Outside initialization expressions the restrictions on using ieee_value are much looser.

这篇关于Fortran中的正/负无穷大常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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