2.1.17.8.4 ocmath_compare_data


Description

Compare two vectors or matrices cell by cell in predefined tolerance.

Syntax

int ocmath_compare_data( int nSize, const double * pData1, const double * pData2, bool * bIsSame, double dTolerance = 1e-10, bool bAbsoluteError = true )

Parameters

nSize
[input] Size of vector or matrix
pData1
[input] Pointer to the first data array
pData2
[input] Pointer to the second data array
bIsSame
[output] Compared result:
true: The two data arrays are same cell by cell in predefined tolerance
false: The two data arrays have some different cell.
dTolerance
[input] Tolerance predefined
bAbsoluteError
[input] Using absolute error or relative error when compare

Return

Returns 0 on success and a non-zero error code on failure.

Examples

EX1

void ocmath_compare_data_ex1()
{
    vector v1 = {1.8999988888, 2.5556775766, 4.567788522, 5.144244553, 6.4133515166};
    vector v2 = {1.899998888801, 2.555677576601, 4.56778852201, 5.14424455301, 6.413351516601};
    bool bIsSame = false;
    double dTolerance = 1e-10;
    int nRet = ocmath_compare_data(v1.GetSize(), v1, v2, &bIsSame, dTolerance);
    if(nRet == OE_NOERROR)
    {
        if(bIsSame)
            printf("The two vectors are same in predefined tolerance: %.11f\n", dTolerance);
        else
            printf("The two vectors have different cell in predefined tolerance: %.11f\n", dTolerance);

    }
    
    dTolerance = 1e-11;
    nRet = ocmath_compare_data(v1.GetSize(), v1, v2, &bIsSame, dTolerance);
    if(nRet == OE_NOERROR)
    {
        if(bIsSame)
            printf("The two vectors are same in predefined tolerance: %.11f\n", dTolerance);
        else
            printf("The two vectors have different cell in predefined tolerance: %.11f\n", dTolerance);
    }
}

EX2

void ocmath_compare_data_ex2()
{
    matrix mat1 = {{1.8999988888, 2.5556775766, 4.567788522},
                   {5.144244553, 6.4133515166, 7.3155355556}};
    matrix mat2 = {{1.899998888801, 2.555677576601, 4.56778852201}, 
                   {5.14424455301, 6.413351516601, 7.315535555601}};
    int nRows = mat1.GetNumRows();
    int nCols = mat2.GetNumCols();
    bool bIsSame = false;
    double dTolerance = 1e-10;
    int nRet = ocmath_compare_data(nRows*nCols, mat1, mat2, &bIsSame, dTolerance);
    if(nRet == OE_NOERROR)
    {
        if(bIsSame)
            printf("The two matrices are same in predefined tolerance: %.11f\n", dTolerance);
        else
            printf("The two matrices have different cell in predefined tolerance: %.11f\n", dTolerance);
    }
    
    dTolerance = 1e-11;
    nRet = ocmath_compare_data(nRows*nCols, mat1, mat2, &bIsSame, dTolerance);
    if(nRet == OE_NOERROR)
    {
        if(bIsSame)
            printf("The two matrices are same in predefined tolerance: %.11f\n", dTolerance);
        else
            printf("The two matrices have different cell in predefined tolerance: %.11f\n", dTolerance);
    }
}

Remark

See Also

vec_is_equal

Header to Include

origin.h

Reference