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;
}
You are computing the sin of 100.7 radians, and the answer given is the correct one.