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

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

Check if number has passed certain boundary

问题

  1. public void tempChange(Double oldTemp, Double newTemp) {
  2. if (hasNewTempPassedHalfOrWholeDegree(oldTemp, newTemp)) {
  3. // temperature is valid to process
  4. }
  5. }
  6. public boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
  7. double interval = newTemp - oldTemp;
  8. double decimalPart = interval - Math.floor(interval); // Get the decimal part of the interval
  9. if (decimalPart == 0 || decimalPart == 0.5) {
  10. return true;
  11. }
  12. return false;
  13. }
英文:

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).

  1. public void tempChange(Double oldTemp, Double newTemp) {
  2. if (hasNewTempPassedHalfOrWholeDegree(oldTemp, newTemp)) {
  3. // temperature is valid to process
  4. }
  5. }

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:

  1. { oldTemp:19.7, newTemp:20.0 } // valid
  2. { oldTemp:20.0, newTemp:20.2 } // not valid
  3. { oldTemp:20.2, newTemp:20.6 } // valid
  4. { oldTemp:20.6, newTemp:21.7 } // valid
  5. { oldTemp:21.7, newTemp:21.6 } // not valid
  6. { oldTemp:21.6, newTemp:21.5 } // valid
  7. { oldTemp:21.5, newTemp:21.2 } // not valid
  8. { oldTemp:21.2, newTemp:20.2 } // valid
  9. { oldTemp:20.2, newTemp:20.1 } // not valid

答案1

得分: 1

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

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

  1. private boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
  2. int oldTempInt = Math.round(10d * oldTemp);
  3. int newTempInt = Math.round(10d * newTemp);
  4. int start = Math.min(oldTempInt, newTempInt);
  5. int end = Math.max(oldTempInt, newTempInt);
  6. for (int index = start; index <= end; index += 10) {
  7. if (index % 50 == 0) {
  8. return true;
  9. }
  10. }
  11. return false;
  12. }
英文:

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

  1. private boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
  2. int oldTempInt = Math.round(10d * oldTemp);
  3. int newTempInt = Math.round(10d * newTemp);
  4. int start = Math.min(oldTempInt, newTempInt);
  5. int end = Math.max(oldTempInt, newTempInt);
  6. for (int index = start; index &lt;= end; index += 10) {
  7. if (index % 50 == 0) {
  8. return true;
  9. }
  10. }
  11. return false;
  12. }

答案2

得分: 1

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

  1. public static void main(String[] args){
  2. double[][] test = {
  3. {19.7, 20.0}, // 有效的
  4. {20.0, 20.2}, // 无效的
  5. {20.2, 20.6}, // 有效的
  6. {20.6, 21.7}, // 有效的
  7. {21.7, 21.6}, // 无效的
  8. {21.6, 21.5}, // 有效的
  9. {21.5, 21.2}, // 无效的
  10. {21.2, 20.2}, // 有效的
  11. {20.2, 20.1}, // 无效的
  12. };
  13. for(double[] d : test){
  14. boolean b = hasNewTempPassedHalfOrWholeDegree(d[0],d[1]);
  15. System.out.println(Arrays.toString(d) + (b ? "有效的" : "无效的"));
  16. }
  17. }
  18. static boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
  19. BigDecimal x = new BigDecimal(String.valueOf(oldTemp));
  20. BigDecimal y = new BigDecimal(String.valueOf(newTemp));
  21. BigDecimal d = new BigDecimal("0.1");
  22. BigDecimal h = new BigDecimal("0.5");
  23. if(x.compareTo(y) == 0){
  24. return false;
  25. }
  26. else if(x.compareTo(y) < 0){
  27. for (BigDecimal i = x.add(d); i.compareTo(y) <= 0; i = i.add(d)) {
  28. if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
  29. return true;
  30. }
  31. }
  32. }
  33. else {
  34. for (BigDecimal i = x.subtract(d); i.compareTo(y) >= 0; i = i.subtract(d)) {
  35. if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
  36. return true;
  37. }
  38. }
  39. }
  40. return false;
  41. }
英文:

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:

  1. public static void main(String[] args){
  2. double[][] test = {
  3. {19.7, 20.0}, // valid
  4. {20.0, 20.2}, // not valid
  5. {20.2, 20.6}, // valid
  6. {20.6, 21.7}, // valid
  7. {21.7, 21.6}, // not valid
  8. {21.6, 21.5}, // valid
  9. {21.5, 21.2}, // not valid
  10. {21.2, 20.2}, // valid
  11. {20.2, 20.1}, // not valid
  12. };
  13. for(double[] d : test){
  14. boolean b = hasNewTempPassedHalfOrWholeDegree(d[0],d[1]);
  15. System.out.println(Arrays.toString(d) + (b ? &quot;valid&quot; : &quot;not valid&quot;));
  16. }
  17. }
  18. static boolean hasNewTempPassedHalfOrWholeDegree(Double oldTemp, Double newTemp) {
  19. BigDecimal x = new BigDecimal(String.valueOf(oldTemp));
  20. BigDecimal y = new BigDecimal(String.valueOf(newTemp));
  21. BigDecimal d = new BigDecimal(&quot;0.1&quot;);
  22. BigDecimal h = new BigDecimal(&quot;0.5&quot;);
  23. if(x.compareTo(y) == 0){
  24. return false;
  25. }
  26. else if(x.compareTo(y) &lt; 0){
  27. for (BigDecimal i = x.add(d); i.compareTo(y) &lt;= 0; i = i.add(d)) {
  28. if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
  29. return true;
  30. }
  31. }
  32. }
  33. else {
  34. for (BigDecimal i = x.subtract(d); i.compareTo(y) &gt;= 0; i = i.subtract(d)) {
  35. if (i.remainder(h).compareTo(BigDecimal.ZERO) == 0) {
  36. return true;
  37. }
  38. }
  39. }
  40. return false;
  41. }

答案3

得分: -1

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

  1. FLOOR(2*b) > FLOOR(2*a) 时。

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

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

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:

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

So your condition would look like:

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

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:

确定