Skip to content

Fix leading zeros in format_to_fixed for small numbers - #67

Merged
HalidOdat merged 2 commits into
boa-dev:mainfrom
devrelu:fix/to-fixed-leading-zero
Nov 22, 2026
Merged

HalidOdat merged 2 commits into
boa-dev:mainfrom
devrelu:fix/to-fixed-leading-zero

Conversation

@devrelu

@devrelu devrelu commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

This Pull Request fixes #66.

format64_to_fixed previously started the fractional block loop at i = 0. For blocks below MIN_BLOCK_2[idx], the index p fell into the previous row of POW10_SPLIT_2, causing digits computed from the wrong multiplier to be written where leading zeros belong:

ryu_js::Buffer::new().format_to_fixed(1e-17_f64, 40)
// before: 0.8750000000000000100000000000000007154242
// after:  0.0000000000000000100000000000000007154242  (matches V8 / SpiderMonkey)

This PR restores the zero-fill logic from the original C d2fixed implementation, which skips those blocks:

} else if (i < MIN_BLOCK_2[idx]) {
  i = MIN_BLOCK_2[idx];
  memset(result + index, '0', 9 * i);
  index += 9 * i;
}

Changes

  • Write 9 * min_block zeros up front and start the fractional loop at min_block.
  • Update the min_exponent_boundry tests, which previously asserted the buggy output. With exponent bits 0x294, the value is ~5.3e-110, so toFixed(100) must be all zeros. The actual last all-zero exponents at 100 fractional digits are 0x2B1 (zero mantissa) and 0x2B0 (full mantissa), verified against V8 across all exponents.
  • Add a regression test.

Testing

  • cargo test and cargo test --features small pass.
  • Verified that the new and updated tests fail without the fix.
  • A differential test against V8's Number.prototype.toFixed across ~300k random (f64, digits) pairs—plus a sweep of 1.2345 * 10^e for e in -330..25 across all digit counts 0..=100 produced zero mismatches.

Copilot AI balanced review requested due to automatic review settings November 21, 2026 12:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@devrelu devrelu changed the title Fix leading zeros in format_to_fixed for small numbers Fix leading zeros in format_to_fixed for small numbers Nov 21, 2026
@devrelu

devrelu commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

FYI, the Clippy failure is pre-existing on main and unrelated to this PR.

Rust 1.100 deprecates std::f32 / std::f64 module constants imported in tests/ and benches/ (failing CI due to -Dwarnings). Removing those imports and using ::consts::E fixes it while keeping MSRV 1.71 green.

Happy to send a separate PR for this if needed!

@jedel1043

Copy link
Copy Markdown
Member

@devrelu please do so! It sounds like a pretty straightforward change, and I can review it ASAP

@devrelu

devrelu commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Opened #68 for the Clippy fix. Once it's merged, I'll rebase this PR so CI goes green.

@devrelu
devrelu force-pushed the fix/to-fixed-leading-zero branch from 46e09be to d09adeb Compare November 22, 2026 11:13

@HalidOdat HalidOdat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great! Thank you for the contribution! 🙌

@devrelu
devrelu force-pushed the fix/to-fixed-leading-zero branch from d09adeb to 9576e21 Compare November 22, 2026 12:24
@HalidOdat

Copy link
Copy Markdown
Member

I'll do a patch release after this is merged 😄

@HalidOdat
HalidOdat added this pull request to the merge queue Nov 22, 2026
Merged via the queue into boa-dev:main with commit 24f1951 Nov 22, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

format_to_fixed emits wrong leading fractional digits for small numbers

4 participants