C++ templates for sin, cos, tan taylor series I used C++ metaprogramming to build taylor series for sin, cos, and tan (not really for tan). The whole thing is on my github. Please give any feedback related (but not limited) to style, functionality, etc.

Overall I think the biggest flaw is that the client needs to make their own struct with a static const long double member called value, and pass that struct to the "functions." I would have just passed the double, but C++ doesn't let you use floating-point numbers as template arguments. Using a struct was the only way I could think of to get around that.

Thanks!

#ifndef TRIG_HPP
#define TRIG_HPP

namespace mt
{
    const long double PI = 3.14159265358979323f;

    // FACTORIAL

    template &lt;int N&gt; struct fact
    {
        static const long long value = N * fact&lt;N-1&gt;::value;
    };

    template &lt;&gt; struct fact&lt;1&gt;
    {
        static const long long value = 1L;
    };

    // EXPONENTS

    template &lt;class X, int N&gt; struct power
    {
        static const long double value;
    };

    template &lt;class X, int N&gt; const long double power&lt;X, N&gt;::value = X::value * power&lt;X, N-1&gt;::value;

    template &lt;class X&gt; struct power&lt;X, 0&gt;
    {
        static const long double value;
    };

    template &lt;class X&gt; const long double power&lt;X, 0&gt;::value = 1.0f;

    // SIMPLIFY RADIAN TO INTERVAL [-2π, 2π]

    template &lt;class X&gt; struct SimplifyRad
    {
        static const long double value;
    };

    template &lt;class X&gt; const long double SimplifyRad&lt;X&gt;::value =

        X::value - (2 * PI * (double) (int) (X::value/(2*PI))); // float modulo?

    // COSINE

    template &lt;class X, int N&gt; struct cos
    {
        static const long double value;
    };

    template &lt;class X, int N&gt; const long double cos&lt;X, N&gt;::value = 

        (((N % 2 == 0 ? 1 : -1) * power&lt;SimplifyRad&lt;X&gt;, 2*N&gt;::value) / ((long double) fact&lt;2*N&gt;::value)) + cos&lt;X, N-1&gt;::value;

    template &lt;class X&gt; struct cos&lt;X, 0&gt;
    {
        static const long double value;
    };

    template &lt;class X&gt; const long double cos&lt;X, 0&gt;::value = 1.0f;

    // SINE

    template &lt;class X, int N&gt; struct sin
    {
        static const long double value;
    };

    template &lt;class X, int N&gt; const long double sin&lt;X, N&gt;::value =

        (((N % 2 == 0 ? -1 : 1) * power&lt;SimplifyRad&lt;X&gt;, 2*N-1&gt;::value) / ((long double) fact&lt;2*N-1&gt;::value)) + sin&lt;X, N-1&gt;::value;

    template &lt;class X&gt; struct sin&lt;X, 0&gt;
    {
        static const long double value;
    };

    template &lt;class X&gt; const long double sin&lt;X, 0&gt;::value = 0.0f;

    // TANGENT

    template &lt;class X, int N&gt; struct tan
    {
        static const long double value;
    };

    template &lt;class X, int N&gt; const long double tan&lt;X, N&gt;::value = sin&lt;X, N&gt;::value / cos&lt;X, N&gt;::value;
}

#endif


Example usage:

#include &lt;cstdio&gt;
#include "../include/trig.hpp"

#define DEPTH 8

struct var
{
    static const long double value;
};

const long double var::value = mt::PI / 4;

int main(int argc, char *argv[])
{
    printf("%Lf\n", var::value);
    printf("sin(%Lf) = %Lf\ncos(%Lf) = %Lf\ntan(%Lf) = %Lf\n",
            var::value, mt::sin&lt;var, DEPTH&gt;::value,
            var::value, mt::cos&lt;var, DEPTH&gt;::value,
            var::value, mt::tan&lt;var, DEPTH&gt;::value);
}


Output:

$ g++ main.cpp
$ ./a.exe
0.785398
sin(0.785398) = 0.707107
cos(0.785398) = 0.707107
tan(0.785398) = 1.000000
I see several issues with your code.

Your motivation should be stronger/clearer

While you may have a very good motivation for having this functionality, it is not apparent from your post hear nor from your GitHub repo. At compile time there are typically very few values I'd need the sine or cosine of; is it really clearer to write

printf("%f", apply_my_complicated_TMP_construct(PI/4, 8) )


than

auto sine_of_quarter_pi = 0,707106781187f;
printf("%f", sine_of_quarter_pi);


? I don't know that it is. Plus, the second way the reader doesn't need to think about iterations, the taylor series etc. It will also reduce compilation time.

Now, you could say "Ah, but I have a lot of these values to apply sin() or cos()" - but honestly, I doubt it. If that's the case you should just do that offline and independently of developing your C++ code.

In your repository, you list the following advantages:


  
  All the work is done a compile-time.
  The compile-time efficiency is actually going to be better than run-time efficiency compared to "normal,"
  


But, again, there isn't really any work; and there's no "efficiency" issue if you just use the value you need. Even if you had to calculate a sine or cosine a couple dozen times - that's really negligible.

Don't reinvent the wheel I

There's an M_PI constant available in POSIX-compliant system's &lt;math.h&gt; header (and thus, effectively, in most &lt;cmath&gt; headers, including MSVC's with a bit of work, see here). In case you want to be more portable, you might do:

#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif


... but, in fact, these days you can even get a better accuracy constant, M_PIl, on some systems. So you would have actually gained from not reinventing the wheel.

... and there's more. Remember my sqrt(2)/2 from the example above? That's often predefined too. Have a look at your system's &lt;math.h&gt;.

Don't reinvent the wheel II

Someone has already provided what looks like a more robust implementation of the same functionality - here on the site: 

constexpr Sin Function C++ 14

Now, granted, I haven't scrutinized that code. Maybe it's buggy or has other problems. But I'll bet you 100 reputation you haven't read that implementation before writing yours... although you could have, if you'd have looked. I didn't know about it before I was writing this review.

Drop most template recursion in favor of C++14 constexpr functions

You use recursive template instantiation when you could just put a for loop in your function - and sometimes not even need a template:

constexpr long long factorial(int n)
{
    return (n &lt;= 1) ? 1L : (n * factorial(n - 1));
}


(taken from here). Also, when you can't just "go constexpr", you can still iterate over multiple template arguments with for_each_argument and so on.