Skip to content
Snippets Groups Projects
Commit d86603e8 authored by Julia Friesel's avatar Julia Friesel
Browse files

Merge branch 'fix-float-imprecision' into 'master'

Fix float imprecision

See merge request bloodyhealth/drip!138
parents 4a29ef6c 79ff978a
No related branches found
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment