A comparison of two decimal types can use the type with fewer digits
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 20/100
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di boostorg/decimal
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
I maintainer di solito rispondono entro 1 giorno
-
The decimal types have no operator<=> with GCC and Clang if no earlier header includes <compare>Forse già presa @ibmibmibm l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 25/100
I maintainer di solito rispondono entro 1 giorno
-
comparetotal gives incorrect results for NaN operandsForse già presa @ibmibmibm l’ha presa oggi. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 25/100
I maintainer di solito rispondono entro 1 giorno
-
A comparison of a decimal and an integer gives incorrect resultsForse già presa @ibmibmibm l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
I maintainer di solito rispondono entro 1 giorno
-
sinh, cosh, tanh, expm1 and exp2 give incorrect results, and 0 or infinity for some finite resultsForse già presa @ibmibmibm l’ha presa 7 giorni fa. Aperta
Difficoltà 5/5 Più di una settimana Idoneità per principianti 10/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di boostorg/decimal
Issue simili
-
agent:WSL code-review linux pri:medium refactoring
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
kind/enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 82/100
psi29a/d2decomped#691 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
CachyOS/packageinstaller#144 ·
I maintainer di solito rispondono entro 1 giorno
-
(S1 - Need confirmation)
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
CleverRaven/Cataclysm-DDA#89715 ·
I maintainer di solito rispondono entro 1 giorno