Skip to content

Commit

Permalink
Merge pull request #3520 from realm/je/fix-float-sort
Browse files Browse the repository at this point in the history
Fix the float sorting algorithm
  • Loading branch information
jedelbo committed Dec 16, 2019
2 parents c5e6b81 + 4148562 commit 44151d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
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

0 comments on commit 44151d2

Please sign in to comment.