Stanford iphone development β€” CS193P Task 1 - Operation Sin

one of the necessary work is to implement the "sin" button on the calculator. Add the following 4 control 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 am trying to enter sin (60) and the result = -0.304811

but in fact I use a calculator in Windows and the result is 0.8860254

I do not know what is wrong with my code

+4
source share
2 answers

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

 result = sin(M_PI*operd/180) 
+6
source

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

0
source

Source: https://habr.com/ru/post/1414114/


All Articles