Obj-C 计算器 - 弧度换算为度数

发布于 2024-11-17 13:42:04 字数 1092 浏览 4 评论 0原文

我一直在观看斯坦福大学 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无言温柔 2024-11-24 13:42:04

您的意思可能是 operand = sin(operand * M_PI / 180); 吗?

Did you perhaps mean operand = sin(operand * M_PI / 180);?

无人问我粥可暖 2024-11-24 13:42:04

试试这个 - 它对我有用:

    if ([operation isEqual:@"sin"])
    {
        operand = (operand * M_PI/180); // first convert the operand from radians to degrees then perform the operation
        operand = sin(operand);
    }

希望这有帮助

Try this - It works for me:

    if ([operation isEqual:@"sin"])
    {
        operand = (operand * M_PI/180); // first convert the operand from radians to degrees then perform the operation
        operand = sin(operand);
    }

Hope this helps

秋千易 2024-11-24 13:42:04

你在哪里定义操作数?

您确定操作数是 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

爱人如己 2024-11-24 13:42:04
operand = sin(operand * (M_PI/180));
operand = sin(operand * (M_PI/180));
始于初秋 2024-11-24 13:42:04

你尝试过吗

sin( operand ) / 180.0 * M_PI

Did you try

sin( operand ) / 180.0 * M_PI
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文