问题
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;
}
回答1:
You are computing the sin of 100.7 radians, and the answer given is the correct one.
回答2:
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);
回答3:
it's expecting radians
回答4:
According to google, the answer is correct. Notice google assumes radians.
http://www.google.com/search?hl=en&q=sin+of+100.7
回答5:
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
#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.
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
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))
来源:https://stackoverflow.com/questions/2018081/why-is-this-sin-method-returning-a-wrong-answer