Obj-C 计算器 - 弧度换算为度数
我一直在观看斯坦福大学 CS193P 讲座,并且一直在研究作业计算器。我相当轻松地完成了第一个作业,现在我正在努力为该作业获得额外的学分。然而,我陷入了一个额外的学分问题:
实现一个用户界面来选择 sin() 或 cos() 的操作数是 考虑弧度或度数。当您在 C 库中调用 sin(x) 时,假定 x 以弧度为单位(即 0 到 2π 绕一圈),但用户可能想要 输入 180 并按 sin 按钮,得到 0 而不是 -0.8012 (这是 180 弧度)。您可以为此使用 UIButton 并切换 titleLabel 每次按下 UIButton 时都会显示文本,但更好的方法是看看是否可以 通过阅读文档了解如何使用 UISwitch(如果你敢的话!)。
我实现了一个 UISwitch 并将其连接为 IBOutlet。当我执行“操作”时,我会检查开关是打开还是关闭,并将其与要执行的操作一起传递给我的模型。在我的 sin 和 cos 情况下,我执行以下操作:
else if ([operation isEqual:@"sin"])
{
if (radians) {
operand = sin(operand);
}
else {
operand = sin(operand) * (180 / M_PI);
}
}
// similar for cos
如果 radians
(这是一个 BOOL:YES = 弧度,no = 度),那么我照常执行操作;如果用户将开关切换到“度”,我想返回以度为单位的值,如分配所述:用户可能想要输入 180 并按 sin 按钮并得到 0 而不是 -0.8012 (这是180 弧度的正弦)。
但是,此代码不能完全工作。当我将开关切换到度数并执行 sin 180
时,它返回的值或多或少为 -45
。那里出了点问题;我不太确定是什么。我已经查过如何进行转换,我认为我做得对,但显然不是。
我意识到这可能更适合 math.stackexchange,但由于我想发布一些代码,所以我将其放在这里。有人可以就实现这一点的最佳方法提供一些建议吗?我已经有一段时间没有使用 cos 或 sin、弧度和度数了。
谢谢!
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:
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!).
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:
else if ([operation isEqual:@"sin"])
{
if (radians) {
operand = sin(operand);
}
else {
operand = sin(operand) * (180 / M_PI);
}
}
// similar for cos
If radians
(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: 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).
However, this code doesn't fully work. When I put the switch to degrees and do sin 180
it returns a value of more or less -45
. 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.
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的意思可能是
operand = sin(operand * M_PI / 180);
吗?Did you perhaps mean
operand = sin(operand * M_PI / 180);
?试试这个 - 它对我有用:
希望这有帮助
Try this - It works for me:
Hope this helps
你在哪里定义操作数?
您确定操作数是 CGFloat 而不是内存地址吗?
您如何格式化输出?如果您使用 %e 它将以科学计数法输出...尝试执行 %f
Where did you define operand?
Are you sure that
operand
is a CGFloat and not a memory address?How are you formatting your output? If you are using %e it will output in scientific notation...try doing %f
你尝试过吗
Did you try