Evaluating cos


noblepa
05-14-2009, 11:35 PM
I have a doubt is there a solution to get

cos(90*pi/180) ==0.0
in c++ (ofcourse not in display, but in computation).

assume I have put
float pi=3.1415926;

The way I did it is like this

float cos0(float x)
{
if(cos(x) < 1E-08)
return 0.0;
else
return cos(x);
}


Any other more valid solutions please.

Thank you in advance!:p

davekw7x
05-15-2009, 08:48 AM
I have a doubt...

I have a lot of doubts. A lot.

However...

Your stated program requirement can be satisfied by


float pi = 3.1415926;
float cos0(float x)
{
if (x == (90*pi/180)) {
return 0.0;
}
else {
return cos(x);
}
}


Actually, since the only functional specification you gave is to require a return a value of zero for one particular value of its argument, your stated requirement can be also be met by

float pi = 3.1415926;
float cos0(float x)
{
if (x == (90*pi/180)) {
return 0.0;
}
else {
return 42.0;
}
}


Now, I have serious doubts about whether either of my silly little examples would satisfy your requirements, and I hope that I didn't hurt your feelings.

But here's the deal:

To increase chances of getting a meaningful response (that might be actually helpful), I respectfully suggest that you give us some more information.


Please state the specification of your function. Exactly.

How about telling us what you are going to do with the value that is returned by your function?

How about telling us why are you using floats? Why not doubles?

What the heck does this have to do with the topics of this part of the NR forum, namely Numerical Recipes Version 2 C++ programming?


Regards,

Dave