• Create Account

### #ActualCornstalks

Posted 11 January 2013 - 08:44 AM

I have to object to a definition of logarithm that says that the logarithm of 0 is 0. You should define the logarithm of 1 as 0, simplify your formula (no need for the funky !!') and let the compiler complain if someone tries to evaluate the logarithm of 0.

I did that to keep things really simple. How's the following?

 // b = base, n = number, r = recursion depth level
template <unsigned int b, unsigned int n, unsigned int r = 0, bool recurse = n >= b, bool valid = (n > 0) && (b > 1)>
struct log;

template <unsigned int b, unsigned int n, unsigned int r, bool recurse>
struct log<b, n, r, recurse, true>
{
enum { value = log<b, n / b, r + 1>::value };
};

template <unsigned int b, unsigned int n, unsigned int r>
struct log<b, n, r, false, true>
{
enum { value = r };
};


I'm not sure how much I like the above. Is there a better way to do it?

### #1Cornstalks

Posted 11 January 2013 - 08:43 AM

I have to object to a definition of logarithm that says that the logarithm of 0 is 0. You should define the logarithm of 1 as 0, simplify your formula (no need for the funky !!') and let the compiler complain if someone tries to evaluate the logarithm of 0.

I did that to keep things really simple. How's the following?


</p><div>template <unsigned int b, unsigned int n, unsigned int r = 0, bool recurse = n >= b, bool valid = (n > 0) && (b > 1)></div>
<div>struct log;</div>
<div> </div>
<div>template <unsigned int b, unsigned int n, unsigned int r, bool recurse></div>
<div>struct log<b, n, r, recurse, true></div>
<div>{</div>
<div>    enum { value = log<b, n / b, r + 1>::value };</div>
<div>};</div>
<div> </div>
<div>template <unsigned int b, unsigned int n, unsigned int r></div>
<div>struct log<b, n, r, false, true></div>
<div>{</div>
<div>    enum { value = r };</div>
<div>};</div>
<div>

I'm not sure how much I like the above. Is there a better way to do it?

PARTNERS