Skip to content
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

Fix the float sorting algorithm #3520

Merged
merged 1 commit into from Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/realm/column.hpp
Expand Up @@ -895,20 +895,23 @@ template <> struct IntTypeForSize<8> { using type = uint64_t; };
template <typename Float>
int compare_float(Float a_raw, Float b_raw)
{
// nans (and by extension nulls) are treated as being less than all non-nan values
bool a_nan = std::isnan(a_raw);
bool b_nan = std::isnan(b_raw);
if (a_nan != b_nan) {
return a_nan ? 1 : -1;
if (!a_nan && !b_nan) {
// Just compare as IEEE floats
return a_raw == b_raw ? 0 : a_raw < b_raw ? 1 : -1;
}

// Compare the values as ints, which gives correct results for IEEE floats
// and bypasses the usual behavior of nans not being comparable to each other
using IntType = typename _impl::IntTypeForSize<sizeof(Float)>::type;
IntType a = 0, b = 0;
memcpy(&a, &a_raw, sizeof(Float));
memcpy(&b, &b_raw, sizeof(Float));
return a == b ? 0 : a < b ? 1 : -1;
if (a_nan && b_nan) {
// Compare the nan values (including nulls) as unsigned
using IntType = typename _impl::IntTypeForSize<sizeof(Float)>::type;
IntType a = 0, b = 0;
memcpy(&a, &a_raw, sizeof(Float));
memcpy(&b, &b_raw, sizeof(Float));
return a == b ? 0 : a < b ? 1 : -1;
}
// One is nan, the other is not
// nans are treated as being less than all non-nan values
return a_nan ? 1 : -1;
}
} // namespace _impl

Expand Down
2 changes: 1 addition & 1 deletion test/test_table.cpp
Expand Up @@ -1877,7 +1877,7 @@ TEST_TYPES(Table_SortFloat, float, double)
for (size_t i = 300; i < 600; ++i) {
CHECK(sorted.get(i).is_null(col));
}
for (size_t i = 600; i + i < 900; ++i) {
for (size_t i = 600; i + 1 < 900; ++i) {
CHECK_GREATER(sorted.get(i + 1).get<TEST_TYPE>(col), sorted.get(i).get<TEST_TYPE>(col));
}
}
Expand Down