Implicit conversion integer <--> logical in Fortran if statement

半世苍凉 提交于 2019-12-14 02:09:20

问题


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


回答1:


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..



来源:https://stackoverflow.com/questions/37293641/implicit-conversion-integer-logical-in-fortran-if-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!