[BUG]: `number/float16/base/to-word` mis-rounds subnormals at the 11-bit shift boundary

Đang mở
#16,510 1 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

@harsh-thakkar7 đang làm issue này rồi.

Từ ngày 5/11/2026.

  • #16511 của @harsh-thakkar7 — đang mở

Đá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
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, node.js
Lĩnh vực
backend

Hướng nghiên cứu

The issue identifies lib/main.js and the subnormal rounding branch as the starting point. Check the conversion against native Float16Array around the 11-bit shift boundary, including positive and negative values and the exact halfway value. Done means the reported mismatches are fixed and focused regression coverage confirms the expected half-precision words; pull request #16511 is already open.

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

Mô tả

Description

@stdlib/number/float16/base/to-word returns zero for float32 values just above 2^-25 that should round to the smallest positive half-precision subnormal. Native Float16Array rounds these to 0x0001.

var toWord = require( '@stdlib/number/float16/base/to-word' );

toWord( 2.98038125038147e-8 );  // returns 0x0000, expected 0x0001
toWord( -2.98038125038147e-8 ); // returns 0x8000, expected 0x8001

Comparing against new Float16Array over 20,000 values in [2^-25, 2^-24) shows 10,015 mismatches; the values that should round to 0x0001 are returned as zero. The exact halfway value 2^-25 correctly ties to zero in both implementations.

Root Cause

In lib/main.js, the subnormal rounding path branches on:

if ( shift < 11 ) {

When shift === 11, the code falls into the else branch, which extracts the round bit from the original 23-bit mantissa at the wrong position. With shift = 11 the 11-bit mantissa is shifted out entirely, and the round bit is its lowest bit (bit 10), which the first branch already handles correctly. Values requiring shift > 11 return zero earlier via the f16Exponent < -10 check, so the else branch is only reachable for shift === 11.

Changing the condition to shift <= 11 fixes all mismatches in a 50,000-value sweep.

Environment
  • Version: develop (7cf56423a)
  • Node.js with native Float16Array support
  • The bug is platform-independent and affects the JavaScript path only; the C path returns the bits of an already-encoded stdlib_float16_t.
Checklist
  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
Ngôn ngữ chính
JavaScript
Star
6k
Fork
1.3k
Merge trung bình
1 ngày 21 giờ
Pull request đã merge (30 ngày)
224

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.

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 stdlib-js/stdlib

Tất cả issue của stdlib-js/stdlib

Issue tương tự

Thêm issue về JavaScript

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.