为什么这个罪的方法返回一个错误的答案? [英] why is this sin method returning a wrong answer?

查看:145
本文介绍了为什么这个罪的方法返回一个错误的答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,工作在一些类别,我碰到一个奇怪的问题,基本上扩大计算器类添加一些trig方法,我得到一个不正确的值,当我调用sin方法在返回形式一个双。我发送一个值100.7到方法,它返回0.168231,从我可以看到正确的值应该是= 0.939693或有abouts。



继承代码,我

(感谢)



http://files.me.com/knyck2/svpfd4

  // 
// Calculator_trig.m
// 11.4_calculator_trig
//
//由Nicholas Iannone创建1 / 6/10。
//版权所有2010 __MyCompanyName__。版权所有。
//

#importCalculator_trig.h
#import< math.h>

@implementation计算器(三角函数)

- (double)sin
{
double result;

result =(double)sin(accum);

返回结果;

}
- (double)cos
{
double result;

result = cos(accum);

返回结果;
}
- (double)tan
{
double result;

result = tan(accum);

返回结果;
}

@end

#importCalculator.h


@implementation计算器
- (void)setAccumulator:(double)value
{
accumulator = value;
}

- (void)clear
{
accumulator = 0;
}

- (double)accumulator
{
return accum;
}

- (double)memoryClear
{
memory = 0;
NSLog(@memory has been cleared);
return accum;
}

- (double)memoryStore
{
memory = accum;
NSLog(@内存已设置为%g,内存);
return accum;
}

- (double)memoryRecall
{
accumulator = memory;
NSLog(@累计器已设置为%g,累加器);
return accum;
}

- (double)memoryAdd
{
memory + = accumulator;
NSLog(@accumulator:%g已添加到内存,内存现在为%g,累加器,内存);
return accum;
}

- (double)memorySubtract
{
memory - = accumulator;
NSLog(@accumulator:%g已从内存中减去,内存现在为%g,累加器,内存);
return accum;
}

- (double)add:(double)value
{
accumulator + = value;
return accum;
}

- (double)subtract:(double)value
{
accumulator - = value;
return accum;
}

- (double)multiply:(double)value
{
accumulator * = value;
return accum;
}

- (double)divide:(double)value
{
accumulator / = value;
return accum;
}

- (double)changeSign
{
accumulator = -accumulator;
return accum;
}

- (double)reciprocal
{
accumulator = 1 / accumulator;
return accum;
}

- (双)xSquared
{
累加器* =累加器;
return accum;
}
@end

#import< Foundation / Foundation.h>
#importCalculator.h
#importCalculator_trig.h

int main(int argc,const char * argv []){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

计算器* 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;
}


解决方案

100.7弧度,给出的答案是正确的。


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.

这篇关于为什么这个罪的方法返回一个错误的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆