简体   繁体   English

Fortran DO循环,警告仅使用整数

[英]Fortran DO loop, warning to use integer only

I installed gfortran on my Ubuntu 15.04 system. 我在我的Ubuntu 15.04系统上安装了gfortran。 While compiling Fortran code, the DO loop asks to take integer parameters only and not real values or variables. 在编译Fortran代码时,DO循环仅要求获取整数参数,而不是实数值或变量。 That includes the loop variable and the step expression. 这包括循环变量和步骤表达式。 Why can't it take real values too? 为什么不能采取真正的价值呢?

The following is a program taken from here , exercise 3.5 of the section nested do loops . 以下是从这里开始的程序,练习3.5的嵌套do循环部分。

        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

The error shown after compiling is: 编译后显示的错误是:

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

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. Fortran标准现在要求do构造的循环控制由(标量)整数表达式给出,并且循环变量是(标量)整数变量。 The loop control consists of the start, step, and stop expressions (your step expression is 0.5 ). 循环控件由start,step和stop表达式组成(步骤表达式为0.5 )。 See R818 and R819 (8.1.6.2) of the Fortran 2008 document. 请参见Fortran 2008文档的R818和R819(8.1.6.2)。 That, then, is the short and simple answer: the standard says so. 那么,这是一个简短而简单的答案:标准是这样说的。

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. 在Fortran中使用其他形式进行循环控制直到Fortran 95.也就是说,从Fortran 95开始使用真实表达式是一个已删除的功能。

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. 但是与它们的便携性存在真正的困难。

Consider 考虑

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

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 )). 那将是(根据Fortran 90的规则) MAX(INT((m2 – m1 + m3) / m3), 0)其中( m1是起始值( 0. ), m2是停止值( 1. )和m3步长值( 0.1 ))。 Is that 10 or 11 (or even 9)? 是10或11(甚至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. 它完全取决于您的数字表示:我们记得0.1可能不能完全表示为实数, INT在转换为整数时会截断。 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

or 要么

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

Finally, you mention .f90 and .f95 file suffixes. 最后,你提到.f90.f95文件后缀。 gfortran doesn't take the first to mean that the source code follows the Fortran 90 standard (where the code would be fine). gfortran并不首先表示源代码遵循Fortran 90标准(代码可以正常)。 Further, the messages from the compiler are merely warnings, and these can be suppressed using the -std=legacy option. 此外,来自编译器的消息仅仅是警告,可以使用-std=legacy选项来抑制这些消息。 Conversely, using -std=f95 (or later standards) these become errors. 相反,使用-std=f95 (或更高版本的标准)这些会成为错误。


As a bonus fun fact consider the following piece of Fortran 90 code. 作为奖励有趣的事实,请考虑下面的Fortran 90代码。

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

While the loop named loop_real is valid, that named loop_integer isn't. 虽然名为loop_real的循环有效,但名为loop_integer的循环不是。 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 . INT(0.5)0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM