隐式转换整数< - >逻辑在Fortran if语句中 [英] Implicit conversion integer <--> logical in Fortran if statement
问题描述
我有一些遗留的Fortran代码,我被要求分析并翻译成现代语言。我不知道过去编译代码时使用了哪种编译器,所以现在我试着用gfortran编译它。代码中包含这样的语句导致gfortran投诉:
pre code>程序测试
隐式无
整数* 4 :: var
var = .true。
if(var)then
write(*,*)Hi
endif
end program test
$ c
使用gfortran编译此错误会导致以下错误:
test.f:6:9:
if(var)then
1
错误:(1)处的IF子句需要标量LOGICAL表达式
(另外,它给出了在 var = .true中完成的转换的警告。
)。
我不确定编译代码的哪个编译器,但显然代码应该按原样编译。有没有办法告诉gfortran接受这种转换?
根据文档,虽然在if语句中没有进行隐式转换: https://gcc.gnu.org/onlinedocs/gfortran/Implicitly-convert -LOGICAL-and-INTEGER-values.html
这在GFortran中是不可能的。 手册指出:
但是,在
if语句中不存在INTEGER值的隐式转换,在I / O操作中也不存在LOGICAL或INTEGER值的隐式转换。
您只能在分配中执行隐式转换,例如您的
integer :: var
var = .true。
但即使如此,您也必须非常小心。它不符合标准,编译器之间的值 var
会有所不同。英特尔曾经使用 -1
(所有位设为1),除非选择 -standard-semantics
code> .true。,但gfortran在C语言中使用 +1
。新版本的英特尔Fortran更改了默认设置。另一个方向更加棘手,可能有值不是 .true。
也不是 .false。
。 p>
I have some legacy Fortran code which I was asked to analyze and translate to a modern language. I don't know which compiler was used in the past to compile the code, so for now, I'm trying to compile it with gfortran. The code contains a statement like this was causes gfortran to complain:
program test
implicit none
integer*4 :: var
var=.true.
if(var) then
write(*,*) "Hi"
endif
end program test
Compiling this with gfortran gives the following error:
test.f:6:9:
if(var) then
1
Error: IF clause at (1) requires a scalar LOGICAL expression
(In addition, it gives a warning about the conversion done in var=.true.
).
I'm not sure which which compiler the code was compiled, but apparently the code should compile as it is. Is there a way to tell gfortran to accept this conversion?
According to the docs, no implicit conversion is done within if-statements though: https://gcc.gnu.org/onlinedocs/gfortran/Implicitly-convert-LOGICAL-and-INTEGER-values.html
This is not possible in GFortran. The manual states:
However, there is no implicit conversion of INTEGER values in if-statements, nor of LOGICAL or INTEGER values in I/O operations.
You are only able to perform implicit conversions in assignments like your
integer :: var
var = .true.
but even there you must be very careful. It is not standard conforming and the value var
will differ between compilers. Intel used to use -1
(all bits set to 1), unless -standard-semantics
was chosen, for .true.
, but gfortran uses +1
as in the C language. New versions of Intel Fortran changes the default. The other direction is even trickier, there might be values which are neither .true.
nor .false.
.
这篇关于隐式转换整数< - >逻辑在Fortran if语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!