Compile time sine
How can C++ be used to compute sine values for constant arguments, without using macros?
That is, how can sin(0.3) be converted to its value as compile time, moved into the instruction?
Wizza Wuzza?
I know this doesn''t help... but I personally would just bust out MS calculator and do something like this:
somevar = 0.29552020 // sine .3
TempusElf
somevar = 0.29552020 // sine .3
TempusElf
Granted, but in a long expression, things get messy quickly.
I''d though about using an intermediated template like
but double can''t be a type argument.
I might be able to statically convert the double to some aux class at compile time, i''ll have to look at that.
Wizza Wuzza?
I''d though about using an intermediated template like
template <double x> c_sin {public: const static double val = ... // taylor};var = c_sin<0.3>::val // not ideal anyway
but double can''t be a type argument.
I might be able to statically convert the double to some aux class at compile time, i''ll have to look at that.
Wizza Wuzza?
What compiler are you using? Depending on how smart your compiler is you can get different ways to do this. For example, on MSVC .NET 2003, you can do
const double sine_of_zero_point_three = sin(0.3);
and it will work happily.
(To be fair, there is a small static initialization overhead, but the actual code using the const variable will be just as efficient as if you used const double sine_of_zero_point_three = 0.29552. If this isn''t good enough, we can talk about the horrors of compile time template metaprogramming.)
const double sine_of_zero_point_three = sin(0.3);
and it will work happily.
(To be fair, there is a small static initialization overhead, but the actual code using the const variable will be just as efficient as if you used const double sine_of_zero_point_three = 0.29552. If this isn''t good enough, we can talk about the horrors of compile time template metaprogramming.)
I cant remember the details but i do remember reading about someone doing a recursive inline function to calculate sin/cos etc, not hot on the maths side of it but i beleive "true sin" is an infinitely long polynomial, once the recursion depth was upped on the compiler the compiler could completely calculate it at compile time
of course you should only use that for constants
of course you should only use that for constants
For the record you can approximate the sin function with a Taylor series (in words):
sin(x) = Sum of (-1)^n * x^(2x+1) / (2n+1)!
In theory, if you write down infinitely many terms this would be true. In practice I''d clip x to [0, 2 Pi] or [-pi, pi] or so.
[ Bananas | My dead site | www.sgi.com | Goegel ]
sin(x) = Sum of (-1)^n * x^(2x+1) / (2n+1)!
In theory, if you write down infinitely many terms this would be true. In practice I''d clip x to [0, 2 Pi] or [-pi, pi] or so.
[ Bananas | My dead site | www.sgi.com | Goegel ]
quote:Original post by Narcist
of course you should only use that for constants
If its a constant the compiler will probably calculate it at compile time anyway (although if your compiler options are set to something like ''minimum size'' it might leave it to runtime).
You write a template function that uses the taylor-series approximation, then you use a meta-template to perform your iteration calling the template taylor-series sin function.
The result is more than just inlined code; the sine values actually become part of the op codes.
I''ve been thinking about writing a meta-template FFT based on this idea...
The result is more than just inlined code; the sine values actually become part of the op codes.
I''ve been thinking about writing a meta-template FFT based on this idea...
This link has almost exactly what you want-except you''ll have to express you angle as a ratio of integers though:
metaprogramming
metaprogramming
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement