Builder Brent
Builder Brent

Reputation: 455

Why am I having this issue with the tan() function on iOS?

This is my first time building an iOS project using any kind of advanced math. I have searched for quite a while but have yet to find anything to fix my problem. I am still quite new to iOS so any help would be greatly appreciated.

I am trying to use a basic tan() function that takes an input from a textfield, turns it into degrees (because tan() defaults to radians) and displays in a label. If I was to input tan 30 into a calculator it would return .577, I am getting .449, which is close but if I input 31, i receive a value of "2.356"?

Here is the part of my code:

-(IBAction)calculate {
float x = ([textField1.text intValue]);
float d = (x * 180) / M_PI;
float y = tan(d);


label.text = [[NSString alloc] initWithFormat:@"%2.3f", y];
}

Upvotes: 2

Views: 2847

Answers (3)

saurb
saurb

Reputation: 685

your "d" is a float. Try using tanf() instead of tan().

Upvotes: 0

Dmorneault
Dmorneault

Reputation: 199

Try this

-(IBAction)calculate {
float x = textField1.text.doubleValue;
float d = (x * 180) / M_PI;
float y = tan(d);

label.text = [NSString stringWithFormat:@"%0.3f", y];

Upvotes: 0

user149341
user149341

Reputation:

The format string you're using (%2.f) is inappropriate for the data -- it's specifying that no decimal places should be displayed. Try just using %f.

Upvotes: 1

Related Questions