Sine to radians Objective-C

Go To StackoverFlow.com

-1

This part of an app that I am working on, I have the following equation in objective C. All variables are floats. r is a sine of an angle and I would like to convert r to radians when diplayed in the text field.

r = ns/ni
r = sinf(r);
rr = asinf(r);

textfieldRi.text = [NSString stringwithFormat:@"%.02f", rr];

I am not getting correct results. Any suggestions please?

2012-04-04 05:15
by MacUser


2

I'm not entirely certain what you think you're gaining by executing a sine followed by an inverse sine, except possibly to ensure the range of r is reduced to it's smallest range, in which case asin(sin(r)) should work fine, assuming r is already in radians.

If you want to convert r to radians, I have to assume it's currently in degrees, in which case you just need to do something like:

radians = degrees * 3.141592653589 / 180.0; // or M_PI in math.h.
2012-04-04 05:38
by paxdiablo
I would use M_PI instead of 3.14159 though.. - lnafziger 2012-04-04 05:41
Yeah, I never know whether ObjC has all the stuff that C has, so I was just playing it safe. If you have a PI constant, use that. Updated to incorporate the suggestion - paxdiablo 2012-04-04 05:44
Thank you for this advice worked - MacUser 2012-04-04 18:33


1

Objective-c automatically uses radians instead of degrees, however to convert degrees to radians divide by 180 then multiply by PI.

2012-04-04 05:40
by Otium
I m just starting to use the mathmatical function in objective C this is very important to know, thanks - MacUser 2012-04-04 18:35
Ads