使用opencv将float和double值保存或读取到yaml文件时值发生变化
values changing while saving or reading float and double values to yaml file using opencv
设置:ubuntu 18.04.
上的 opencv 3.2
我使用 YAML 文件保存了一个整数、一个浮点数和一个双精度值。 YAML文件中float和double的值与程序写入的值不同
#include <opencv2/core/core.hpp>
int main(int ac, char** av)
{
cv::FileStorage fs("file.yaml", cv::FileStorage::WRITE); // create FileStorage object
int a = 1;
float b = 0.2;
double c = 0.3;
fs << "a" << a;
fs << "b" << b;
fs << "c" << c;
fs.release(); // releasing the file.
return 0;
}
file.yaml 读取
%YAML:1.0
---
a: 1
b: 2.0000000298023224e-01
c: 2.9999999999999999e-01
此外,当我使用代码所属读取上述 YAML 文件时,我得到了 float 和 double 值的更改值。
#include <opencv2/core/core.hpp>
#include <iostream>
int main(int ac, char** av)
{
cv::FileStorage fs("file.yaml", cv::FileStorage::READ); // create FileStorage object
int a; float b; double c;
fs["a"] >> a;
fs["b"] >> b;
fs["c"] >> c;
std::cout << "a, b, c="<< a << ","<< b << ","<< c << std::endl;
fs.release(); // releasing the file.
return 0;
}
以上程序的输出和保存的YAML文件:
a, b, c=1,0.2,0.3
我的问题是如何在不更改值的情况下使用 opencv 读写 float 和 double 值from/to YAML 文件
The values of the float and the double in the YAML file is different from the values which are written by the program.
float
和 double
使用二进制基数的浮点格式表示。这些格式无法表示值 0.2 和 0.3。最接近的可表示值是 0.20000000298023223876953125 和 0.299999999999999988897769753748434595763683319091796875.
写入文件的数字“2.0000000298023224e-01”和“2.9999999999999999e-01”与表示值(如上所示)不同,但包含足够多的数字以唯一标识表示值。回读时,生成的 float
和 double
值应等于上面显示的值。
Also when I read the above YAML file using code belong [below?] I get altered values for the float and the double values.
“改变的值”是什么意思?根据问题,“下面的代码”的输出是“a, b, c=1,0.2,0.3”。虽然 0.2 和 0.3 与上面显示的表示值不同,但它们是我们期望在将这些值发送到 std::cout
时默认输出的值。最有可能发生的事情是,当从文件中读取“2.0000000298023224e-01”时,0.20000000298023223876953125 存储在 float b
中,并将其写入 std::cout
会产生“0.2”,正如预期的那样, double c
和 0.3 也类似。你认为这有什么不同?
设置:ubuntu 18.04.
上的 opencv 3.2我使用 YAML 文件保存了一个整数、一个浮点数和一个双精度值。 YAML文件中float和double的值与程序写入的值不同
#include <opencv2/core/core.hpp>
int main(int ac, char** av)
{
cv::FileStorage fs("file.yaml", cv::FileStorage::WRITE); // create FileStorage object
int a = 1;
float b = 0.2;
double c = 0.3;
fs << "a" << a;
fs << "b" << b;
fs << "c" << c;
fs.release(); // releasing the file.
return 0;
}
file.yaml 读取
%YAML:1.0
---
a: 1
b: 2.0000000298023224e-01
c: 2.9999999999999999e-01
此外,当我使用代码所属读取上述 YAML 文件时,我得到了 float 和 double 值的更改值。
#include <opencv2/core/core.hpp>
#include <iostream>
int main(int ac, char** av)
{
cv::FileStorage fs("file.yaml", cv::FileStorage::READ); // create FileStorage object
int a; float b; double c;
fs["a"] >> a;
fs["b"] >> b;
fs["c"] >> c;
std::cout << "a, b, c="<< a << ","<< b << ","<< c << std::endl;
fs.release(); // releasing the file.
return 0;
}
以上程序的输出和保存的YAML文件:
a, b, c=1,0.2,0.3
我的问题是如何在不更改值的情况下使用 opencv 读写 float 和 double 值from/to YAML 文件
The values of the float and the double in the YAML file is different from the values which are written by the program.
float
和 double
使用二进制基数的浮点格式表示。这些格式无法表示值 0.2 和 0.3。最接近的可表示值是 0.20000000298023223876953125 和 0.299999999999999988897769753748434595763683319091796875.
写入文件的数字“2.0000000298023224e-01”和“2.9999999999999999e-01”与表示值(如上所示)不同,但包含足够多的数字以唯一标识表示值。回读时,生成的 float
和 double
值应等于上面显示的值。
Also when I read the above YAML file using code belong [below?] I get altered values for the float and the double values.
“改变的值”是什么意思?根据问题,“下面的代码”的输出是“a, b, c=1,0.2,0.3”。虽然 0.2 和 0.3 与上面显示的表示值不同,但它们是我们期望在将这些值发送到 std::cout
时默认输出的值。最有可能发生的事情是,当从文件中读取“2.0000000298023224e-01”时,0.20000000298023223876953125 存储在 float b
中,并将其写入 std::cout
会产生“0.2”,正如预期的那样, double c
和 0.3 也类似。你认为这有什么不同?