First of all, if(x==MINVAL) makes no sense, I think you meant to write if(x < MINVAL) .
Second, I'm pretty certain that you would be better off using a Tailor series expansion. All you should need is a Taylor series expansion around 0 degrees for both the sine and cosine, which is valid (within you desired precision) on an interval from -45 degree to 45 degrees (i.e. -Pi/4 to Pi/4). With simple trig. identities, you should be able to use those two expansions to calculate a sine, cosine or tangent of any angle. For example, for the sine of an angle of 80 degrees, you can compute the cosine of -10 degrees (which is in the Taylor series' range for the cosine), and similarly for other ranges of values.
Third, you should also remember that branchings (e.g. conditional statements) are surprisingly expensive and slow, you should reduce those to a minimum. Also, mark your free-functions with the keyword "inline" to allow the compiler to inline if it leads to faster code.
Finally, if you are using or can use C++0x, you should mark those functions with constexpr keyword to allow compile-time computation of the values whenever possible.
mike_2000_17
Posting Virtuoso
2,137 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
First of all, if(x==MINVAL) makes no sense, I think you meant to write if(x < MINVAL) .
Second, I'm pretty certain that you would be better off using a Tailor series expansion. All you should need is a Taylor series expansion around 0 degrees for both the sine and cosine, which is valid (within you desired precision) on an interval from -45 degree to 45 degrees (i.e. -Pi/4 to Pi/4). With simple trig. identities, you should be able to use those two expansions to calculate a sine, cosine or tangent of any angle. For example, for the sine of an angle of 80 degrees, you can compute the cosine of -10 degrees (which is in the Taylor series' range for the cosine), and similarly for other ranges of values.
Third, you should also remember that branchings (e.g. conditional statements) are surprisingly expensive and slow, you should reduce those to a minimum. Also, mark your free-functions with the keyword "inline" to allow the compiler to inline if it leads to faster code.
Finally, if you are using or can use C++0x, you should mark those functions with constexpr keyword to allow compile-time computation of the values whenever possible.
I would suspect using trig function be just as fast if not faster than taylor expansion.
@OP: Unless you are in a really limited environment, there is no need for all this complication. You should just use a simple one liner function call.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608