Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linearpart.h: Handle NaN NoData properly #264

Open
wants to merge 1 commit into
base: Develop
Choose a base branch
from

Conversation

HuidaeCho
Copy link

@HuidaeCho HuidaeCho commented May 11, 2024

This PR fixes handling of NoData that is NaN. Subtracting a NaN value from a non-NaN or NaN value yields NaN and comparing this result with MINEPS will always return false because comparisons on NaN do not produce a signal (Not-a-Number).

Testing code:

#include <math.h>
#include <cstdio>

#define MINEPS 1e-30

int main()
{
    bool isNoData;
    double noData = NAN;

    // this cell is NoData
    double noDataCell = NAN;
    // this cell is not NoData
    double dataCell = 123;

    //---------------------------------------------------------
    printf("## Before this PR\n");
    printf("* no data cell:\t");
    isNoData = abs(noDataCell - noData) < MINEPS;
    if (isNoData)
        // expected output
        printf("NoData\n");
    else
        // actual output
        printf("Not NoData (incorrect)\n");

    printf("* data cell:\t");
    isNoData = abs(dataCell - noData) < MINEPS;
    if (isNoData)
        printf("NoData\n");
    else
        // expected and actual output
        printf("Not NoData (correct)\n");
    printf("\n");

    //---------------------------------------------------------
    printf("## After this PR\n");
    printf("* no data cell:\t");
    isNoData = (isnan(noData) && isnan(noDataCell)) ||
               (!isnan(noData) && abs(noDataCell - noData) < MINEPS);
    if (isNoData)
        // expected and actual output
        printf("NoData (correct)\n");
    else
        printf("Not NoData\n");

    printf("* data cell:\t");
    isNoData = (isnan(noData) && isnan(dataCell)) ||
               (!isnan(noData) && abs(dataCell - noData) < MINEPS);
    if (isNoData)
        printf("NoData\n");
    else
        // expected and actual output
        printf("Not NoData (correct)\n");
}

Result:

$ c++ -o test test.cpp -Wall; ./test
## Before this PR
* no data cell: Not NoData (incorrect)
* data cell:    Not NoData (correct)

## After this PR
* no data cell: NoData (correct)
* data cell:    Not NoData (correct)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant