Note that there are some explanatory texts on larger screens.

plurals
  1. POObj-C Calculator - Radians to Degrees
    text
    copied!<p>I've been watching the Stanford CS193P lectures and I've been working on the assignment calculator. I got through the first assignment fairly easily, and I'm trying to do the extra credit for the assignment now. I'm stuck however at one of the extra credit questions:</p> <p><em>Implement a user-interface for choosing whether the operand to sin() or cos() is considered radians or degrees. When you call sin(x) in the C library, x is assumed to be in radians (i.e. 0 to 2π goes around the circle once), but users might want to enter 180 and press the sin button and get 0 instead of -0.8012 (which is the sine of 180 radians). You could use a UIButton for this and switch out the titleLabel’s text each time the UIButton is pressed, but a better way would be to see if you can figure out how to use a UISwitch by reading the documentation (if you dare!).</em></p> <p>I implemented a UISwitch and hooked it up as an IBOutlet. When I perform an 'operation', I check if the switch is on or off and pass this to my model along with the operation to perform. In my sin and cos cases, I do the following:</p> <pre><code>else if ([operation isEqual:@"sin"]) { if (radians) { operand = sin(operand); } else { operand = sin(operand) * (180 / M_PI); } } // similar for cos </code></pre> <p>If <code>radians</code> (which is a BOOL: YES = radians, no = degrees), then I perform the operation as usually; if the user puts the switch to 'degrees', I want to return the value in degrees, as the assignment states: <em>users might want to enter 180 and press the sin button and get 0 instead of -0.8012 (which is the sine of 180 radians).</em></p> <p>However, this code doesn't fully work. When I put the switch to degrees and do <code>sin 180</code> it returns a value of more or less <code>-45</code>. Something is wrong there; and I'm not really sure what. I have looked up how to do the conversion and I think I'm doing it right, but apparently not.</p> <p>I realise this is perhaps better suited for math.stackexchange, but since there was some code I wanted to post I put it here. Can someone provide some advice on the best way to implement this? It's been a while since I even worked with cos or sin, radians and degrees.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload