Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrPenguin9

macrumors member
Original poster
Aug 1, 2008
59
0
How do you use sin, cos, and tan in the iPhone sdk? And also how do you use the inverse of them?

Thanks
 
But when I do "tan(1)" in xcode it gives me back 1556013989!?!? But the tan of 1 is 0.017455... What am I doing wrong?

Thanks
 
Smells like a type issue

But when I do "tan(1)" in xcode it gives me back 1556013989!?!? But the tan of 1 is 0.017455... What am I doing wrong?

Thanks

1556013989? It might just be printing a floating-point-formatted value as an integer without the type conversion?

From whence are you getting this tan() function? math.h? What type does tan(), for example, return? Does it take radians or degrees? What type are you storing the return value into?

I checked the man page for tan() and it says:
Code:
     #include <math.h>

     double
     tan(double x);

     long double
     tanl(long double x);

     float
     tanf(float x);

DESCRIPTION
     The tan() function computes the tangent of x (measured in radians).
So.... the only function that matches that signature takes a double (automatic conversion) and returns a double (also automatic conversion) and takes radians.

How are you viewing the value? What does your code look like?

EDIT: I just checked the value of tan(1 radian) in Calculator.app - it says: 1.557408. So... why do you think it should be 0.017455? That's the value of tan(1 DEGREE)
 
Here my code from MainView.m
Code:
#import "MainView.h"

@implementation MainView

-(void)awakeFromNib {
	Text.text = [NSString stringWithFormat:@"%d", tan(1)];
}

@end
(Oh, and I didn't know that tan(x) was in radians.) Thank you guys for helping! :)
 
-(void)awakeFromNib {
Text.text = [NSString stringWithFormat:mad:"%d", tan(1)];
}

There's your problem, tan returns a double but the %d format string expects an integer. Changing the '%d' to '%lf' should fix that. Typing 'man 3 printf' at the terminal will give you a lot more information about the printf-style format strings that Apple (and a lot of other libraries) use; look for the part that starts with "The format string is composed of zero or more directives"
 
A quick note about automatic conversion and floating-point weirdness...

There's your problem, tan returns a double but the %d format string expects an integer. Changing the '%d' to '%lf' should fix that. Typing 'man 3 printf' at the terminal will give you a lot more information about the printf-style format strings that Apple (and a lot of other libraries) use; look for the part that starts with "The format string is composed of zero or more directives"

These "magic" type conversions don't occur unless the compiler knows that there should be one. One case where it won't know is in code like stringWithFormat: - it doesn't really know that there should be a conversion, so it merrily passes along a double. Once you start playing with floating-point types, you need to pay attention to that.

Another brief note about floating-point types. Don't write code like:
Code:
double a,b;
...
if (a == b) {
...
The problem with = and floating point types is that they're very unlikely to be equal. Even something like:
Code:
float a,b,c,prod1,prod2;
... // compute values for a, b, and c
prod1 = a*b*c;
prod2 = c*b*a;

if (prod1 == prod2) {
...
On your whiteboard, these numbers are the same. Inside the computer, they don't have to be. It's weird, but you need to get used to it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.