Merge branch 'fix-float-imprecision' into 'master'
Fix float imprecision See merge request bloodyhealth/drip!138
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user