Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

nth_root does not restore the rounding mode and can return an upper bound below the true root

Aperta Adatta ai principianti
#54 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
data

Direzione di ricerca

Inizia in boost/numeric/interval/arith2.hpp da nth_root e confrontalo con fmod, cos e tan nello stesso file, che creano già un oggetto di arrotondamento per il tipo protetto. Leggi l'output del reproducer: la modalità di arrotondamento viene modificata dopo la chiamata, e il limite superiore è inferiore a sqrt(x) per alcuni input in (0, 1). Il lavoro è completo quando la funzione ripristina la modalità di arrotondamento e i limiti contengono la radice vera, verificato con il reproducer e con i test esistenti sugli intervalli.

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

Descrizione

nth_root (in boost/numeric/interval/arith2.hpp) works in the unprotected interval type but never creates a rounding object for the protected type, unlike fmod, cos and tan. This has two effects when it is called from code running in round-to-nearest.

  1. The rounding mode is left changed when the function returns.
  2. For some inputs in (0, 1) the upper bound is below the true root, so the result does not contain it.
Reproducer

Godbolt (GCC trunk, Boost 1.92.0, -std=c++03 -frounding-math)

It calls nth_root(interval<double>(0.80166573794976181), 2) from round-to-nearest, reads the rounding mode before and after, and computes upper^2 - x exactly.

Output (the same on develop at 3957843; nth_root is identical in Boost 1.92.0 and develop):

rounding mode: to nearest before, changed after
nth_root([0.80166573794976181], 2) = [0.89535788260882665, 0.89535788260882687]
upper^2 - x = -1.31841e-17

upper^2 - x is negative, so the upper bound is below sqrt(x).

Tested with

x86-64 Linux, glibc 2.39, double, always with -frounding-math:

  • GCC 13.3: -O0, -O2, -O3, C++03 and C++17, -O2 -mfma, -O2 -mfpmath=387, -m32 -O2
  • Clang 18.1.3: -O0, -O2, -O2 -mfma

All give the result above. It also happens at -O0, so it does not depend on optimization.

How often

I ran 1.6 million pairs (x, k), with x uniform in (0, 1) and k = 2…9, and checked each bound exactly with MPFR. 1,321 upper bounds were below the true root. No lower bound was
wrong. The rounding mode was changed after every call. For x in (1, 1000) no bound was wrong, but the mode was still changed after every call.

No compiler flag changes these counts. Tried on top of -frounding-math:

  • GCC 13.3: -O0, -O2, -ftrapping-math, -fsignaling-nans, -ffloat-store, -fno-inline, -ffp-contract=off, -mfpmath=387, #pragma STDC FENV_ACCESS ON
  • Clang 18.1.3: -O2, -ftrapping-math, -ffp-exception-behavior=strict, -ffp-model=strict (at -O0 and -O2), #pragma STDC FENV_ACCESS ON

With the rounding mode set to upward before the call, or with the caller holding a rounding object (interval<double>::traits_type::rounding rnd;) around the call, there were
no wrong bounds.

Lingua principale
C++
Stelle
31
Fork
48
Merge medio
2g 5m
PR unite (30g)
4

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

Tutte le issue di boostorg/interval

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.