2011-12-27 57 views
0

我想要square.using pow()。所以,POW(3,2)應9.But它顯示8在我的iOS計算器application.here是我的代碼:Objective-C數學函數

else if([opertaion isEqualToString:@"sqrt"]) 
    { 

     result=pow([self popOperand],[self popOperand]); 
    } 

方法

-(double)popOperand 
{ 
    NSNumber *operandObject = [self.operandStack lastObject]; 
    if(operandObject)[self.operandStack removeLastObject]; 
    return [operandObject doubleValue]; 
} 

有一兩件事,我新手在iOS編程。

在此先感謝。

+0

我懷疑你沒有按照你認爲的順序從你的棧中彈出這些值。 – Abizern 2011-12-27 15:45:49

+0

我解決了我的問題。請看下面的評論。謝謝你:-) – Adnan 2011-12-27 15:51:01

回答

3

我會想象你只是想着你的操作數倒退。 2^3 == 8.

+0

是的,我得到了它。我按了3^2.這就是爲什麼我認爲它應該是9.謝謝你:-) – Adnan 2011-12-27 15:49:51

+0

@YasirAdnan如果這回答你的問題,你真的應該通過點擊聲譽微調下的勾號來接受它。 – Abizern 2011-12-27 15:52:05

0
result=pow([self popOperand],[self popOperand]); 
This statement is being executed from left to right ... so its calculating pow(2,3) 

You can use 
double x = [self operand]; 
double y = [self operand]; 
result = pow(x,y) 
+0

這也是從左到右執行的 – Adnan 2011-12-27 16:02:42

0

看起來好像你試圖彈出第一個數字,然後是最後一個。所以,getPower(3,2)會變成2^3。我會看看你的堆棧,並確保你正確地安排它們。