为什么这个 sin 方法返回错误的答案?
嘿,在处理某些类别时,我遇到了一个奇怪的问题,我基本上是在计算器类上扩展以添加一些三角方法,当我在返回中以以下形式调用 sin 方法时,我得到了错误的值一个双人。我向该方法发送一个值 100.7,它返回 0.168231,据我所知,正确的值应该是 = 0.939693 或类似的值。
这是代码,我还在这里附加了完整项目的链接:(
谢谢)
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;
}
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.
heres the code, I'm also attaching a link to the full project here:
(thanks)
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在计算 100.7 弧度的 sin,给出的答案是正确的。
You are computing the sin of 100.7 radians, and the answer given is the correct one.
它正在等待弧度。要获得所需的答案,请先将度数转换为弧度:
It's expecting radians. To get the answer you want, convert degrees to radians first:
它期待弧度
it's expecting radians
根据谷歌的说法,答案是正确的。请注意,谷歌假设弧度。
http://www.google.com/search?hl= en&q=sin+of+100.7
According to google, the answer is correct. Notice google assumes radians.
http://www.google.com/search?hl=en&q=sin+of+100.7
sin 函数需要弧度。如果你想获得度数,你需要将度数转换为弧度。
你是如何做到的?
简单的。
一个圆有 360 度。弧度是多少?
弧度定义为角度前面的弧长除以半径的比值。
因此,对于一个完整的圆,弧长就是圆的周长。
圆的周长是多少?
好吧,π 定义为圆的周长与直径之比。
什么是直径?
那么,直径是半径的 2 倍。基本上,直径是一条穿过圆心并在该线与圆相交时结束的线。半径是一条从中心开始到圆结束的线。
所以
圆的周长是 π * 直径 = π * 2 * 半径 = 2π 半径。这被缩短为 2πr,其中 r 是半径。
那么,一个圆有多少弧度呢?
简单
你用圆的周长除以半径。多田你有 2πr/r=2π。
2π 相当于 360 度。
那么如果我们知道度数,我们怎么知道弧度呢?
很简单,我们乘以 2π,然后除以 360。
所以我们将整个乘以 2π/360=π/180。
理解这一点的一种方法是想象弧度和度是“单位”。每180度就有π弧度。这意味着 π 弧度/180 度是 1,因为它们是完全相同的数字的比率。
因此,如果您有 107 度,则 107
IS 107 度 * 1 = 107 度 * π 弧度/180 度。当然,计算机不关心单位。最后变成 107 * π/180。
在 Objective-c 中,M_PI 是一个存储 π 值的常量。
我要做的是声明
现在,该值实际上并不是 1。但是,从概念上讲,如果我们考虑单位,则每度弧度确实为 1。这是因为 1 弧度远大于 1 度。
为了获得以弧度表示的角度,我不能改变角度。我所做的就是将其乘以一个概念上为 1 的数字。所以我乘以每度的弧度。结果是一个小得多的数字。但这个小得多的数字代表完全相同的角度。这是因为那个小得多的数字是以弧度为单位的角度大小,并且每个弧度都更大。
然后我做
多田……
清楚了吗?
您可以做的另一件事是定义
然后使用 sin(DEGTORAD(107))
The sin function is expecting radian. If you want to get degree you need to convert degree to radian.
How do you do so?
Simple.
In a circle there are 360 degrees. How much radian is there?
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.
So
Circle's circumference is π * diameter = π * 2 * radius = 2π radius. This is shortened to 2πr, where r is the radius.
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π.
And that 2π is equivalent to 360 degree.
So if we know the degree, how do we know the radian?
Simple, we multiply by 2π and we divide that by 360.
So we multiply the whole thing by 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.
So if you have 107 degree that 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.
In Objective-c M_PI is a constant that store the value of π.
What I would do is I would declare
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.
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.
Then I do
Tada.....
Clear?
Another thing you can do is to define
And then use sin(DEGTORAD(107))