将它们添加到C 中的JSONCPP模块中的数组之后

Bad values after adding them to array in JsonCpp module in C++

本文关键字:模块 数组 之后 JSONCPP 中的 添加      更新时间:2023-10-16

在将值添加到JSONCPP数组之后,我有问题。我的代码接下来是:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "jsoncpp/dist/jsoncpp.cpp"
#include "jsoncpp/dist/json/json.h"
#include "jsoncpp/dist/json/json-forwards.h"
using namespace std;
void printArray(const char *measure, float **arr, int rows, int cols)
{
    int i,j;
    cout << endl;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            cout << arr[i][j] << "t";
        }
        cout << endl;
    }
    cout << endl;

    if (measure == "ECG")
    {
        int i,j;
        Json::Value ECG;
        Json::Value time(Json::arrayValue);
        Json::Value values(Json::arrayValue);
        for (i = 0; i < rows; i++)
        {
            for (j = 0; j < 1; j++)
            {
                time.append(Json::Value(arr[i][j]));
            }
        }
        for (i = 0; i < rows; i++)
        {
            for (j = 1; j < 2; j++)
            {
                values.append(Json::Value(arr[i][j]));
            }
        }
        cout << endl << time << endl;
        cout << endl << values << endl;
    }
}

int main()
{
    int i,j;
    int rows, cols;
    const char *measur;
    rows = 10;
    cols = 2;
    float **array = new float *[rows];
    for(int i = 0; i < rows; i++)
    {
        array[i] = new float [cols];
        for(int j = 0; j < cols; j++)
        {
            array[i][j]=(i+j)/10.0;
        }
    }
    measur = "ECG";
    printArray(measur, array, rows, cols);    
}

编译和运行后,我有以下内容:

0   0.1 
0.1 0.2 
0.2 0.3 
0.3 0.4 
0.4 0.5 
0.5 0.6 
0.6 0.7 
0.7 0.8 
0.8 0.9 
0.9 1   

[
    0.0,
    0.10000000149011612,
    0.20000000298023224,
    0.30000001192092896,
    0.40000000596046448,
    0.5,
    0.60000002384185791,
    0.69999998807907104,
    0.80000001192092896,
    0.89999997615814209
]
[
    0.10000000149011612,
    0.20000000298023224,
    0.30000001192092896,
    0.40000000596046448,
    0.5,
    0.60000002384185791,
    0.69999998807907104,
    0.80000001192092896,
    0.89999997615814209,
    1.0
]

您可以看到,在将值添加到JSON数组之前,一切都可以,但是之后,有一个不同的值。现在,它是一个数据示例,但它将是一个库,它将从ECG,脉动氧计,气流等获取数据。来自RAPBerryPi的模块,因此来自"值"数组的数据将大约为0.14453453,0.23546456等。我必须防止。这样的情况。我的价值必须尽可能准确。

谢谢。

浮动和双打不存储10个数字。他们使用的基本2有时会非常接近,但是/-值很小。Wiki在浮点上:https://en.wikipedia.org/wiki/floating-point_arithmetic

稍后,您通常可以通过将浮子转换回带有sprintf或类似的字符串来恢复相同的值(出于显示目的(。看起来您正在阅读1个十进制数字,因此:

 sprintf(stringout, "%.1f", floatin);

比较浮子和加倍时,请始终牢记这一"不相等"的事实。而不是问"是x == y",您应该问"是x == y /-某些公差值,例如0.0001"

好吧,我将浮动类型更改为两倍,现在我的精度要好得多。对我来说很好。现在,请告诉我,如何仅使用8个小数位置值发送到"值"表?我不想在值= [0.12121212、0.21212121]之类的值阵列数据中拥有,但我只想像这样的值= [0.1212,0.2121]。我想做的是在循环时切断"值"表中的多余信息。我的意思是,如何在将数据附加到"值"表中设置数字限制。

以这样的精度打印输入值:

cout << std::setw(11) << std::setprecision(9) << arr[i][j] << "t";

其他一切都很好。

编辑:这是没有预防的,因为您不能准确地代表二进制的0.9。在由B1*1/2 B2*1/4 B3*1/4 ...组成的二进制分数中而且,如果您从传感器中读取数据,那么它已经是0.1212121212 ....如果要显示到用户TRUCE的数据,则可以使用与上述相同的技巧。但是,如果您仍然只想使用10-15个小数点存储空间,则需要使用固定点数学,但我敢打赌。