Skip to content

fix: preserve sign when rounding the smallest subnormal - #15667

Merged
kgryte merged 1 commit into
stdlib-js:developfrom
manit2004:fix/math-round2-negative-subnormal-sign
Sep 30, 2026
Merged

kgryte merged 1 commit into
stdlib-js:developfrom
manit2004:fix/math-round2-negative-subnormal-sign

Conversation

@manit2004

Copy link
Copy Markdown
Contributor

Resolves #15666

Description

What is the purpose of this pull request?

This pull request:

  • Fixes round2(-Number.MIN_VALUE) returning the positive smallest subnormal instead of the negative one. The function normalizes x to its absolute value up front and tracks the original sign separately (sign), then multiplies back by sign on every return path except one; the early-return for "this is exactly the smallest subnormal, can't round it any further" returned the bare (already-absolute-valued) x instead of sign * x. Adds a test covering the negative case, since the existing suite only checked the positive smallest subnormal (where sign is +1, so the bug was invisible).

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • None

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

Repro:

var round2 = require( '@stdlib/math/base/special/round2' );

var v = round2( -Number.MIN_VALUE );
// returns 5e-324, should return -5e-324

Verified before/after: 39/40 passing (1 new failing test) on the unfixed code, 40/40 after the fix.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".


@stdlib-js/reviewers

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@manit2004
manit2004 requested a review from a team September 29, 2026 19:12
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. labels Sep 29, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/round2 $\\color{green}218/218$
$\\color{green}+100.00\\%$
$\\color{green}19/19$
$\\color{green}+100.00\\%$
$\\color{green}2/2$
$\\color{green}+100.00\\%$
$\\color{green}218/218$
$\\color{green}+100.00\\%$

The above coverage report was generated for the changes in this PR.

@kgryte kgryte 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.

LGTM. Thanks for fixing, @manit2004!

@kgryte kgryte changed the title fix: preserve sign when rounding the smallest subnormal in round2 fix: preserve sign when rounding the smallest subnormal Sep 30, 2026
@kgryte
kgryte merged commit 8c521ed into stdlib-js:develop Sep 30, 2026
64 checks passed
kgryte added a commit that referenced this pull request Sep 30, 2026
Ref: #15667

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown_pkg_readmes
    status: na
  - task: lint_markdown_docs
    status: na
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: passed
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@stdlib-bot stdlib-bot removed the Needs Review A pull request which needs code review. label Sep 30, 2026
@manit2004
manit2004 deleted the fix/math-round2-negative-subnormal-sign branch September 30, 2026 07:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Math Issue or pull request specific to math functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

round2 flips the sign when rounding the smallest subnormal (negative input)

3 participants