私は次のコードを持っています:
template<typename I,typename O> O convertRatio(I input,
I inpMinLevel = std::numeric_limits<I>::min(),
I inpMaxLevel = std::numeric_limits<I>::max(),
O outMinLevel = std::numeric_limits<O>::min(),
O outMaxLevel = std::numeric_limits<O>::max() )
{
double inpRange = abs(double(inpMaxLevel - inpMinLevel));
double outRange = abs(double(outMaxLevel - outMinLevel));
double level = double(input)/inpRange;
return O(outRange*level);
}
使用法は次のようなものです。
int value = convertRatio<float,int,-1.0f,1.0f>(0.5);
//value is around 1073741823 ( a quarter range of signed int)
問題は、I=int
関数のO=float
デフォルトパラメータの場合です:
float value = convertRatio<int,float>(123456);
行のdouble(inpMaxLevel - inpMinLevel)
結果は -1.0 で、float で 4294967295 になると予想しています。
もっとうまくやるアイデアはありますか?基本的な考え方は、値を範囲から別の範囲に変換して、異なるデータ型の可能性を持たせることです。