objective-c - 为什么这个 sin 方法会返回错误的答案?

标签 objective-c xcode class trigonometry calculator

嘿,在处理某些类别时我遇到了一个奇怪的问题,我基本上是在计算器类上扩展以添加一些触发方法,当我在 return 中调用 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;
}

最佳答案

你正在计算 100.7 弧度的 sin,给出的答案是正确的。

关于objective-c - 为什么这个 sin 方法会返回错误的答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2018081/

相关文章:

objective-c - 从另一个 modalViewController 解雇ModalViewController

ios - 从 xcode 中的 mainBundle 获取子目录

javascript - 第二个参数 Object.create

c++ - 错误 : Invalid base class C++

iphone - 调用 ABAddressBookGetPersonWithRecordID 的问题

objective-c - Swift 数组到 AnyObject 错误

iphone - iPhone应用程式已锁定地区

iOS Firebase 数据库网址

ios - 替换 iOS ALAssetsLibrary 中的 ALAsset 对象

c++ - 了解成员初始值设定项列表