cpp_bin_float fma rounds the product before the addition instead of fusing
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
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
- 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/multiprecision
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
boostorg/multiprecision#779 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
boostorg/multiprecision#775 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
boostorg/multiprecision#768 · 4 comments ·
-
Eigen NumTraits specialisation doesn't define max_digits10()Possibly taken @trestoncuzzort claimed this 28 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
boostorg/multiprecision#744 ·
-
`cpp_double_fp_backend` optimized single-limb operationsMay be free again @ckormanyos claimed this 462 days ago, and no pull request is open. Openenhancement optimization
boostorg/multiprecision#728 · 1 assignee ·
All issues in boostorg/multiprecision
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