View Full Version : How do you use sin, cos, and tan?
MrPenguin9
May 13, 2009, 06:28 PM
How do you use sin, cos, and tan in the iPhone sdk? And also how do you use the inverse of them?
Thanks
SilentPanda
May 13, 2009, 06:31 PM
I'm guessing you'd just use the C versions of cos, sin, and tan.
MrPenguin9
May 13, 2009, 08:01 PM
Can I have an example of that please. :)
Thanks
PhoneyDeveloper
May 13, 2009, 08:46 PM
x = sin(y);
Is there something more?
Try typing this in the terminal or google it
man sin
MrPenguin9
May 13, 2009, 09:29 PM
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
Ron C
May 13, 2009, 10:25 PM
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:
#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)
MrPenguin9
May 13, 2009, 11:06 PM
Here my code from MainView.m
#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! :)
Guiyon
May 14, 2009, 12:12 AM
-(void)awakeFromNib {
Text.text = [NSString stringWithFormat:@"%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"
Ron C
May 14, 2009, 04:49 PM
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:
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:
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.
MrPenguin9
May 15, 2009, 09:18 AM
It worked. :D
Thank you all for helping me! :)
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.