quick C question

Scootin159

Diamond Member
Apr 17, 2001
3,650
0
76
I'm trying to use PI, cos & sin in C (linux gcc compiler). I included <math.h>, but cannot figure out how to use PI, ect.

Also, do you guys know where (under a typical redhat instalation) I would find the math.h file so I can just read it & see what is in there?
 

Scootin159

Diamond Member
Apr 17, 2001
3,650
0
76


<< RedHat 7.2

find / -name math.h

/usr/include/math.h
/usr/lib/bcc/include/math.h
>>



When all else fails go for the blatantly obvious (/usr/include):eek:. Thanks notfred. I also know how to do a search now - very helpful.
 

BlackOmen

Senior member
Aug 23, 2001
526
0
0
Hmm, someone correct me if I'm wrong, but the include file does not include a constant PI. I was taught to do const double PI=4*atan(1.0);.
 

Scootin159

Diamond Member
Apr 17, 2001
3,650
0
76
Found this in the math.h file:

# define M_# define M_PI 3.14159265358979323846 /* pi */
 

Scootin159

Diamond Member
Apr 17, 2001
3,650
0
76
still can't find how to do cos, should be y = cos(x); right?

/* Cosine of X. */
#define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)

(from tgmath.h) <- which I included as well.
 

BlackOmen

Senior member
Aug 23, 2001
526
0
0
I stand corrected on PI. As for cosine, it should be just y=cos(x); where x is in radians.
 

Scootin159

Diamond Member
Apr 17, 2001
3,650
0
76
It seems it should be that way, but:

float theta = 0;
//convert theta to radians
theta *= (float)(M_PI/180);
c = cos(5);


gives me this:

gcc 1.c
/tmp/ccyHH7o0.o: In function `main':
/tmp/ccyHH7o0.o(.text+0x16c): undefined reference to `cos'
collect2: ld returned 1 exit status
 

BlackOmen

Senior member
Aug 23, 2001
526
0
0
Works fine for me using gcc 2.95.3. You're absolutely sure you remember to include math.h?

Otherwise I would attribute it to the old version of gcc.
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
You need to include -lm at the end of your compile line to include the math library in the link. The math library is not included by default on links.
 

Scootin159

Diamond Member
Apr 17, 2001
3,650
0
76


<< You need to include -lm at the end of your compile line to include the math library in the link. The math library is not included by default on links. >>



That was it. Thanks guys.