Math.Round(x) rounds imprecisely under Fable 5.26.0

Đã đóng
#5,251 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Một pull request liên quan đã được merge.

  • #5254 của @MangelMaxime — đã merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
62/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ệ
javascript
Lĩnh vực
compilers

Hướng nghiên cứu

The bug is in the round helper of fable-library-js (Util.round), which uses toFixed(8) and an epsilon tie check; the reproduction is the Math.Round loop over 2.500000001 and 3.4999999999 in the issue. Start by reading that helper and checking whether the same logic is mirrored in other Fable targets. Done when both values round to 3 and Math.Round(x, digits) matches .NET behaviour, with a regression test added.

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

Mô tả

Math.Round(double) calls a value a tie when its fraction is within 1e-8 of 0.5, so values near a half round the wrong way. That way 2.500000001 rounds down to 2 and 3.4999999999 rounds up to 4 under Fable 5.26.0, instead of 3 for both cases.

To reproduce this issue

open System
for x in [ 2.500000001; 3.4999999999 ] do
    printfn "%s" (string (Math.Round x))

fable-library-js’s Util.round reads the value through toFixed(8) and calls it a tie when the fraction is within 1e-8 of 0.5

const n = +(digits ? value * m : value).toFixed(8);
...
const r = (f > 0.5 - e && f < 0.5 + e) ? ((i % 2 === 0) ? i : i + 1) : Math.round(n);

Math.Round(double) works on the exact value and a tie is a fraction of exactly one half.

Suggested fix that would be exact.

const t = Math.trunc(x), f = Math.abs(x - t);
return f > 0.5 ? t + Math.sign(x) : f < 0.5 ? t : (t % 2 === 0 ? t : t + Math.sign(x));

For Math.Round(x, digits) the same applies after scaling by 10^digits, as .NET does.

Ngôn ngữ chính
F#
Star
3.2k
Fork
335
Merge trung bình
19 giờ 39 phút
Pull request đã merge (30 ngày)
158

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

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

  • Không có Dockerfile hay tệp Docker Compose
  • Không có mẫu pull request
  • Không có hướng dẫn đóng góp

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 fable-compiler/Fable

Tất cả issue của fable-compiler/Fable

Issue tương tự

Thêm issue về Compilers

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.