A comparison of two decimal types can use the type with fewer digits

Aperta
#1,563 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à
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
20/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

The bug is in the mixed-type comparison operators of the decimal library, where both operands are converted to the wider type and the right operand's type wins when sizes match. Start with the reproduction program in the issue, which compares decimal_fast32_t, decimal_fast64_t and IEEE decimal types, and check which rows give wrong results. Done means all seven rows match the expected false, true, false, true output and equality is exact without rounding. Linked pull request #1567 already targets this, so review that work before starting anything new.

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

Descrizione

Problem

A comparison of two different decimal types converts both values to the type with the larger size. decimal_fast32_t has the same size as decimal64_t, and decimal_fast64_t has the same size as decimal128_t. For two types of the same size, the operator uses the type of the right operand. This type can have fewer digits, and the conversion rounds the other value.

  • Two values that are not equal compare as equal, and the result changes with the order of the operands.
  • A fast type has no subnormal values. A subnormal value of the IEEE type with the same precision converts to zero, and compares equal to zero.

Reproduction

#include <boost/decimal.hpp>
#include <iostream>

using namespace boost::decimal;
using namespace boost::decimal::literals;

template <typename T1, typename T2>
void check(const char* text, const T1 small, const T2 large)
{
    std::cout << std::boolalpha << "| " << text << " | " << (small == large) << " | " << (small < large)
              << " | " << (large == small) << " | " << (large > small) << " |\n";
}

int main()
{
    check("`18496.97_DFF`, `18496.9723_DD`", 18496.97_DFF, 18496.9723_DD);
    check("`1_DFF`, `1.0000001_DD`", 1_DFF, 1.0000001_DD);
    check("`-1.0000001_DD`, `-1_DFF`", -1.0000001_DD, -1_DFF);
    check("`1_DDF`, `1.0000000000000001_DL`", 1_DDF, 1.0000000000000001_DL);
    check("`0_DFF`, `1e-100_DF`", 0_DFF, 1e-100_DF);
    check("`0_DDF`, `1e-390_DD`", 0_DDF, 1e-390_DD);
    check("`0_DLF`, `1e-6170_DL`", 0_DLF, 1e-6170_DL);
}

Result on develop (215bbf025), GCC 16.2.1, x86-64 Linux, C++20. In each row, the first value is less than the second value.

small, large small == large small < large large == small large > small
18496.97_DFF, 18496.9723_DD false true true true
1_DFF, 1.0000001_DD false true true true
-1.0000001_DD, -1_DFF true false false false
1_DDF, 1.0000000000000001_DL false true true true
0_DFF, 1e-100_DF true true true true
0_DDF, 1e-390_DD true true true true
0_DLF, 1e-6170_DL true true true true

In the last three rows, the values are equal and also less.

Expected result

Each row gives false, true, false, true. A comparison is exact: it does not round an operand.

Proposed fix

Compare in the type with more digits, or in the IEEE type when the precision is the same. For equality, compare the coefficients and the exponents, as the less-than comparison does, without a conversion.

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.