A comparison of a decimal and an integer gives incorrect results

Open
#1,564 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
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

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_t and decimal128_t returns false when the exponent of the decimal is larger than the precision of the type. decimal32_t{5, 8} == 500000000 is 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_t and decimal128_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

  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.