Hi All,
Is there an interest into having a power() template calculating the N-th power of any number, when N is known at compile-time ?t uses the algorithm of the STL power function(), and runs between two or three times faster, and maybe could be of interest in the " which provides run-time and compile-time evaluation of the greatest common divisor (GCD) or least common multiple (LCM)
It simply works this way, for example to have PI at exponent three :
double a = power_const<3>( 3.14159)
As the single include file is very short, please apologize if I give it straight on, just to save your time and to have an idea how badly you judge my poor code ;)
Thank for your understanding.
/******************************************************************************
** power_const.h
******************************************************************************/
template< class _Tp, int Exp, int Bool > struct power_const_t {
};
template< class _Tp, int Exp > struct power_const_t< _Tp, Exp, 0 > {
inline static _Tp eval(const _Tp & the_t, const _Tp & the_P ) {
return power_const_t> 1), ( (Exp >> 1) & 1) >::eval( the_t, the_P * the_P );
}
};
template< class _Tp, int Exp > struct power_const_t< _Tp, Exp, 1 > {
inline static _Tp eval(const _Tp & the_t, const _Tp & the_P ) {
return the_P * power_const_t> 1), ( (Exp >> 1) & 1) >::eval( the_t, the_P * the_P );
}
};
template< class _Tp > struct power_const_t< _Tp, 1, 0 > {
inline static _Tp eval(const _Tp & the_t, const _Tp & the_P ) {
return the_P ;
}
};
template< class _Tp > struct power_const_t< _Tp, 1, 1 > {
inline static _Tp eval(const _Tp & the_t, const _Tp & the_P ) {
return the_P ;
}
};
template< class _Tp > struct power_const_t< _Tp, 0, 0 > {
inline static _Tp eval(const _Tp & the_t, const _Tp & the_P ) {
return 1 ;
}
};
template< class _Tp > struct power_const_t< _Tp, 0, 1 > {
inline static _Tp eval(const _Tp & the_t, const _Tp & the_P ) {
return the_P * the_P ;
}
};
template< class _Tp, int N > inline _Tp power_const( const _Tp & the_t ) {
return power_const_t::eval( the_t, the_t );
};
template< class _Tp, int N > inline _Tp power_const( const _Tp & the_t ) {
return power_const_t::eval( the_t, the_t );
};
/******************************************************************************
** power_const.h
******************************************************************************/
[Non-text portions of this message have been removed]