from_str_r moves exactly representable decimals by one ULP under TowardZero/TowardNegative (port of llvm#228928)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
Hướng nghiên cứu
Start by reading the from_str_r implementation and the existing floating point conversion tests; the issue gives reproducible decimal inputs and expected bit patterns for Single and Double under directed rounding modes. Add regression cases covering the reported exact and near-boundary conversions, then verify the resulting values and status flags across the affected formats and rounding modes. The issue says the fix likely depends on an upstream LLVM change, so check that defect’s status before estimating implementation work.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
This was identified by comparing floating point number conversion results across a few open-source implementations. The implementation of from_str_r incorrectly rounds some exactly representable numbers, however only in TowardZero/TowardNegative rounding modes which do not appear to be used yet. That makes this lower priority but opening for awareness - feel free to close if this is expected/intended.
Below is the full AI-generated summary:
from_str_r treats some exactly representable decimal strings as inexact. Under TowardZero and TowardNegative (and TowardPositive for negative inputs) it then returns the value one ULP below the exact one. The status is INEXACT in every mode, including NearestTiesToEven, where the value itself is right.
This is the LLVM APFloat defect reported as https://github.com/llvm/llvm-project/issues/228928, carried over by the port. rustc_apfloat 0.2.3 (+llvm-462a31f5a5ab) fails on different inputs than current LLVM does (the single-precision example in the LLVM issue converts correctly here), but the pattern is the same.
[dependencies]
rustc_apfloat = "=0.2.3"
use rustc_apfloat::ieee::{Double, Single};
use rustc_apfloat::{Float, Round};
fn check<F: Float>(label: &str, text: &str, round: Round, want: u128) -> bool {
let r = F::from_str_r(text, round).unwrap();
let got = r.value.to_bits();
let ok = got == want;
println!("{label:<24} {got:#018x} want {want:#018x} {:?}{}", r.status, if ok { "" } else { " <- WRONG" });
ok
}
fn main() {
// (2^23 + 1) * 2^-45: exactly the binary32 value 0x34800001
let single = "0.000000238418607523271930404007434844970703125";
// 2^11 + 2^-41: exactly the binary64 value 0x40A0000000000001
let double = "2048.00000000000045474735088646411895751953125";
let mut ok = true;
for (name, round) in [
("NearestTiesToEven", Round::NearestTiesToEven),
("TowardZero", Round::TowardZero),
("TowardNegative", Round::TowardNegative),
("TowardPositive", Round::TowardPositive),
] {
ok &= check::<Single>(&format!("Single {name}"), single, round, 0x34800001);
ok &= check::<Double>(&format!("Double {name}"), double, round, 0x40A0000000000001);
}
std::process::exit(if ok { 0 } else { 1 });
}
Output (rustc 1.99.0, Linux aarch64):
Single NearestTiesToEven 0x0000000034800001 want 0x0000000034800001 Status(INEXACT)
Double NearestTiesToEven 0x40a0000000000001 want 0x40a0000000000001 Status(INEXACT)
Single TowardZero 0x0000000034800000 want 0x0000000034800001 Status(INEXACT) <- WRONG
Double TowardZero 0x40a0000000000000 want 0x40a0000000000001 Status(INEXACT) <- WRONG
Single TowardNegative 0x0000000034800000 want 0x0000000034800001 Status(INEXACT) <- WRONG
Double TowardNegative 0x40a0000000000000 want 0x40a0000000000001 Status(INEXACT) <- WRONG
Single TowardPositive 0x0000000034800001 want 0x0000000034800001 Status(INEXACT)
Double TowardPositive 0x40a0000000000001 want 0x40a0000000000001 Status(INEXACT)
Scope: a sweep over decimal strings into Double, Single, Half, BFloat, Float8E5M2, Float8E4M3FN and Quad in all five modes found 1,268 exactly representable inputs moved one step in the rounding direction, in Double, Single, BFloat and Quad. It also found 84 inexact inputs within a tiny fraction of an ULP of a Quad value, rounded as if they were on the other side of it (e.g. 4.5326225836066175776471072707593525867497e-4965 under TowardZero gives 6 × 2⁻¹⁶⁴⁹⁴ instead of 7 × 2⁻¹⁶⁴⁹⁴). No other differences turned up in 151,415 conversions. Half and the FP8 types had none (LLVM 21.1.8 does fail for IEEEhalf).
Since rustc only folds literals with NearestTiesToEven, compiled code is not affected. This matters to direct users of the crate who pass a directed mode, and to anyone relying on the status flag. The fix most likely belongs in LLVM first; this issue is here so it gets ported when that happens.
Use the following links to see the expected conversion results:
- Ngôn ngữ chính
- Rust
- Star
- 23
- Fork
- 18
- Merge trung bình
- 2 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 2
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
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của rust-lang/rustc_apfloat
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
rust-lang/rustc_apfloat#44 · 3 bình luận ·
-
Overflow flag when result is larget finite valueCó thể đã có người làm @Garfield1002 đã nhận 51 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
rust-lang/rustc_apfloat#36 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
rust-lang/rustc_apfloat#20 ·
-
Support squareRootĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
rust-lang/rustc_apfloat#14 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
rust-lang/rustc_apfloat#10 · 7 bình luận ·
Tất cả issue của rust-lang/rustc_apfloat
Issue tương tự
-
documentation priority:p2 type:chore
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
facebookincubator/scrut#60 ·
-
dispatch:sol-high documentation
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 85/100
carrick-sh/carrick#768 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
correctness postgres tursopg
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
tursodatabase/turso#10442 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Incorrect "Share Decks" linkĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
ankitects/anki#5864 · 1 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày