Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

クローズ 初心者向け
#54 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp
領域
data

調査の方向性

boost/numeric/interval/arith2.hpp の nth_root から始め、同じファイル内で既に保護された型のために丸めオブジェクトを作成している fmod、cos、tan と比較してください。再現プログラムの出力を読んでください。呼び出し後に丸めモードが変更されており、(0, 1) の一部の入力では上限が sqrt(x) を下回っています。関数が丸めモードを復元し、境界が真の平方根を含むことを、再現プログラムと既存の区間テストで確認できれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C++
スター
31
フォーク
48
平均マージ
1日 13時間
マージ済み PR(30日)
6

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

boostorg/interval のほかの issue

boostorg/interval の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。