Skip to content

Commit

Permalink
Minor bugfix, minor improvement (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Aug 4, 2023
1 parent df57e03 commit bce92d8
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.apache.logging.log4j.ThreadContext;
import reactor.core.publisher.Flux;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Optional;
import java.util.SortedSet;
import java.util.TreeSet;
Expand Down Expand Up @@ -74,11 +76,22 @@ private Double computeResolution(Double value) {

// Stored as a set and not as a value to possibly improve the algorithm in the future,
// including detecting noisy analog signals (might want to return NaN instead of null for that case)
diffs.add(Math.abs(value - lastKnown));

var diff = round(Math.abs(value - lastKnown));

if (Double.compare(diff, 0.0) == 0) {
// Sorry, no cigar
return resolution;
}

diffs.add(diff);

if (diffs.size() > 50) {
logger.warn("Noisy signal? Trimming the tail: {}", diffs);
((TreeSet<?>) diffs).descendingIterator().remove();

var i = ((TreeSet<?>) diffs).descendingIterator();
i.next();
i.remove();
}
}

Expand All @@ -94,6 +107,13 @@ private Double computeResolution(Double value) {
}
}

private double round(Double d) {
return
BigDecimal.valueOf(d)
.setScale(3, RoundingMode.HALF_UP)
.doubleValue();
}

private Double computeResolution(SortedSet<Double> source) {

if (source.size() < 2) {
Expand Down

0 comments on commit bce92d8

Please sign in to comment.