Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 463
Raphael Roth Avatar asked Apr 10 '26 08:04

Raphael Roth


1 Answers

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

like image 77
Vladimir F Героям слава Avatar answered Apr 13 '26 08:04

Vladimir F Героям слава



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!