What is the best way to validate if a value with decimals is in a defined range

85 Views Asked by At

I have a file with thresholds defined and these threshold are use to help to take decision.

The values looks like this:

"thresholds":[
    { "min": 0.0, "max": 0.25, "text": "VERY UNLIKELY" },
    { "min": 0.26, "max": 0.50, "text": "UNLIKELY" }
    { "min": 0.51, "max": 0.75, "text": "LIKELY" }
    { "min": 0.76, "max": 1.0, "text": "VERY LIKELY" }
]

The condition:

for (Threshold threshold : thresholds) {
    if ((threshold.getMin() <= predictionValue) &&
        (predictionValue <= threshold.getMax())) {
            return threshold.getText();
    }
}

If the value to check is something like 0.2500000001, it fall somewhere between 0.25 and 0.26. So I ask, what is the optimal way to determine if a value is in a certain range without having empty gap?

Should I add a parameter for precision and apply this precision on the min & max values? I don't want to have to configure the file with values like 0.259999999.

1

There are 1 best solutions below

1
On

You end up with this grey zone because you want to declare a boundary with 2 values. This does not work. I will give you an idea of how it works:

What you should do:

"thresholds":[
    { "max": 0.25, "text": "VERY UNLIKELY" },
    { "max": 0.50, "text": "UNLIKELY" }
    { "max": 0.75, "text": "LIKELY" }
    { "max": 1.0, "text": "VERY LIKELY" }
]

And the condition:

for (Threshold threshold : thresholds) {
    if (predictionValue < threshold.getMax()) {
            return threshold.getText();
    }
}

As you can see, one value suffices to define a boundary.