Obj-C计算器 - 弧度到度数 [英] Obj-C Calculator - Radians to Degrees
问题描述
我一直在观看斯坦福CS193P讲座,我一直在研究作业计算器。我很轻松地完成了第一项任务,现在我正在努力为任务做额外的工作。然而,我遇到了一个额外的信用问题:
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:
实现一个用户界面,用于选择sin()或cos()的操作数是否为
被认为是弧度或度数。当你在C库中调用sin(x)时,x被假定为
为弧度(即0到2π绕圆圈一次),但是用户可能想要
输入180并按下罪按钮并获得0而不是-0.8012(这是
180弧度的正弦值)。您可以使用UIButton并在每次按下UIButton时切换titleLabel的
文本,但更好的方法是查看是否可以通过阅读文档来了解如何使用UISwitch (如果你敢!)。
我实施了一个UISwitch并将其作为IBOutlet连接起来。当我执行'操作'时,检查开关是打开还是关闭,并将其传递给我的模型以及要执行的操作。在我的罪和案件中,我做以下事情:
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
如果弧度
(这是一个BOOL:YES =弧度,没有=度),那么我通常执行操作;如果用户将开关置于'度',我想以度为单位返回值,因为赋值声明:用户可能想要输入180并按下sin按钮并获得0而不是-0.8012(这是180弧度的正弦值。
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).
但是,此代码不能完全正常工作。当我将开关置于度并执行 sin 180
时,它返回的值大于或等于 -45
。那里出了点问题;而且我不确定是什么。我已经查找了如何进行转换,我认为我做得对,但显然没有。
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.
我意识到这可能更适合math.stackexchange,但由于我想发布一些代码,我把它放在这里。有人可以就实施此方法的最佳方式提供一些建议吗?已经有一段时间了,因为我甚至使用过cos或sin,弧度和度数。
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.
谢谢!
推荐答案
你在哪里定义操作数?
你确定操作数
是一个CGFloat而不是内存地址?
Are you sure that operand
is a CGFloat and not a memory address?
如何格式化输出?如果您使用%e,它将以科学记数法输出...尝试%f
How are you formatting your output? If you are using %e it will output in scientific notation...try doing %f
这篇关于Obj-C计算器 - 弧度到度数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!