检查数字是否超过了特定的边界。

huangapple go评论82阅读模式
英文:

Check if number has passed certain boundary

问题

public void tempChange(Double oldTemp, Double newTemp) {
    if (hasNewTempPassedHalfOrWholeDegree(oldTemp, newTemp)) {
        // temperature is valid to process
    }
}

public boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
    double interval = newTemp - oldTemp;
    double decimalPart = interval - Math.floor(interval); // Get the decimal part of the interval
    
    if (decimalPart == 0 || decimalPart == 0.5) {
        return true;
    }
    
    return false;
}
英文:

I have a Thermostat that calls tempChange(oldTemp, newTemp) when the temperature changes by 0.1*C which is too frequent for my use case. I want to add an if() to filter out when the temperature has hits or passes a half or whole degree.

I am not sure how I would even begin with coding hasNewTempPassedHalfOrWholeDegree(oldTemp, newTemp).

public void tempChange(Double oldTemp, Double newTemp) {
    if (hasNewTempPassedHalfOrWholeDegree(oldTemp, newTemp)) {
        // temperature is valid to process
    }
}

A valid temperature change would be one where the new temperature has passed a half or whole degree (ie: 20.0, 20.5, 21.0, 21.5). In other words, as @Aioros as worded: I am trying to find the "interval between the two temperatures [that] includes a whole number or a whole number + 0.5.".

Example of temperature timeline and expected results:

{ oldTemp:19.7, newTemp:20.0 } // valid
{ oldTemp:20.0, newTemp:20.2 } // not valid
{ oldTemp:20.2, newTemp:20.6 } // valid
{ oldTemp:20.6, newTemp:21.7 } // valid
{ oldTemp:21.7, newTemp:21.6 } // not valid
{ oldTemp:21.6, newTemp:21.5 } // valid
{ oldTemp:21.5, newTemp:21.2 } // not valid
{ oldTemp:21.2, newTemp:20.2 } // valid
{ oldTemp:20.2, newTemp:20.1 } // not valid

答案1

得分: 1

以下是代码的中文翻译部分:

这里有一种方法来测试温度变化是否跨越了0或0.5的边界。

private boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
    int oldTempInt = Math.round(10d * oldTemp);
    int newTempInt = Math.round(10d * newTemp);
    int start = Math.min(oldTempInt,  newTempInt);
    int end = Math.max(oldTempInt,  newTempInt);
    
    for (int index = start; index <= end; index += 10) {
        if (index % 50 == 0) {
            return true;
        }
    }
    
    return false;
}
英文:

Here's one way to test if a temperature change crosses a .0 or a .5 boundary.

private boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
    int oldTempInt = Math.round(10d * oldTemp);
    int newTempInt = Math.round(10d * newTemp);
    int start = Math.min(oldTempInt,  newTempInt);
    int end = Math.max(oldTempInt,  newTempInt);
    
    for (int index = start; index &lt;= end; index += 10) {
    	if (index % 50 == 0) {
    		return true;
    	}
    }
    
    return false;
}

答案2

得分: 1

一个非常简单(不够优雅)的方法是将您的Double值转换为BigDecimal,并在范围内进行迭代,检查在除以0.5时是否有任何值留下余数为零,使用经典的for循环:

public static void main(String[] args){
    double[][] test = {
        {19.7, 20.0}, // 有效的
        {20.0, 20.2}, // 无效的
        {20.2, 20.6}, // 有效的
        {20.6, 21.7}, // 有效的
        {21.7, 21.6}, // 无效的
        {21.6, 21.5}, // 有效的
        {21.5, 21.2}, // 无效的
        {21.2, 20.2}, // 有效的
        {20.2, 20.1}, // 无效的
    };
    for(double[] d : test){
        boolean b = hasNewTempPassedHalfOrWholeDegree(d[0],d[1]);
        System.out.println(Arrays.toString(d) + (b ? "有效的" : "无效的"));
    }
}

static boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
    
    BigDecimal x = new BigDecimal(String.valueOf(oldTemp));
    BigDecimal y = new BigDecimal(String.valueOf(newTemp));
    
    BigDecimal d = new BigDecimal("0.1");
    BigDecimal h = new BigDecimal("0.5");
    
    if(x.compareTo(y) == 0){
        return false;
    }
    else if(x.compareTo(y) < 0){
        for (BigDecimal i = x.add(d); i.compareTo(y) <= 0; i = i.add(d)) {
            if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
                return true;
            }
        }
    }
    else {
        for (BigDecimal i = x.subtract(d); i.compareTo(y) >= 0; i = i.subtract(d)) {
            if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
                return true;
            }
        }
    }
    return false;
}
英文:

A very simple (not elegant) way could be to convert your Double values to BigDecimal and iterate over the range checking if any value in between leaves a remainder of zero when divided by 0.5 using a classic for loop:

public static void main(String[] args){
double[][] test = {
{19.7, 20.0}, // valid
{20.0, 20.2}, // not valid
{20.2, 20.6}, // valid
{20.6, 21.7}, // valid
{21.7, 21.6}, // not valid
{21.6, 21.5}, // valid
{21.5, 21.2}, // not valid
{21.2, 20.2}, // valid
{20.2, 20.1}, // not valid
};
for(double[] d : test){
boolean b = hasNewTempPassedHalfOrWholeDegree(d[0],d[1]);
System.out.println(Arrays.toString(d) + (b ? &quot;valid&quot; : &quot;not valid&quot;));
}
}
static boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
BigDecimal x = new BigDecimal(String.valueOf(oldTemp));
BigDecimal y = new BigDecimal(String.valueOf(newTemp));
BigDecimal d = new BigDecimal(&quot;0.1&quot;);
BigDecimal h = new BigDecimal(&quot;0.5&quot;);
if(x.compareTo(y) == 0){
return false;
}
else if(x.compareTo(y) &lt; 0){
for (BigDecimal i = x.add(d); i.compareTo(y) &lt;= 0; i = i.add(d)) {
if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
return true;
}
}
}
else {
for (BigDecimal i = x.subtract(d); i.compareTo(y) &gt;= 0; i = i.subtract(d)) {
if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
return true;
}
}
}
return false;
}

答案3

得分: -1

如果我理解你所寻找的内容正确的话,你可能会注意到两个数 ab 之间的区间在包含整数或整数与半个整数的情况下为:

当 FLOOR(2*b) > FLOOR(2*a) 时。

因此,你的条件看起来会是:

if (Math.floor(2*newTemp) != Math.floor(2*oldTemp)) {
    // 有效
} else {
    // 无效
}
英文:

If I understand correctly what you're looking for, you could notice that the interval between two numbers a and b includes an integer or a integer-and-a-half when:

FLOOR(2*b) &gt; FLOOR(2*a).

So your condition would look like:

if (Math.floor(2*newTemp) != Math.floor(2*oldTemp)) {
// valid
} else {
// not valid
}

huangapple
  • 本文由 发表于 2020年10月20日 23:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定