gpt4 book ai didi

java - 验证带小数的值是否在定义范围内的最佳方法是什么

转载 作者:行者123 更新时间:2023-12-02 11:54:25 25 4
gpt4 key购买 nike

我有一个定义了阈值的文件,这些阈值用于帮助做出决策。

这些值如下所示:

"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" }
]

条件:

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

如果要检查的值类似于 0.2500000001,则它介于 0.25 和 0.26 之间。所以我问,确定一个值是否在某个范围内且没有空间隙的最佳方法是什么?

我应该添加精度参数并将该精度应用于最小值和最大值吗?我不想用 0.259999999 这样的值配置文件。

最佳答案

您最终会得到这个灰色区域,因为您想要声明具有 2 个值的边界。这是行不通的。我将向您介绍它是如何工作的:

你应该做什么:

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

条件:

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

正如您所见,一个值足以定义边界。

关于java - 验证带小数的值是否在定义范围内的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47698963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com