Jon
Jon

Reputation: 8531

Check if number has passed certain boundary

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

Upvotes: 0

Views: 278

Answers (3)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51465

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 <= end; index += 10) {
        if (index % 50 == 0) {
            return true;
        }
    }
    
    return false;
}

Upvotes: 1

Eritrean
Eritrean

Reputation: 16498

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 ? "valid" : "not valid"));
    }
}

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;
}

Upvotes: 1

Aioros
Aioros

Reputation: 4383

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) > FLOOR(2*a).

So your condition would look like:

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

Upvotes: -1

Related Questions