cpp_bin_float fma rounds the product before the addition instead of fusing

Aperta
#777 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@NAThompson ci sta già lavorando.

Dal 11/10/2026.

  • #778 di @NAThompson — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C++
Stelle
268
Fork
130
Merge medio
1g 15h
PR unite (30g)
2

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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di boostorg/multiprecision

Tutte le issue di boostorg/multiprecision

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.