A comparison of a decimal and an integer gives incorrect results
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 22/100
Research direction
The reproduction program compares decimal32_t, decimal64_t, decimal128_t and the decimal_fast types with integers using ==, < and >, so start with the comparison operators for those types. Build the reproduction and check its six rows against the expected table. Pull request #1567 already targets this, so read it before starting. Done means every row matches the expected result in both rounding modes.
Written by the indexing model from the issue text.
Description
Problem
A comparison of a decimal type and an integer has two defects.
- The equality of
decimal32_t,decimal64_tanddecimal128_treturns false when the exponent of the decimal is larger than the precision of the type.decimal32_t{5, 8} == 500000000is false. For these values,operator>also returns true. - Some comparisons first round the integer to the precision of the decimal type. These are the order of
decimal64_tanddecimal128_t, and the equality and the order of the fast types. An integer with more digits than the type then compares equal to a near decimal value. The result also changes with the rounding mode.
Reproduction
#include <boost/decimal.hpp>
#include <cstdint>
#include <iostream>
using namespace boost::decimal;
template <typename T, typename Integer>
void check(const char* text, const T x, const Integer n)
{
std::cout << std::boolalpha << "| " << text << " | " << (x == n) << " | " << (x < n) << " | " << (x > n);
fesetround(rounding_mode::fe_dec_downward);
std::cout << " | " << (x == n) << " | " << (x < n) << " | " << (x > n) << " |\n";
fesetround(rounding_mode::fe_dec_to_nearest);
}
int main()
{
check("`decimal32_t{5, 8}`, `500000000`", decimal32_t{5, 8}, 500000000);
check("`decimal64_t{5, 17}`, `500000000000000000`", decimal64_t{5, 17}, INT64_C(500000000000000000));
check("`decimal64_t{1, 16}`, `10000000000000001`", decimal64_t{1, 16}, INT64_C(10000000000000001));
check("`decimal_fast32_t{1, 7}`, `10000001`", decimal_fast32_t{1, 7}, 10000001);
check("`decimal_fast32_t{1000001, 1}`, `10000015`", decimal_fast32_t{1000001, 1}, 10000015);
check("`decimal_fast64_t{1, 16}`, `10000000000000001`", decimal_fast64_t{1, 16}, INT64_C(10000000000000001));
}
Result on develop (215bbf025), GCC 16.2.1, x86-64 Linux, C++20. Each row shows x == n, x < n and x > n in the default rounding mode. Then it shows the same three results in the fe_dec_downward mode.
x, n |
== |
< |
> |
== downward |
< downward |
> downward |
|---|---|---|---|---|---|---|
decimal32_t{5, 8}, 500000000 |
false | false | true | false | false | true |
decimal64_t{5, 17}, 500000000000000000 |
false | false | true | false | false | true |
decimal64_t{1, 16}, 10000000000000001 |
false | false | true | false | false | true |
decimal_fast32_t{1, 7}, 10000001 |
true | false | false | true | false | false |
decimal_fast32_t{1000001, 1}, 10000015 |
false | true | false | true | false | false |
decimal_fast64_t{1, 16}, 10000000000000001 |
true | false | false | true | false | false |
Expected result
A comparison is exact, and it does not change with the rounding mode. In the first two rows the values are equal. In the other four rows x is less than n.
| Rows | == |
< |
> |
|---|---|---|---|
| 1 and 2 | true | false | false |
| 3 to 6 | false | true | false |
Proposed fix
Compare the value of the decimal with the magnitude of the integer without a rounding step. If the scaled value fits in 64 bits, compare one 64-bit or 128-bit product. If not, the positions of the first digits give the order.
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
-
comparetotal gives incorrect results for NaN operandsPossibly taken @ibmibmibm claimed this 1 day ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 25/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