Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ RGB颜色值为双精度-精度太大_C++_Colors_Double_Precision_Googletest - Fatal编程技术网

C++ RGB颜色值为双精度-精度太大

C++ RGB颜色值为双精度-精度太大,c++,colors,double,precision,googletest,C++,Colors,Double,Precision,Googletest,我目前正在重新编写一个名为RGBConverter的Arduino库,用于将RGB颜色空间中的颜色值转换为其他颜色。我还想添加一些使用gtest的测试,因为当涉及到以浮点数表示的颜色时,准确性确实很重要。这不仅适用于人眼的颜色感知,也适用于处理光的各种传感器 在创建一个基本测试用例时,我循环了一个RGB颜色值组件,例如红色;从0到10作为整数,并将其转换为它的双精度表示,我非常失望这个过程是多么不准确 以下是我的测试用例的代码: TEST(UnitCoversion, IntToDouble_s

我目前正在重新编写一个名为RGBConverter的Arduino库,用于将RGB颜色空间中的颜色值转换为其他颜色。我还想添加一些使用gtest的测试,因为当涉及到以浮点数表示的颜色时,准确性确实很重要。这不仅适用于人眼的颜色感知,也适用于处理光的各种传感器

在创建一个基本测试用例时,我循环了一个RGB颜色值组件,例如红色;从0到10作为整数,并将其转换为它的双精度表示,我非常失望这个过程是多么不准确

以下是我的测试用例的代码:

TEST(UnitCoversion, IntToDouble_single) {
  double x = 0.00000, x_res;
  int color = 0;
  for(; color < 10; color++) {
    printf("%d -> (expected: %.5f | actual: %.5f", color, x, x_res);
    RGBConverter::rgbIntToDouble_single(color, &x_res);
    EXPECT_NEAR(x, x_res, 0.0001);
    x+=0.004;
  }
}
不用说测试失败了:

The difference between x and x_res is 0.00015686274509803949, which exceeds 0.0001, where
x evaluates to 0.0080000000000000002,
x_res evaluates to 0.0078431372549019607, and
0.0001 evaluates to 0.0001.
3 -> (expected: 0.01200 | actual: 0.00784/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00023529411764705924, which exceeds 0.0001, where
x evaluates to 0.012,
x_res evaluates to 0.011764705882352941, and
0.0001 evaluates to 0.0001.
4 -> (expected: 0.01600 | actual: 0.01176/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00031372549019607898, which exceeds 0.0001, where
x evaluates to 0.016,
x_res evaluates to 0.015686274509803921, and
0.0001 evaluates to 0.0001.
5 -> (expected: 0.02000 | actual: 0.01569/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00039215686274509873, which exceeds 0.0001, where
x evaluates to 0.02,
x_res evaluates to 0.019607843137254902, and
0.0001 evaluates to 0.0001.
6 -> (expected: 0.02400 | actual: 0.01961/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00047058823529411847, which exceeds 0.0001, where
x evaluates to 0.024,
x_res evaluates to 0.023529411764705882, and
0.0001 evaluates to 0.0001.
7 -> (expected: 0.02800 | actual: 0.02353/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00054901960784313822, which exceeds 0.0001, where
x evaluates to 0.028000000000000001,
x_res evaluates to 0.027450980392156862, and
0.0001 evaluates to 0.0001.
8 -> (expected: 0.03200 | actual: 0.02745/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00062745098039215796, which exceeds 0.0001, where
x evaluates to 0.032000000000000001,
x_res evaluates to 0.031372549019607843, and
0.0001 evaluates to 0.0001.
9 -> (expected: 0.03600 | actual: 0.03137/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00070588235294118118, which exceeds 0.0001, where
x evaluates to 0.036000000000000004,
x_res evaluates to 0.035294117647058823, and
0.0001 evaluates to 0.0001.
[  FAILED  ] UnitCoversion.IntToDouble_single (1 ms)
[----------] 1 test from UnitCoversion (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] UnitCoversion.IntToDouble_single

 1 FAILED TEST
正如你所看到的,在浮点后的2-3位小数后,我得到的结果非常糟糕。我觉得这种行为太接近浮子了。不过我用的是双打


我的问题是:我是不是做错了什么?我确实意识到,对浮点数求和会在时间上累积舍入误差,但在这里它从一开始就开始了!使用0.1或0.01作为EXPECT_NEAR的绝对误差参数值几乎不是任何测试。。。这种程度的误差确实会影响用一堆LED测试的色觉。如果可能,我该如何改进?如果是这样的话,我还不如换成浮点数。至少在内存方面,它会更有效。

您计算了一些奇怪的东西,对于步骤1而不是步骤0,您有x==0.004和xres=1255。,这当然不等于0.004=1/250,在那之后误差就增大了。你的意思是1/255。是的,我忽略了这张脸。你能把答案贴出来吗?这样我就可以接受了。
The difference between x and x_res is 0.00015686274509803949, which exceeds 0.0001, where
x evaluates to 0.0080000000000000002,
x_res evaluates to 0.0078431372549019607, and
0.0001 evaluates to 0.0001.
3 -> (expected: 0.01200 | actual: 0.00784/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00023529411764705924, which exceeds 0.0001, where
x evaluates to 0.012,
x_res evaluates to 0.011764705882352941, and
0.0001 evaluates to 0.0001.
4 -> (expected: 0.01600 | actual: 0.01176/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00031372549019607898, which exceeds 0.0001, where
x evaluates to 0.016,
x_res evaluates to 0.015686274509803921, and
0.0001 evaluates to 0.0001.
5 -> (expected: 0.02000 | actual: 0.01569/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00039215686274509873, which exceeds 0.0001, where
x evaluates to 0.02,
x_res evaluates to 0.019607843137254902, and
0.0001 evaluates to 0.0001.
6 -> (expected: 0.02400 | actual: 0.01961/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00047058823529411847, which exceeds 0.0001, where
x evaluates to 0.024,
x_res evaluates to 0.023529411764705882, and
0.0001 evaluates to 0.0001.
7 -> (expected: 0.02800 | actual: 0.02353/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00054901960784313822, which exceeds 0.0001, where
x evaluates to 0.028000000000000001,
x_res evaluates to 0.027450980392156862, and
0.0001 evaluates to 0.0001.
8 -> (expected: 0.03200 | actual: 0.02745/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00062745098039215796, which exceeds 0.0001, where
x evaluates to 0.032000000000000001,
x_res evaluates to 0.031372549019607843, and
0.0001 evaluates to 0.0001.
9 -> (expected: 0.03600 | actual: 0.03137/RGBConverter/main.cpp:15: Failure
The difference between x and x_res is 0.00070588235294118118, which exceeds 0.0001, where
x evaluates to 0.036000000000000004,
x_res evaluates to 0.035294117647058823, and
0.0001 evaluates to 0.0001.
[  FAILED  ] UnitCoversion.IntToDouble_single (1 ms)
[----------] 1 test from UnitCoversion (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] UnitCoversion.IntToDouble_single

 1 FAILED TEST