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

objective-c – 为什么这个sin方法会回答错误的答案?,第1张

概述嘿,在一些类别上工作,我遇到了一个奇怪的问题,我基本上扩展了一个计算器类来添加一些trig方法,当我在返回时调用sin方法时,我得到的值不正确一双.我发送一个100.7的值到该方法,它返回0.168231,从我可以看到正确的值应该是= 0.939693或那里. 下面是代码,我还在这里附加了一个完整项目的链接: (谢谢) http://files.me.com/knyck2/svpfd4 //// 嘿,在一些类别上工作,我遇到了一个奇怪的问题,我基本上扩展了一个计算器类来添加一些trig方法,当我在返回时调用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); 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弧度的罪,给出的答案是正确的. 总结

以上是内存溢出为你收集整理的objective-c – 为什么这个sin方法会回答错误的答案?全部内容,希望文章能够帮你解决objective-c – 为什么这个sin方法会回答错误的答案?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1003345.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存