2011-12-27 73 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。我会看看你的堆栈,并确保你正确地安排它们。