libwrpにmath.hxxを加え、max_power_of関数を定義しました。
この関数は「N以下で最大のBのべき乗Rを得る」関数です。
<使用例>
R = wrp::math::max_power_of<B>(N);
C++, using GeSHi 1.0.8.8
#include <iostream>
#include <wrp/math.hxx>
int main()
{
std::cout
<<wrp::math::max_powert_of<2>(9000)<<std::endl
<<wrp::math::max_powert_of<3>(9000)<<std::endl
<<wrp::math::max_powert_of<4>(9000)<<std::endl
<<wrp::math::max_powert_of<5>(9000)<<std::endl
<<wrp::math::max_powert_of<6>(9000)<<std::endl
<<wrp::math::max_powert_of<7>(9000)<<std::endl
;
}
Parsed in 0.019 seconds at 19.57 KB/s
実行結果は以下の通り。
8192
6561
4096
3125
7776
2401
<主な用途>
- GPUの効率的なテクスチャサイズへのリサイズ
- 高速フーリエ変換に対する標本数の正規化
<実装>
C++, using GeSHi 1.0.8.8
template<size_t B, typename T>
inline const T max_power_of(const T n)
{
assert(B > 1);
assert(n > 0);
std::function<const T(const T)> f_ = [&](const T c)->const T
{
const T cb = c * static_cast<const T>(B);
return cb > n ? c : f_(cb);
};
return f_(1);
}
Parsed in 0.018 seconds at 15.66 KB/s
<参考>
5b0d8e97-fdcd-4e02-b2e0-ec6e7b744a75|0|.0