Split floating point number into fractional and integral parts
<p>I'm writing a template class designed to work with any floating point type. For some of the methods I need to split a number into its integral and fractional parts. With primitive floating point types I can just cast to an integer to truncate the fractional part, but this wouldn't work with a big number class. Ideally my class would only use the four basic arithmetic operations (addition, subtraction, multiplication, division) in its calculations.</p>

<p>The method below is the solution I came up with. All it does is subtract powers of ten until the original number is less than 1. It works well, but seems like a brute-force approach. Is there a more efficient way to do this?</p>

<pre><code>template&lt; typename T &gt;
class Math
{
    public:

    static T modf( T const &amp; x, T &amp; intpart )
    {
        T sub = 1;
        T ret = x;

        while( x &gt;= sub )
        {
            sub *= 10;
        }

        sub /= 10;

        while( sub &gt;= 1 )
        {
            while( ret &gt;= sub )
            {
                ret -= sub;
            }

            sub /= 10;

        }//while [sub] &gt; 0

        intpart = x - ret;

        return ret;
    }
}
</code></pre>

<p>Note that I've removed the sign management code for brevity.</p>
