Provide true clock-modulus operation for arbitrary sign values I wrote this template to do as stated in the title; did I miss anything?

template&lt;typename T&gt; T clockmod(T v, T m) //todo: test T is numeric
{
    typedef long double D;
    T t=(m-m); //zero default
    D dv=std::fabs((D)v);
    D dm=std::fabs((D)m);
    if ((dv&gt;0.0)&amp;&amp;(dm&gt;0.0))
    {
        int id=int(dv/dm);
        D d=(dm*id);
        D dr=(dv-d);

        if (v&gt;0)
        {
            if (m&gt;0) t=(T)dr;
            else if (m&lt;0) t=(T)(dr-dm);
        }
        else if (v&lt;0)
        {
            if (m&gt;0) t=(T)(dm-dr);
            else if (m&lt;0) t=(T)(-dr);
        }
    }
    return t;
}


Where:
v / m may be any numeric type;
m (the modulus base) always range from 0 to m, m&lt;0 or m&gt;0;
the sign of v indicates the 'direction of counting' round the clock, positive increments from 0, negative decrements from m
Style comments:


Generic typename T implies you do not know or care what kind of type it is or which properties it has. Why not name it Numeric or something in that direction?
You should really use better names for variables. At first glance, no one has any idea what which variable is intended to do.
Even a single line if deserves braces. When omitting these and later adding to the code, programmers often forget to add them and introduce bugs.
typedef is a relic of C compatibility. Consider the using keyword instead.
Do not just arbitrary rename a type especially into something like D. You have an IDE it can do completion for you, use it. (And yes, even vim and emacs do completion, no excuses unless you write your code in notepad.)
Comment on your intentions and assumptions of the pieces of your code.


Solution issues:


  the sign of v indicates the 'direction of counting' round the clock, positive increments from 0, negative decrements from m



You are probably looking for this, meaning you should declare the template function and then offer specific implementations for the different possible instances of T.
Is a cast to long double really required? If T is a numeric type, all required operations should be defined for T. There is no reason to fall back to std::fabs().
If you want to do it by hand, I would suggest something in the way of

bool clockwise = (v &gt;= 0);
T absolute_v;
if(!clockwise){ // !clockwise implies v &lt; 0
    absolute_v = -v; // we want the abs(v)
} else {
    absolute_v = v;
}