comparetotal gives incorrect results for NaN operands

Open
#1,565 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@ibmibmibm is already working on this.

Since Nov 22, 2026.

  • #1567 by @ibmibmibm — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
cpp
Domain
data

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) and comparetotal(qNaN, -sNaN) return true for all six types.
  • The predicate is reflexive. For two NaNs with the same sign, kind and payload, comparetotal returns 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/decimal

All issues in boostorg/decimal

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.