2016-12-07 42 views
1

我有流数据,作为介于-2和+4之间的浮点值。我需要编写一个函数,将这些值在-1和+1之间进行归一化。正常化-1和1之间的负数或正数?

我:

float normalize(float input) 
{ 
    int min = -1; 
    int max = 1; 
    float normalized_x = (input - min)/(max - min); 
    return normalized_x; 
} 

但是,这给了我,是不正确的值,范围从-0.4到+2.3,粗略。我需要在我的功能中调整什么?

谢谢。

+5

难道你不感到惊讶,价值观' -2'和'4'不会出现在公式中的任何位置?源范围很重要,你不同意吗? –

+0

这就是输入数据的范围,所以'float input' – anti

+0

@Igor实际上并不需要这些。 (虽然你确实需要从他们派生的值) – Iluvatar

回答

5

你想第一中心围绕0的范围内,然后分让它去从-1到1

float normalize(float input) 
{ 
    float normalized_x = (input - 1)/3; 
    return normalized_x; 
} 

更广义的:

const float min = -2; 
const float max = 4; 
float normalize(float input) 
{ 
    float average  = (min + max)/2; 
    float range  = (max - min)/2; 
    float normalized_x = (input - average)/range; 
    return normalized_x; 
} 
+0

这看起来可行。谢谢! – anti