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
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)