comparetotal gives incorrect results for NaN operands
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
Research direction
The predicate comparetotal is shared by the six decimal types, and test_total_ordering.cpp is the test that should gain the missing cases. Build the reproduction program from the issue and check the four NaN rows for each type. Pull request #1567 already targets this fix, so review it or its tests before starting new work. Done means all six types return false, false, true, true for the four cases, with the reflexivity case covered by a test.
Written by the indexing model from the issue text.
Description
Problem
comparetotal(x, y) is the totalOrder predicate of IEEE 754-2019 5.10. It is true when x is before y in the total order, or when the two have the same representation. For NaN operands it has three defects.
- A positive NaN is after each number, and a negative NaN is before each number.
comparetotal(qNaN, 1)returns true for all six types.comparetotal(1, -qNaN)returns true for the fast types. - A positive NaN is after a negative NaN of the other kind, but
comparetotal(sNaN, -qNaN)andcomparetotal(qNaN, -sNaN)return true for all six types. - The predicate is reflexive. For two NaNs with the same sign, kind and payload,
comparetotalreturns false.
Reproduction
#include <boost/decimal.hpp>
#include <iostream>
#include <limits>
using namespace boost::decimal;
template <typename T>
void check(const char* name)
{
const T one {1};
const T qnan {std::numeric_limits<T>::quiet_NaN()};
const T snan {std::numeric_limits<T>::signaling_NaN()};
std::cout << std::boolalpha << "| `" << name << "` | " << comparetotal(one, -qnan) << " | " << comparetotal(qnan, one)
<< " | " << comparetotal(qnan, qnan) << " | " << comparetotal(-snan, -snan) << " |\n";
}
int main()
{
check<decimal32_t>("decimal32_t");
check<decimal64_t>("decimal64_t");
check<decimal128_t>("decimal128_t");
check<decimal_fast32_t>("decimal_fast32_t");
check<decimal_fast64_t>("decimal_fast64_t");
check<decimal_fast128_t>("decimal_fast128_t");
}
Result on develop (215bbf025), GCC 16.2.1, x86-64 Linux, C++20.
| Type | (1, -qNaN) |
(qNaN, 1) |
(qNaN, qNaN) |
(-sNaN, -sNaN) |
|---|---|---|---|---|
decimal32_t |
false | true | false | false |
decimal64_t |
false | true | false | false |
decimal128_t |
false | true | false | false |
decimal_fast32_t |
true | true | false | false |
decimal_fast64_t |
true | true | false | false |
decimal_fast128_t |
true | true | false | false |
test_total_ordering.cpp expects false for a NaN compared with itself, and it does not check a NaN on the left side of a number.
Expected result
Each row gives false, false, true, true. Python decimal gives the same four results with x.compare_total(y) <= 0.
Proposed fix
Write the NaN part again in the order of 5.10 d.1 to d.3: first the sign, then signaling before quiet, then the payload. Negative NaNs have the opposite order. Change the documentation of the payload rule to "less than or equal".
Reference
- #1490 (Relook Fundamental Optimization of Comparisons)
- Dominant language
- C++
- Stars
- 67
- Forks
- 16
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 12
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/decimal
-
Difficulty 4/5 3-5 days Newbie friendliness 22/100
Maintainers usually reply within 1 day
-
The decimal types have no operator<=> with GCC and Clang if no earlier header includes <compare>Possibly taken @ibmibmibm claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
A comparison of a decimal and an integer gives incorrect resultsPossibly taken @ibmibmibm claimed this 1 day ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 22/100
Maintainers usually reply within 1 day
-
A comparison of two decimal types can use the type with fewer digitsPossibly taken @ibmibmibm claimed this 1 day ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 20/100
Maintainers usually reply within 1 day
-
sinh, cosh, tanh, expm1 and exp2 give incorrect results, and 0 or infinity for some finite resultsPossibly taken @ibmibmibm claimed this 7 days ago. Open
Difficulty 5/5 Over a week Newbie friendliness 10/100
Maintainers usually reply within 1 day
All issues in boostorg/decimal
Similar issues
-
agent:WSL code-review linux pri:medium refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
kind/enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
psi29a/d2decomped#691 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
CachyOS/packageinstaller#144 ·
Maintainers usually reply within 1 day
-
(S1 - Need confirmation)
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
CleverRaven/Cataclysm-DDA#89715 ·
Maintainers usually reply within 1 day