2012-12-20 87 views
0

可能重複:
Why are these numbers not equal?R如果else語句,跳過如果,只是做別的

以下R代碼裏面是一個更大的功能的一部分,它使跳過if語句,並在最後執行else語句。有什麼建議麼?感謝

if(solv==0){ 
theta<-pi/2 
} else if(solv==1){ 
theta<-0 
} else if(solv==-1) { 
theta<-pi 
} else{ 
comb<-top/bottom 

theta<-acos(comb)} 
+0

太少的信息。在if語句前嘗試'print(solv)'? – liuminzhao

+0

什麼讓你知道它不工作?你能分享一些特定的信息嗎?我會把'dput(solve)'放在if語句之前 –

+2

請參閱http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal?lq=1。嘗試'if(isTRUE(all.equal(solve,0)))'等 – GSee

回答

0

重要的問題是,solve是一個整數?

如果不是這樣,那麼作爲@GSee在評論你的問題暗示,在代碼中的bug是有關floating point errors

試試下面的不是

# Set whichever tolerance is acceptable to you 
tol <- 1e-16 

if(isTRUE(all.equal(solv, 0, tolerance=tol))){ 
theta<-pi/2 
} else if(isTRUE(all.equal(solv, 1, tolerance=tol))){ 
theta<-0 
} else if(isTRUE(all.equal(solv, -1, tolerance=tol))) { 
theta<-pi 
} else{ 
comb<-top/bottom 

theta<-acos(comb)}