将分数转换为浮点数

时间:2016-09-27 11:25:01

标签: c embedded blackfin

我尝试将分数转换为浮点数并将其用于比较。 但是值太小而且对于布尔变量的结果它返回true。我的汇合是正确的吗?或者我应该以另一种我不知道的方式来做这件事?

测试用例:

  // result is -0.0074
  float coilh0re = fr32_to_float(GO_coil_H[0].re)*0.8f;
  // result is -0.0092
  float coilrefundamental = fr32_to_float(CoilEepromData.coilboardhspule.reFundamental);
  // result is -0.01123
  float coilh0re2 = fr32_to_float(GO_coil_H[0].re)*1.2f;
  -0.0074>-0.0092> -0.01123

这是一段剪辑代码

       bool resultA  = fr32_to_float(GO_coil_H[0].re)*0.8f < fr32_to_float(CoilEepromData.coilboardhspule.reFundamental)  ? 1 : 0;
       bool resultB  = fr32_to_float(CoilEepromData.coilboardhspule.reFundamental)  <= fr32_to_float(GO_coil_H[0].re)*1.2f ? 1 : 0;

       bool resultAB = !(resultA & resultB); // always true

       bool resultC  =  fr32_to_float(GO_coil_H[1].re)*0.8f < fr32_to_float(CoilEepromData.coilboardhspule.reHarmonic) ? 1:0;
       bool resultD  = fr32_to_float(CoilEepromData.coilboardhspule.reHarmonic)  <= fr32_to_float(GO_coil_H[1].re)*1.2f ? 1:0;

       bool resultCD = !(resultC & resultD); // always true

       bool resultE  =  fr32_to_float(GO_coil_H[0].im)*0.8f < fr32_to_float(CoilEepromData.coilboardhspule.imFundamental)? 1 : 0;
       bool resultF  =  fr32_to_float(CoilEepromData.coilboardhspule.imFundamental) <= fr32_to_float(GO_coil_H[0].im)*1.2f ? 1 : 0;

       bool resultEF = !(resultE & resultF);// always true

       bool resultG  =  fr32_to_float(GO_coil_H[1].im)*0.8f < CoilEepromData.coilboardhspule.imHarmonic ? 1 : 0;
       bool resultH  =  fr32_to_float(CoilEepromData.coilboardhspule.imHarmonic) <= fr32_to_float(GO_coil_H[1].im)*1.2f ? 1 : 0;

       bool resultGH = !(resultG & resultH);// always true

        if(! ((fr32_to_float(GO_coil_H[0].re)*0.8f < fr32_to_float(CoilEepromData.coilboardhspule.reFundamental)) && (fr32_to_float(CoilEepromData.coilboardhspule.reFundamental) <= fr32_to_float(GO_coil_H[0].re)*1.2f) ) 
        || ! ((fr32_to_float(GO_coil_H[1].re)*0.8f < fr32_to_float(CoilEepromData.coilboardhspule.reHarmonic))    && (fr32_to_float(CoilEepromData.coilboardhspule.reHarmonic) <= fr32_to_float(GO_coil_H[1].re)*1.2f)    )
        || ! ((fr32_to_float(GO_coil_H[0].im)*0.8f < fr32_to_float(CoilEepromData.coilboardhspule.imFundamental)) && (fr32_to_float(CoilEepromData.coilboardhspule.imFundamental) <= fr32_to_float(GO_coil_H[0].im)*1.2f) )
        || ! ((fr32_to_float(GO_coil_H[1].im)*0.8f < fr32_to_float(CoilEepromData.coilboardhspule.imHarmonic))    && (fr32_to_float(CoilEepromData.coilboardhspule.imHarmonic) <= fr32_to_float(GO_coil_H[1].im)*1.2f)    ) )

        {
            eUserCode           = E_USER_SOIL_FAILED;
            eProcessState       = E_ERROR_HANDLING;  
        }

}

2 个答案:

答案 0 :(得分:1)

如果出现OP,则要测试值reFundamental是否在re的+/- 20%范围内。这不是float精度问题,而是数学问题。

// Simplified problem
float re = -0.01123f/1.2f;
float reFundamental = -0.0092f;
bool resultA  = re*0.8f < reFundamental;
bool resultB  = reFundamental <= re*1.2f;
bool resultAB = !(resultA & resultB); // always true

但值为负数,因此<<=反转

各种替代方案。示例:(调整味道)

bool in_range(float x, float limit, float factor) {
  float limitp = limit*(1.0f + factor);
  float limitm = limit*(1.0f - factor);

  if (x > limitm) return x <= limitp;
  if (x < limitm) return x >= limitp;
  return x == limitp;
}

bool resultAB = !in_range(fr32_to_float(CoilEepromData.coilboardhspule.reFundamental),   
    fr32_to_float(GO_coil_H[0].re), 0.20);

答案 1 :(得分:0)

如果你想比较分数 - 根本不要使用浮点数。将它们转换为相同的分母并比较分子。