comparetotal gives incorrect results for NaN operands

Aperta
#1,565 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@ibmibmibm ci sta già lavorando.

Dal 22/11/2026.

  • #1567 di @ibmibmibm — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
data

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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)
Lingua principale
C++
Stelle
67
Fork
16
Merge medio
1g 15h
PR unite (30g)
12

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di boostorg/decimal

Tutte le issue di boostorg/decimal

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.