永发信息网

fortran 循环控制变量为实型时编译老有Warning

答案:4  悬赏:0  手机版
解决时间 2021-03-07 15:57
  • 提问者网友:战皆罪
  • 2021-03-07 12:00
如题,比如说有段代码如下
program test
implicit none
real s,x
x=0.1
do s=0.1,0.9,0.1
x=s+1
write(*,*)x
end do
end
编译的话会出现如下编译提示
do s=0.1,0.9,0.1
1
Warning: Deleted feature: Loop variable at (1) must be integer
do.f95:5.6:

do s=0.1,0.9,0.1
1
Warning: Deleted feature: Start expression in DO loop at (1) must be integer
do.f95:5.10:

do s=0.1,0.9,0.1
1
Warning: Deleted feature: End expression in DO loop at (1) must be integer
do.f95:5.14:

do s=0.1,0.9,0.1
1
Warning: Deleted feature: Step expression in DO loop at (1) must be integer
但是会有可执行程序生成。
我想问下为什么会有警告提示,fortran不是支持非整型循环控制变量么?如果没有其他方法避免警告的话怎么加入编译选项使其不提示警告?
PS:本人使用的是编译器是GNU Fortran (Ubuntu 4.4.3-4ubuntu5) 4.4.3
最佳答案
  • 五星知识达人网友:琴狂剑也妄
  • 2021-03-07 12:36
我用的也是gfortran,也碰到这种问题,所以我现在已经用整型来循环了。不过你如果要忽略警告,可以这样子,在编译时加上-std=legacy:
gfortran testloop.f90 -o testloop -std=legacy
不过我的是windows下,不知道你的版本行不行?

下面是手册的原话,关于-std=std
Specify the standard to which the program is expected to conform, which maybe one of ‘f95’, ‘f2003’, ‘gnu’, or ‘legacy’. The default value for std is ‘gnu’,which specifies a superset of the Fortran 95 standard that includes all of the extensions supported by GNU Fortran, although warnings will be given for obsolete extensions not recommended for use in new code. The ‘legacy’ value is equivalent but without the warnings for obsolete extensions, and may be useful for old non-standard programs. The ‘f95’ and ‘f2003’ values specify strict conformance to the Fortran 95 and Fortran 2003 standards, respectively; errors are given for all extensions beyond the relevant language standard, and warnings are given for the Fortran 77 features that are permitted but obsolescent in later standards.
全部回答
  • 1楼网友:不如潦草
  • 2021-03-07 15:02
你好! 原则上来讲,循环变量只能是整型,不能是实行。 为什么呢?因为循环变量在步长和终止时需要进行相等判断(确定是否终止循环) 那么整型是不能做相等判断的,因为存在着计算机存储误差,我想你应该知道。 我的回答你还满意吗~~
  • 2楼网友:撞了怀
  • 2021-03-07 13:44
你可以考虑换个高版本的编译器,我这里Compaq Visual Fortran 6.5编译、运行无任何问题。 程序是直接复制你的。
  • 3楼网友:上分大魔王
  • 2021-03-07 12:44
你就用整形循环变量
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯