xcode 为什么这个 sin 方法返回错误的答案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2018081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why is this sin method returning a wrong answer?
提问by nickthedude
Hey, working on some categories and I've bumped up against a weird issue, im basically expanding on a calculator class to add some trig methods, and i am getting an incorrect value when i call the sin method in the return in the form of a double. i send a value of 100.7 to the method and it returns 0.168231, from what i can see the correct value should be = 0.939693 or there abouts.
嘿,在处理某些类别时,我遇到了一个奇怪的问题,我基本上扩展了一个计算器类以添加一些触发方法,当我以以下形式调用返回中的 sin 方法时,我得到了一个不正确的值一个双。我向该方法发送了一个 100.7 的值,它返回 0.168231,从我可以看到的正确值应该是 = 0.939693 或大约。
heres the code, I'm also attaching a link to the full project here:
这是代码,我还在此处附加了完整项目的链接:
(thanks)
(谢谢)
http://files.me.com/knyck2/svpfd4
http://files.me.com/knyck2/svpfd4
//
// Calculator_trig.m
// 11.4_calculator_trig
//
// Created by Nicholas Iannone on 1/6/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "Calculator_trig.h"
#import <math.h>
@implementation Calculator (Trigonometry)
-(double) sin
{
double result;
result = (double) sin (accumulator);
return result;
}
-(double) cos
{
double result;
result = cos ( accumulator);
return result;
}
-(double) tan
{
double result;
result = tan ( accumulator);
return result;
}
@end
#import "Calculator.h"
@implementation Calculator
-(void) setAccumulator: (double) value
{
accumulator = value;
}
-(void) clear
{
accumulator = 0;
}
-(double) accumulator
{
return accumulator;
}
-(double) memoryClear
{
memory = 0;
NSLog(@"memory has been cleared");
return accumulator;
}
-(double) memoryStore
{
memory = accumulator;
NSLog(@"memory has been set to %g", memory);
return accumulator;
}
-(double) memoryRecall
{
accumulator = memory;
NSLog(@"accumulator has been set to %g", accumulator);
return accumulator;
}
-(double) memoryAdd
{
memory += accumulator;
NSLog(@"accumulator: %g has been added to memory, memory is now %g", accumulator, memory);
return accumulator;
}
-(double) memorySubtract
{
memory -= accumulator;
NSLog(@"accumulator: %g has been subtracted from memory, memory is now %g", accumulator, memory);
return accumulator;
}
-(double) add: (double) value
{
accumulator += value;
return accumulator;
}
-(double) subtract: (double) value
{
accumulator -= value;
return accumulator;
}
-(double) multiply: (double) value
{
accumulator *= value;
return accumulator;
}
-(double) divide: (double) value
{
accumulator /= value;
return accumulator;
}
-(double) changeSign
{
accumulator = -accumulator;
return accumulator;
}
-(double) reciprocal
{
accumulator = 1 / accumulator;
return accumulator;
}
-(double) xSquared
{
accumulator *= accumulator;
return accumulator;
}
@end
#import <Foundation/Foundation.h>
#import "Calculator.h"
#import "Calculator_trig.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Calculator *myCalc = [[Calculator alloc] init];
double a = 0;
[myCalc setAccumulator: 100.70];
a = [myCalc sin];
NSLog(@" sin of accumulator = %f", a);
[myCalc release];
[pool drain];
return 0;
}
回答by Hamish Grubijan
You are computing the sin of 100.7 radians, and the answer given is the correct one.
您正在计算 100.7 弧度的 sin,给出的答案是正确的。
回答by John Feminella
It's expecting radians. To get the answer you want, convert degrees to radians first:
它在期待弧度。要获得您想要的答案,请先将度数转换为弧度:
// [radians] = [degrees] * [pi]/180
double theta = 100.7 * M_PI/180;
// sin(1.757 radians) == ~0.98
double result = sin(theta);
回答by Jimmy
it's expecting radians
期待弧度
回答by James Lawruk
According to google, the answer is correct. Notice google assumes radians.
根据谷歌,答案是正确的。注意谷歌假设弧度。
回答by user4951
The sin function is expecting radian. If you want to get degree you need to convert degree to radian.
sin 函数需要弧度。如果你想获得学位,你需要将度数转换为弧度。
How do you do so?
你怎么做?
Simple.
简单的。
In a circle there are 360 degrees. How much radian is there?
在一个圆圈中有 360 度。有多少弧度?
Radian is defined as the ratio between the length of the arc in front of the angle divided by the radius.
弧度定义为角度前面的弧长除以半径的比值。
So, for a full circle, the length of the arc is simply the circumference of the circle.
所以,对于一个完整的圆,弧的长度就是圆的周长。
What is the full circumference of the circle?
圆的完整周长是多少?
Well, π is defined to be the ratio between the circumference of the circle to the diameter.
嗯,π 被定义为圆的周长与直径的比值。
What is diameter?
什么是直径?
Well, diameter is 2 times the radius. Basically diameter is a line that go through the center of the circle and ended when the line meet the circle. Radius is a line that start at a center and end at the circle.
嗯,直径是半径的2倍。基本上直径是一条穿过圆心并在该线与圆相交时结束的线。半径是一条从中心开始到圆结束的线。
So
所以
Circle's circumference is π * diameter = π * 2 * radius = 2π radius. This is shortened to 2πr, where r is the radius.
圆的周长是 π * 直径 = π * 2 * 半径 = 2π 半径。这被缩短为 2πr,其中 r 是半径。
So, how many radians are there in a circle?
那么,一个圆有多少弧度?
Easy
简单
You divide the circle's circumference with the radius. Tada you got 2πr/r=2π.
您将圆的周长除以半径。Tada 你得到了 2πr/r=2π。
And that 2π is equivalent to 360 degree.
而那个 2π 相当于 360 度。
So if we know the degree, how do we know the radian?
那么如果我们知道度数,我们怎么知道弧度呢?
Simple, we multiply by 2π and we divide that by 360.
很简单,我们乘以 2π,然后除以 360。
So we multiply the whole thing by 2π/360=π/180.
所以我们把整个东西乘以 2π/360=π/180。
A way to see this is to imagine that radian and degree are "units". There are π radian for every 180 degrees. That means π radians/180 degrees is one because those are the ratio of the exact same number.
看到这一点的一种方法是想象弧度和度数是“单位”。每 180 度有 π 弧度。这意味着 π 弧度/180 度是 1,因为它们是完全相同数字的比率。
So if you have 107 degree that 107
所以如果你有 107 度,那么 107
IS 107 degrees * 1 = 107 degrees * π radians/180 degrees. Of course the computer don't care about the unit. At the end it becomes 107 * π/180.
IS 107 度 * 1 = 107 度 * π 弧度/180 度。当然,计算机并不关心单元。最后变成 107 * π/180。
In Objective-c M_PI is a constant that store the value of π.
在 Objective-c 中,M_PI 是一个常量,用于存储 π 的值。
What I would do is I would declare
我会做的是我会声明
#define radianperdegree (M_PI/180)
Now, the value is not really 1. However, conceptually, radianperdegree is indeed 1 if we take into account the unit. That's because 1 radian is way bigger than 1 degree.
现在,该值并不是真正的 1。但是,从概念上讲,如果我们考虑单位,radianperdegree 确实是 1。那是因为 1 弧度比 1 度大得多。
To get the angle in radian, I must not change the angle. What I do is I multiply that by a number that's concepsually 1. So I multiply by radianperdegree. The result is a much smaller number. But that much smaller number represent the exact same angle. That is because that much smaller number is the angle size in radian and each radian is bigger.
为了得到弧度的角度,我不能改变角度。我所做的是将其乘以一个概念上为 1 的数字。所以我乘以 radianperdegree。结果是一个小得多的数字。但是那个小得多的数字代表了完全相同的角度。那是因为小得多的数字是以弧度为单位的角度大小,并且每个弧度都更大。
Then I do
然后我做
double result = sin(100.7 * radianperdegree);
Tada.....
多田……
Clear?
清除?
Another thing you can do is to define
您可以做的另一件事是定义
#define RADTODEG(x) ((x) * 57.29578)
#define DEGTORAD(x) ((x) / 57.29578)
And then use sin(DEGTORAD(107))
然后使用 sin(DEGTORAD(107))