Why does this sin method return the wrong answer?

Hey, working on some categories, and I ran into some strange problem, basically extending the calculator class to add some trigger methods, and I get the wrong value when I call the sin method in the return to double form. I send the value 100.7 to the method and returns 0.168231, from which I can see the correct value should be = 0.939693 or theres abouts.

Here is the code, I am also attaching a link to the full project here:

(thank)

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
source share
5 answers

100,7 , .

+12

. , :

// [radians] = [degrees] * [pi]/180
double theta = 100.7 * M_PI/180;

// sin(1.757 radians) == ~0.98
double result = sin(theta);
+10

+5

sin . , .

?

Simple.

360 . ?

, .

, - .

?

, π .

?

, 2 . - , , . - , .

,

- π * = π * 2 * = 2π . 2πr, r - .

, ?

. Tada 2πr/r = 2π.

2π 360 .

, , ?

, 2π 360.

, 2π/360 = π/180.

- , "". 180 π . , π /180 , .

, 107 , 107

IS 107 * 1 = 107 * π /180 . , . 107 * π/180.

Objective-c M_PI - , π.

,

#define radianperdegree (M_PI/180) 

1. 1, . , 1 1 .

, . , , , 1. , . . . , - , .

double result = sin(100.7 * radianperdegree);

.....

?

Another thing you can do is determine

#define RADTODEG(x) ((x) * 57.29578)
#define DEGTORAD(x) ((x) / 57.29578)

And then use sin (DEGTORAD (107))

0
source

Source: https://habr.com/ru/post/1727571/


All Articles