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

查看:118
本文介绍了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.5em>嵌套执行循环.

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天全站免登陆