views:

413

answers:

4

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;
}
+3  A: 

it's expecting radians

Jimmy
+6  A: 

You are computing the sin of 100.7 radians, and the answer given is the correct one.

http://www.google.com/#hl=en&amp;source=hp&amp;q=100.7+degrees+in+radians&amp;aq=f&amp;aqi=&amp;oq=&amp;fp=e8d6ef47431c6a4a

http://www.krellinst.org/uces/archive/resources/trig/node10.html

Hamish Grubijan
thanks everyone, that makes sense, I really need to brush up on my trig and by brush up i mean learn it for the first time, any good resources as it applies to objective - c and programming in general?
nickthedude
Trigonometry is language-agnostic.
pavium
Gawd ... this comes up so often! How do programmers not know this?
Hamish Grubijan
+1  A: 

According to google, the answer is correct. Notice google assumes radians.

http://www.google.com/search?hl=en&amp;q=sin+of+100.7

James Lawruk
+3  A: 

It's expecting radians. To get the answer you want, convert degrees to radians first:

// [radians] = [degrees] * [pi]/180
double theta = 100.7 * 3.14159/180;
// sin(1.757 radians) == ~0.98
double result = sin(theta);
John Feminella
For maximum accuracy, you should use the predefined constant for pi if your language provides it. For example, in C that is M_PI.
Eric Skroch