0

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.

Jonathan Anctil
  • 1,025
  • 3
  • 20
  • 44
  • 1
    You could define the thing as a half-open interval where the upper limit value is not included. Example: min: 0 - max 0.26 <- and then just check if your value is >= min and < max – OH GOD SPIDERS Dec 07 '17 at 16:03
  • @OHGODSPIDERS Yes good idea! – Jonathan Anctil Dec 07 '17 at 16:05
  • If you want precision with decimals you can use BigDecimal class, make sure you use the BigDecimal(String val) constructor, because if you pass double/float directly it will still be imprecise: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html – whatamidoingwithmylife Dec 07 '17 at 16:06

1 Answers1

1

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.

PKlumpp
  • 4,913
  • 8
  • 36
  • 64