2011-10-24 61 views
1

*我正在嘗試使用FORTRAN代碼根據某些條件對數據組進行分組。代碼如下。Fortran中的邏輯表達式

gauche = 0.0 
trans = 0.0 
do i = 1, total_data 
!write(*,*) nombor(i), dihedral(i) 

if ( (0 > dihedral(i) < 120) .or. (-120 > dihedral(i) < 0) ) then 

    gauche = gauche + 1 
else 
    trans = trans + 1 
endif  
end do 

write(20,*) "Layer1_seg_total= ",(gauche+trans)," ","gauche_seg= ",gauche,"trans_seg= trans 

但是當我編譯我得到如下錯誤信息:

if ((0 > dihedral(i) < 120) .or. (-120 > dihedral(i) < 0)) then 
         1 
Error: Expected a right parenthesis in expression at (1) 
population.f90:42.5: 

else 
    1 
Error: Unexpected ELSE statement at (1) 
population.f90:44.4: 

endif 
    1 
Error: Expecting END DO statement at (1) 

我無法跟蹤誤差。任何人都可以提出錯誤?

:這不是一個分配

回答

4

的Fortran 90具有六個關係運算符:<,< =,>,> =,==,/ =
每個這六個關係運算符的需要兩個表達式,比較它們的值,並得出.TRUE。或.FALSE。
因此,a> b < c是錯誤的,因爲< b是LOGICAL且c是REAL。

重寫你的測試爲:

if ((0 > dihedral(i) .and. dihedral(i) < 120) .or. (-120 > dihedral(i) .and. dihedral(i) < 0)) then 
+2

是的,但在邏輯上表達式(0> x。和。x <120),另一個也沒有意義。我認爲它(0 Wildcat

+0

同意,但我的理解是,這個問題更多的是關於理解編譯器消息:) – cma

+0

非常感謝。有用。 – Vijay

0

這是什麼?

0 > dihedral(i) < 120 

如果是< X < B,則它應該這樣寫

a < x .and. x < b 

如果別的東西......

1

不能合併表達式是這樣的: A> B < c in Fortran 這樣寫: a> b .and。 b < c

+0

非常感謝。 – Vijay