Fortran DO 循环,警告仅使用整数 [英] Fortran DO loop, warning to use integer only

查看:33
本文介绍了Fortran DO 循环,警告仅使用整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Ubuntu 15.04 系统上安装了 gfortran.在编译 Fortran 代码时,DO 循环要求只接受整数参数,而不是实数值或变量.这包括循环变量和步骤表达式.为什么它也不能取真实值?

I installed gfortran on my Ubuntu 15.04 system. While compiling Fortran code, the DO loop asks to take integer parameters only and not real values or variables. That includes the loop variable and the step expression. Why can't it take real values too?

下面的程序摘自这里,小节的练习3.5 嵌套的do循环.

The following is a program taken from here, exercise 3.5 of the section nested do loops.

        program  xytab
        implicit none
        !constructs a table of z=x/y for values of x from 1 to 2 and 
        !y from 1 to 4 in  steps of .5
        real         ::   x, y, z 
        print *, '           x           y           z'
        do  x = 1,2
            do y = 1,4,0.5
                z = x/y
                print *, x,y,z
            end do
        end  do
        end  program xytab

编译后显示的错误是:

xytab.f95:8.4:

 do y = 1,4,0.5
    1
Warning: Deleted feature: Loop variable at (1) must be integer
xytab.f95:8.12:

 do y = 1,4,0.5
            1
Warning: Deleted feature: Step expression in DO loop at (1) must be integer
xytab.f95:7.3:

do x = 1,2
   1
Warning: Deleted feature: Loop variable at (1) must be integer

推荐答案

Fortran 标准现在要求 do 构造的循环控制由(标量)整数表达式给出,并且循环变量是(标量)整数变量.循环控制由开始、步进和停止表达式组成(您的步进表达式是 0.5).请参阅 Fortran 2008 文档的 R818 和 R819 (8.1.6.2).那么,这就是简短而简单的答案:标准是这样说的.

The Fortran standard now requires that a do construct's loop control is given by (scalar) integer expressions and that the loop variable is a (scalar) integer variable. The loop control consists of the start, step, and stop expressions (your step expression is 0.5). See R818 and R819 (8.1.6.2) of the Fortran 2008 document. That, then, is the short and simple answer: the standard says so.

正如编译器的消息所暗示的那样,它比这要复杂一些.在 Fortran 95 之前,使用其他形式的循环控制在 Fortran 中一直存在.也就是说,从 Fortran 95 开始,使用实数表达式是一项已删除的功能.

It's a little more complicated than that, as the messages from the compiler suggest. Using other forms for loop control was present in Fortran up until Fortran 95. That is, from Fortran 95 onward using real expressions is a deleted feature.

使用实数表达式有什么危害?使用得当,可想而知,没有坏处.但是它们的便携性确实存在困难.

What harm is there in using real expressions? Used correctly, one could imagine, there is no harm. But there's real difficulty in portability with them.

考虑

do x=0., 1., 0.1
 ...
end do

多少次迭代?那将是(根据 Fortran 90 的规则) MAX(INT((m2 – m1 + m3)/m3), 0) 其中 (m1 是起始值 (0.),m2 停止值 (1.) 和 m3 步长值 (0.1)).那是 10 还是 11(甚至是 9)?这完全取决于您的数字表示:我们记得 0.1 可能无法完全表示为实数,并且 INT 在转换为整数时会截断.您还必须担心重复添加实数.

How many iterations? That would be (under the rules of Fortran 90) MAX(INT((m2 – m1 + m3) / m3), 0) where (m1 is the start value (0.), m2 the stop value (1.) and m3 the step value (0.1)). Is that 10 or 11 (or even 9)? It depends entirely on your numeric representation: we recall that 0.1 may not be exactly representable as a real number and INT truncates in converting to integer. You'd also have to worry about repeated addition of real numbers.

所以,使用整数并在循环内做一些算术

So, use integers and do some arithmetic inside the loop

do y_loop = 0, 6
  y = 1 + y_loop/2.
  ...
end do

y = 1
do
  if (y>4) exit
  ...
  y = y+0.5
end do

最后,您提到了 .f90.f95 文件后缀.gfortran 并不首先表示源代码遵循 Fortran 90 标准(代码会很好).此外,来自编译器的消息只是警告,可以使用 -std=legacy 选项来抑制这些消息.相反,使用 -std=f95(或更高版本的标准)这些会成为错误.

Finally, you mention .f90 and .f95 file suffixes. gfortran doesn't take the first to mean that the source code follows the Fortran 90 standard (where the code would be fine). Further, the messages from the compiler are merely warnings, and these can be suppressed using the -std=legacy option. Conversely, using -std=f95 (or later standards) these become errors.

还有一个有趣的事实,请考虑以下 Fortran 90 代码.

As a bonus fun fact consider the following piece of Fortran 90 code.

real y
integer i

loop_real: do y=1, 4, 0.5
end do loop_real

loop_integer: do i=1, 4, 0.5
end do loop_integer

虽然名为 loop_real 的循环有效,但名为 loop_integer 的循环无效.在迭代计数的计算中,三个表达式被转换为循环变量的种类,带有种类参数.INT(0.5)0.

While the loop named loop_real is valid, that named loop_integer isn't. In the calculation of the iteration count the three expressions are converted to the kind, with kind parameters, of the loop variable. INT(0.5) is 0.

这篇关于Fortran DO 循环,警告仅使用整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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