0
以下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)}
以下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)}
重要的問題是,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)}
太少的信息。在if語句前嘗試'print(solv)'? – liuminzhao
什麼讓你知道它不工作?你能分享一些特定的信息嗎?我會把'dput(solve)'放在if語句之前 –
請參閱http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal?lq=1。嘗試'if(isTRUE(all.equal(solve,0)))'等 – GSee