error in if-statement, requires scalar logical expression

前端 未结 2 2013
刺人心
刺人心 2020-12-11 14:29

Within a subroutine I try to create a statement, however it will only work if I enter a number directly, as soon as I replace the number with a variable, it will give the er

相关标签:
2条回答
  • 2020-12-11 15:04

    From your comment var%type3 is a real, dimension(1,1). This isn't scalar, and var%type3 < 0.5 will be an array of the same shape.

    As the error message states, the test condition for the if should be scalar logical. Depending on what you want to do your test condition can be one of a non-exhaustive list:

    • var%type3(1,1) < 0.5
    • ALL(var%type3 < 0.5)
    • ANY(var%type3 < 0.5)

    The first case seems natural as it is a scalar condition, but I leave the others as you may well be expanding to cases where it isn't a (1,1) array.

    0 讨论(0)
  • 2020-12-11 15:07

    You try to use a DIMENSION(1, 1) type as a REAL.

    You shoul add (1, 1) to access to yout REAL contained in the DIMENSION(1, 1)

    Use :

      IF ( var%type3(1, 1) < 0.5 ) THEN
         print *, 'IT WORKS'
      END IF
    

    Exemple to get this error :

    MODULE vardef
      TYPE vartype
         REAL :: type3(1, 1)
      END TYPE vartype
    END MODULE vardef
    
    PROGRAM test
      USE vardef
    
      TYPE(vartype) var
    
      var%type3(1, 1) = 0
    
      IF ( var%type3 < 0.5 ) THEN
         print *, 'IT WORKS'
      END IF
      RETURN
    
    END PROGRAM test
    
    0 讨论(0)
提交回复
热议问题