Skip to content

fix(builtins): correct toPrecision output for subnormal floats#5226

Open
HiteshShonak wants to merge 3 commits intoboa-dev:mainfrom
HiteshShonak:fix/number-toprecision-subnormal
Open

fix(builtins): correct toPrecision output for subnormal floats#5226
HiteshShonak wants to merge 3 commits intoboa-dev:mainfrom
HiteshShonak:fix/number-toprecision-subnormal

Conversation

@HiteshShonak
Copy link
Contributor

This Pull Request fixes/closes #5225.

It changes the following:

  • When format!("{this_num:.100}") collapses a subnormal float to all zeros, fall back to scientific notation formatting to correctly extract the mantissa and exponent.
  • Add regression tests for Number.MIN_VALUE.toPrecision(1) and toPrecision(2).

Testing:

cargo test -p boa_engine number -- --nocapture

Spec reference: https://tc39.es/ecma262/#sec-number.prototype.toprecision

@HiteshShonak HiteshShonak requested a review from a team as a code owner March 22, 2026 16:46
Copilot AI review requested due to automatic review settings March 22, 2026 16:46
@github-actions github-actions bot added C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics Waiting On Review Waiting on reviews from the maintainers labels Mar 22, 2026
@github-actions github-actions bot added this to the v1.0.0 milestone Mar 22, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Number.prototype.toPrecision for extremely small (including subnormal) f64 values where fixed-point formatting at 100 fractional digits collapses to all zeros, by falling back to scientific formatting to derive the correct mantissa/exponent.

Changes:

  • Detects the “all zeros” fixed-point formatting case and derives digits/exponent from scientific notation instead.
  • Adds regression tests covering Number.MIN_VALUE.toPrecision(1) and (2) to prevent reintroducing the bug.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
core/engine/src/builtins/number/mod.rs Adds scientific-notation fallback when fixed formatting loses all significant digits, fixing exponent/mantissa extraction.
core/engine/src/builtins/number/tests.rs Adds regression coverage for Number.MIN_VALUE precision formatting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link

github-actions bot commented Mar 22, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 50,732 50,733 +1
Ignored 1,426 1,426 0
Failed 805 804 -1
Panics 0 0 0
Conformance 95.79% 95.79% +0.00%
Fixed tests (1):
test/staging/sm/Number/toPrecision-values.js (previously Failed)

Tested main commit: 2437b6704c4315f6ec8f0766bf229fba23e0e61e
Tested PR commit: 149da84d4b5fe96d737ea061fcb698c2fe42ba28
Compare commits: 2437b67...149da84

@codecov
Copy link

codecov bot commented Mar 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.71%. Comparing base (6ddc2b4) to head (149da84).
⚠️ Report is 919 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5226       +/-   ##
===========================================
+ Coverage   47.24%   59.71%   +12.47%     
===========================================
  Files         476      589      +113     
  Lines       46892    63415    +16523     
===========================================
+ Hits        22154    37869    +15715     
- Misses      24738    25546      +808     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Builtins PRs and Issues related to builtins/intrinsics C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Number.prototype.toPrecision returns wrong output for subnormal floats like Number.MIN_VALUE

2 participants