문제

How to write (0>x<1) i.e, x must lies between 0 and 1 in FORTRAN. (0 .GE. x .LE. 1) or (x .GE. 0 And x .LE. 1) is it right? Thank you

도움이 되었습니까?

해결책 2

You need to use .and. if you want to make more than one comparison.

Also, .GE. and .LE. are going to give you greater than/equal to and less than/equal to. You want to use .gt. (greater than) and .lt. (less than):

program cmp
  real :: x(1:4) = (/0.25,0.5,0.75,1.0/)
  do i=1,4
     if (x(i) .gt. 0 .and. x(i) .lt. 1) then
        print *, x(i), "in range"
     else
        print *, x(i), "out of range"
     endif
  enddo
end program cmp

output:

  0.250000000     in range
  0.500000000     in range
  0.750000000     in range
   1.00000000     out of range

다른 팁

You could write this:

x .GT. 0 .AND. x .LT. 1

Personally, these days I'd use the modern forms of the relational operators, and I always like my less-than/greater-than expressions to have the chevrons opening to the right, so I'd write your expression as

0 < x .AND. x < 1

x .GT. 0 AND x .LT. 1

I hope I helped you out :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top