4

one of the required work is to implement the "sin" button on the calculator Add the following 4 operation buttons: • sin : calculates the sine of the top operand on the stack.

here is my code

- (double)performOperation:(NSString *)operation
{
  double result = 0;

   if ([operation isEqualToString:@"+"]) {

    result = [self popOperand] + [self popOperand];

   }else if ([@"*" isEqualToString:operation]) {

    result = [self popOperand] * [self popOperand];

   }else if ([operation isEqualToString:@"-"]) {

    double subtrahend = [self popOperand];
    result = [self popOperand] - subtrahend;

   }else if ([operation isEqualToString:@"/"]) {

    double divisor = [self popOperand];
    if(divisor) result = [self popOperand] / divisor;

    }else if([operation isEqualToString:@"sin"]){

    double operd = [self popOperand];
    NSLog(@"operd=%g",operd);
    if(operd) result = sin(operd);

   }

[self pushOperand:result];

     return result;
}

I try to input sin(60) and the result=-0.304811

but actually I use calculator in windows and the result is 0.8860254

I don't know what's wrong with my code

2 Answers 2

6

Windows calculator interprets 60 as degrees; your calculator interprets 60 as radians. Both answers are correct. If you want the number interpreted as degrees instead, multiply by M_PI and divide by 180.

result = sin(M_PI*operd/180)
2
  • I shudder to think of a world in which using degrees can be considered "correct" =) May 24, 2012 at 11:01
  • 1
    @StephenCanon That's the world outside: ask anyone about the right angle, and count the number of people who say π/2 :) May 24, 2012 at 11:06
0

sin() takes radians, and Windows calculator takes degrees by default. So, the difference.

Not the answer you're looking for? Browse other questions tagged or ask your own question.