Skip to content

Add an error margin when comparing floats. #129721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions muted-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -538,15 +538,6 @@ tests:
- class: org.elasticsearch.xpack.esql.qa.multi_node.EsqlSpecIT
method: test {knn-function.KnnSearchWithKOption SYNC}
issue: https://github.com/elastic/elasticsearch/issues/129512
- class: org.elasticsearch.xpack.logsdb.qa.StandardVersusStandardReindexedIntoLogsDbChallengeRestIT
method: testMatchAllQuery
issue: https://github.com/elastic/elasticsearch/issues/129527
- class: org.elasticsearch.xpack.logsdb.qa.BulkChallengeRestIT
method: testMatchAllQuery
issue: https://github.com/elastic/elasticsearch/issues/129528
- class: org.elasticsearch.xpack.logsdb.qa.StoredSourceLogsDbVersusReindexedLogsDbChallengeRestIT
method: testMatchAllQuery
issue: https://github.com/elastic/elasticsearch/issues/129529
- class: org.elasticsearch.index.engine.ThreadPoolMergeExecutorServiceTests
method: testIORateIsAdjustedForAllRunningMergeTasks
issue: https://github.com/elastic/elasticsearch/issues/129531
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.function.Supplier;

import static org.elasticsearch.datageneration.matchers.Messages.formatErrorMessage;
import static org.elasticsearch.datageneration.matchers.Messages.prettyPrintCollections;
Expand Down Expand Up @@ -62,31 +61,46 @@ public MatchResult match(List<Object> actual, List<Object> expected) {

var normalizedActual = normalizeDoubles(actual);
var normalizedExpected = normalizeDoubles(expected);
Supplier<MatchResult> noMatchSupplier = () -> MatchResult.noMatch(
formatErrorMessage(
actualMappings,
actualSettings,
expectedMappings,
expectedSettings,
"Values of dynamically mapped field containing double values don't match after normalization, normalized "
+ prettyPrintCollections(normalizedActual, normalizedExpected)
)
);

return normalizedActual.equals(normalizedExpected)
? MatchResult.match()
: MatchResult.noMatch(
formatErrorMessage(
actualMappings,
actualSettings,
expectedMappings,
expectedSettings,
"Values of dynamically mapped field containing double values don't match after normalization, normalized "
+ prettyPrintCollections(normalizedActual, normalizedExpected)
)
);
if (normalizedActual.size() != normalizedExpected.size()) {
return noMatchSupplier.get();
}

for (int i = 0; i < normalizedActual.size(); i++) {
if (floatsEquals(normalizedActual.get(i), normalizedExpected.get(i))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if floatsEquals(...) returns true then there is no match? Maybe rename floatsEquals(...) to floatsNoEquals(...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh boy, my bad. Actually I prefer to keep it floatsEquals and fix the code because its more intuitive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also good with me 👍

return noMatchSupplier.get();
}
}

return MatchResult.match();
}

return matchWithGenericMatcher(actual, expected);
}

private static Set<Float> normalizeDoubles(List<Object> values) {
private static List<Float> normalizeDoubles(List<Object> values) {
if (values == null) {
return Set.of();
return List.of();
}

Function<Object, Float> toFloat = (o) -> o instanceof Number n ? n.floatValue() : Float.parseFloat((String) o);
return values.stream().filter(Objects::nonNull).map(toFloat).collect(Collectors.toSet());

// We skip nulls because they trip the pretty print collections.
return values.stream().filter(Objects::nonNull).map(toFloat).toList();
}

private static boolean floatsEquals(Float actual, Float expected) {
return Math.abs(actual - expected) > 1e-8;
}

private MatchResult matchWithGenericMatcher(List<Object> actualValues, List<Object> expectedValues) {
Expand Down