cpp_bin_float fma rounds the product before the addition instead of fusing

Open
#777 0 comments 0 reactions 0 assignees View on GitHub

@NAThompson is already working on this.

Since Oct 11, 2026.

  • #778 by @NAThompson — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
backend

Research direction

Read the cpp_bin_float backend and detail/default_ops.hpp, where the issue identifies the fallback multiply and add dispatch. Start by tracing how eval_multiply_add is selected for cpp_bin_float; verify the fused result against the provided cpp_bin_float_quad reproduction, including the expected eps * eps. Done when fma preserves the exact fused result for the reported case.

Written by the indexing model from the issue text.

Description

The documentation says fma behaves like its C++11 standard-library counterpart, i.e. a * b + c is rounded once. For cpp_bin_float it is rounded twice: cpp_bin_float has no eval_multiply_add / eval_multiply_subtract of its own, so the unqualified backend dispatch resolves to the generic fallback in detail/default_ops.hpp, which does eval_multiply(t, u, v); eval_add(t, x);. The product is rounded to the working precision before c is added.

Other backends that I can see use the same fallback (cpp_dec_float, gmp_float, cpp_double_fp, mpfi_float); mpfr_float has its own overload (mpfr_fma) and float128 does too, unless BOOST_MP_USE_QUAD is defined. I have only verified the cpp_bin_float behaviour below. cpp_dec_float has guard digits and truncating arithmetic, so its behaviour is a separate question.

Repro:

#include <boost/multiprecision/cpp_bin_float.hpp>
#include <iostream>
#include <limits>
using namespace boost::multiprecision;
int main() {
   using T = cpp_bin_float_quad;
   T eps = std::numeric_limits<T>::epsilon();   // 2^-112
   T x = 1 + eps, z = -(1 + 2 * eps);
   std::cout << fma(x, x, z) << "\n";   // prints 0
   std::cout << eps * eps << "\n";      // 3.70921e-68, the expected result
}

The exact product is x*x = 1 + 2 eps + eps^2, so fma(x, x, z) must be eps^2 = 2^-224, which is exactly representable. The product is first rounded to 1 + 2 eps, which then cancels with z. The same happens for cpp_bin_float_50.

This breaks error-free transformations built on fma (TwoProduct: p = a * b; e = fma(a, b, -p) returns e == 0 for ordinary finite inputs). Boost.Math (boostorg/math#1547, detail/two_product.hpp) has to work around it for these types.

Dominant language
C++
Stars
268
Forks
130
Avg merge
1d 15h
Merged PRs (30d)
2

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/multiprecision

All issues in boostorg/multiprecision

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.