代码之家  ›  专栏  ›  技术社区  ›  ilya1725

将浮点值的向量传递给函数会稍微改变值[闭合]

  •  0
  • ilya1725  · 技术社区  · 7 年前

    我有一个非常简单的小函数,用来验证传递的 vector :

    bool Verify( std::vector<std::pair<float, float>> const & source) {
        // The lookup table must be changing linearly on input
        float delta_x{};
        for (unsigned i = 1; i < source.size(); i++) {
            auto input_delta = std::abs(source[i].first - source[i-1].first);
            if (i == 1) {
                delta_x = input_delta;
            } else {
                std::cout << "LUT"
                  << "Expected step: '" << std::setprecision(10) << delta_x << "'."
                  << " measured step[" << i << "]: '"
                  << std::setprecision(10) << input_delta << "'"
                  << (input_delta-delta_x)
                  << ":" << source[i].first << "-" << source[i-1].first << std::endl;
            if (delta_x != input_delta) {
                return false;
            }
         }
      }
    
      return true;
    }
    

        std::vector<std::pair<float, float>>kDecodeLut{
      {  -1.326, 101.3974},
      {   6.174, 96.0049 },
      {  13.674, 91.5644 },
      {  21.174, 87.5549 },
      {  28.674, 83.7873 },
      {  36.174, 80.1683 },
      {  43.674, 76.6441 },
      {  51.174, 73.1802 },
      {  58.674, 69.7524 }};
    

    Verify 方法see与向量中的see不完全相同。这是打印件:

    LUTExpected step: '7.5'. measured step[2]: '7.5'0:13.67399979-6.173999786
    LUTExpected step: '7.5'. measured step[3]: '7.5'0:21.17399979-13.67399979
    LUTExpected step: '7.5'. measured step[4]: '7.5'0:28.67399979-21.17399979
    LUTExpected step: '7.5'. measured step[5]: '7.5'0:36.17399979-28.67399979
    LUTExpected step: '7.5'. measured step[6]: '7.5'0:43.67399979-36.17399979
    LUTExpected step: '7.5'. measured step[7]: '7.5'0:51.17399979-43.67399979
    LUTExpected step: '7.5'. measured step[8]: '7.5'0:58.67399979-51.17399979
    

    看起来好像有一些原始的数字转换 kDecodeLut 验证 .

    真的有什么转变吗?我不是故意的 要以任何形式复制或修改的向量。


    编辑 : 我添加了代码来打印值 使命感 验证

     for (unsigned i = 1; i < kTiltWingDecodeLut.size(); i++) {
      std::cout << "LUT"
                << ":" << kTiltWingDecodeLut[i].first << "-" << kTiltWingDecodeLut[i-1].first << std::endl;
    }
    Verify(kTiltWingDecodeLut);
    

    LUT:6.174--1.326
    LUT:13.674-6.174
    LUT:21.174-13.674
    LUT:28.674-21.174
    LUT:36.174-28.674
    LUT:43.674-36.174
    LUT:51.174-43.674
    LUT:58.674-51.174
    LUTExpected step: '7.5'. measured step[2]: '7.5'0:13.67399979-6.173999786
    LUTExpected step: '7.5'. measured step[3]: '7.5'0:21.17399979-13.67399979
    LUTExpected step: '7.5'. measured step[4]: '7.5'0:28.67399979-21.17399979
    LUTExpected step: '7.5'. measured step[5]: '7.5'0:36.17399979-28.67399979
    LUTExpected step: '7.5'. measured step[6]: '7.5'0:43.67399979-36.17399979
    LUTExpected step: '7.5'. measured step[7]: '7.5'0:51.17399979-43.67399979
    LUTExpected step: '7.5'. measured step[8]: '7.5'0:58.67399979-51.17399979
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Khouri Giordano    7 年前

    这是舍入的情况。正如你所看到的 this page ,6.174,当转换为32位浮点时,更像是6.1739998。该页解释了实际表示:2^2*1.5434999(二进制中的1.1000101100101000)。64位浮点值使用 附加的

    如果您使用相同的 setprecision(10)