# How do you use sin, cos, and tan?

Discussion in 'iOS Programming' started by MrPenguin9, May 13, 2009.

1. ### MrPenguin9 macrumors member

Joined:
Aug 1, 2008
#1
How do you use sin, cos, and tan in the iPhone sdk? And also how do you use the inverse of them?

Thanks

2. ### SilentPanda Moderator emeritus

Joined:
Oct 8, 2002
Location:
The Bamboo Forest
#2
I'm guessing you'd just use the C versions of cos, sin, and tan.

Joined:
Aug 1, 2008
4. ### PhoneyDeveloper macrumors 68040

Joined:
Sep 2, 2008
#4
Code:
`x = sin(y);`
Is there something more?

Try typing this in the terminal or google it

man sin

5. ### MrPenguin9 thread starter macrumors member

Joined:
Aug 1, 2008
#5
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

6. ### Ron C macrumors member

Joined:
Jul 18, 2008
Location:
Chicago-area
#6
Smells like a type issue

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)

7. ### MrPenguin9 thread starter macrumors member

Joined:
Aug 1, 2008
#7
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!

8. ### Guiyon macrumors 6502a

Joined:
Mar 19, 2008
Location:
North Shore, MA
#8
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"

9. ### Ron C macrumors member

Joined:
Jul 18, 2008
Location:
Chicago-area
#9
A quick note about automatic conversion and floating-point weirdness...

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.

Joined:
Aug 1, 2008