interval::operator/ returns the whole line for [-1,0] / [0,1] (strict is_neg(xu) in div_positive / div_negative)

Đang mở Phù hợp với người mới
#78 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp
Lĩnh vực
data

Hướng nghiên cứu

Lỗi nằm trong include/boost/numeric/interval/detail/division.hpp, ở div_positive (khoảng dòng 78) và div_negative (khoảng dòng 113), nơi phép kiểm tra is_neg(xu) định tuyến sai một tử số có cận trên đúng bằng không. Hãy bắt đầu bằng cách biên dịch đoạn tái hiện trong issue và kiểm tra rằng [-1,0] / [0,1] trả về toàn bộ đường thẳng. Hoàn thành khi trường hợp đó trả về (-inf, 0], trường hợp đối xứng dấu trả về [0, +inf), và một trường hợp hồi quy được thêm vào test/mul.cpp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Version: Boost 1.83.0 (headers only, g++ 13.3.0, -frounding-math). The relevant code in include/boost/numeric/interval/detail/division.hpp is unchanged on develop (a701c9dd22) and master.

What happens. The documentation of / says: "If the denominator contains zero (but not only zero), the result will be the smallest interval containing the set of division results; so one of its bound will be infinite, but it may not be the whole interval." For a numerator whose upper bound is exactly zero and a denominator with one zero endpoint, operator/ (and division_part1) return [-inf, +inf]:

[-1,0] / [0,1]  = [-inf, inf]   expected (-inf, 0]
[-1,0] / [-1,0] = [-inf, inf]   expected [0, +inf)
[0,1]  / [0,1]  = [0, inf]      (correct)
[0,1]  / [-1,0] = [-inf, 0]     (correct)

The result still encloses all quotients, so this is a loss of tightness relative to the documentation rather than an unsound result, and it breaks the sign symmetry between the third and first lines.

Reproducer (Boost headers only; g++ -std=c++17 -frounding-math repro.cpp && ./a.out):

#include <boost/numeric/interval.hpp>
#include <cstdio>
using I = boost::numeric::interval<double>;
static void show(const char* name, I x, I y) {
  I r = x / y;
  std::printf("%-16s = [%g, %g]\n", name, r.lower(), r.upper());
}
int main() {
  show("[-1,0] / [0,1]",  I(-1, 0), I(0, 1));   // expected (-inf, 0]
  show("[-1,0] / [-1,0]", I(-1, 0), I(-1, 0));  // expected [0, +inf)
  show("[0,1] / [0,1]",   I(0, 1),  I(0, 1));   // [0, +inf), correct
  show("[0,1] / [-1,0]",  I(0, 1),  I(-1, 0));  // (-inf, 0], correct
}

Cause. div_positive(const interval&, const T& yu) (line 78) and div_negative(const interval&, const T& yl) (line 113) branch on is_neg(xu), i.e. xu < 0. When xu == 0 and xl < 0 this falls through to the is_neg(xl) branch, which is meant for a numerator containing zero in its interior, and returns the whole line. (The other two is_neg(xu) tests in the file, in div_non_zero and div_zero_part1, give correct results for xu == 0.)

Suggested fix. Replace is_neg(xu) with !is_pos(xu) in those two functions; x == [0,0] is already handled earlier by is_zero. I tested the patched header on 35,654 generated cases (boundary grid plus random, including signed zeros, subnormals, max and infinities) against an exact rational oracle: the 311 mismatching cases become correct, the other 35,343 are unchanged, and all 341 div vectors of the IEEE 1788 test suite (ITF1788) pass. To my reading the division tests in test/mul.cpp do not cover a numerator with an upper endpoint of 0. I'm happy to open a PR with the change and a regression test if that helps.

Ngôn ngữ chính
C++
Star
30
Fork
49
Merge trung bình
2 ngày 15 giờ
Pull request đã merge (30 ngày)
7

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của boostorg/interval

Tất cả issue của boostorg/interval

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.