-1

我正在尝试将分数转换为浮点并将其用于比较。但值太小,对于布尔变量的结果返回 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;  
        }

}
4

2 回答 2

1

如果出现 OP 想要测试一个值reFundamental是否在 +/-20% 的范围内re。这不是一个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

但值是负数,所以<and<=应该颠倒过来

各种替代品。示例:(根据口味调整)

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);
于 2016-09-27T13:22:40.583 回答
0

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

于 2016-09-27T11:27:59.280 回答