diff --git a/lib/sympto/temperature.js b/lib/sympto/temperature.js index d738ba34e017bd530dc1f1381b5ccd79ed54fbc5..1e8c87ed52cb9de2b9f80fb23ece2f5d4d713f2d 100644 --- a/lib/sympto/temperature.js +++ b/lib/sympto/temperature.js @@ -51,7 +51,7 @@ function checkIfFirstHighMeasurement(temp, i, temperatureDays, ltl) { function getResultForRegularRule(nextDaysAfterPotentialFhm, ltl) { if (!nextDaysAfterPotentialFhm.every(day => day.temp > ltl)) return false const thirdDay = nextDaysAfterPotentialFhm[1] - if (rounded(thirdDay.temp - ltl, 0.1) < 0.2) return false + if (isLessThan0Point2(thirdDay.temp - ltl)) return false return { detected: true, rule: 0, @@ -77,7 +77,7 @@ function getResultForSecondExceptionRule(nextDaysAfterPotentialFhm, ltl) { if (nextDaysAfterPotentialFhm.length < 3) return false if (secondOrThirdTempIsAtOrBelowLtl(nextDaysAfterPotentialFhm, ltl)) { const fourthDay = nextDaysAfterPotentialFhm[2] - if (rounded(fourthDay.temp - ltl, 0.1) >= 0.2) { + if (isBiggerOrEqual0Point2(fourthDay.temp - ltl)) { return { detected: true, rule: 2, @@ -104,3 +104,19 @@ function rounded(val, step) { // we round the difference because of JS decimal weirdness return Math.round(val * inverted) / inverted } + + +// since we're dealing with floats, there is some imprecision in comparisons, +// so we add an error margin that is definitely much smaller than any possible +// actual difference between values +// see https://floating-point-gui.de/errors/comparison/ for background + +const errorMargin = 0.0001 + +function isLessThan0Point2(val) { + return val < (0.2 - errorMargin) +} + +function isBiggerOrEqual0Point2(val) { + return val >= (0.2 - errorMargin) +} \ No newline at end of file