cos() command not working in Linux

Hi,
I have written a c program to find cos() of a value , its not working, I am getting value of "val " as 0000.

#include<stdio.h>
#include<math.h>
main()
{
float val;
val = cosf( 1.570796);
printf("\nval = %f",val);
}

How did you compiled your program?
The standard C library math functions are not included in libc; instead, they're in a separate library, libm, which you need to specify explicitly. To compile and link your program the command should be:

% gcc -o your_prog your_prog.c -lm

Regards

That is not the problem; I compiled here and I get the same result (Ubuntu, GCC 4.2.4). Changing "cosf" to "cos" helped to get it to compile, too. But the result is still wrong. Changing the type of val to double also doesn't help.

This works, and prints -0.009000

printf("val = %f\n", cos (1.579796));

Correction, now I get the correct result if I change the type to double, or using cosf with float. I guess I looked at the result carelessly the first time. (Making the printf print a newline after and not before helped see what it does, too.) Sorry for any confusion.

thanks for the reply,
but its not working , I do link with -lm, still not getting correct value which is 0.999624217 but I am getting 0.000796
:confused:

ya I have done the same, its not working

Why do you believe that the cosine of (approximately) pi/2 is 0.999624217 rather than zero?

:slight_smile:

I put 1.570796 in my calculator and pressed the cos button. Sure enough, I got 0.999624217. But my calculator wakes up in degree mode. Shifting into radians, I repeated the experiment. This time I got .000000327. The math library works with radians, not degrees.

Actualy working towards finding DCT of samples, so need accurate values