Skip to content

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

Description

@manit2004

Description

round2(-Number.MIN_VALUE) returns the positive smallest subnormal instead of the negative one. Found this while digging through math/base/special.

The function does the usual if (x < 0) { x = -x; sign = -1; } thing up front so the rest of the body only deals with magnitudes, then multiplies back by sign on every return path; except one. There's an early-return for "this is exactly the smallest subnormal, can't round it any further":

if ( p === MIN_EXP_SUBNORMAL ) {
    return x;   // <-- x is already the abs value at this point, sign is dropped
}

Since x was already reassigned to its absolute value two lines earlier, this just returns the magnitude, not the original signed input. Every other branch in the function (sign * PINF, sign * HUGE, sign * y1, sign * y2) gets this right; this one spot doesn't.

Existing test suite doesn't catch it; it only checks round2(SMALLEST_SUBNORMAL) (positive), never the negative case, so sign being +1 there masked the bug.

Related Issues

None

Questions

No.

Demo

No response

Reproduction

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

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

Expected Results:

-5e-324 (sign preserved, matching every other return path in the function).

Actual Results:

5e-324 — sign silently flipped.

Expected Results

`-5e-324` (sign preserved, matching every other return path in the function).

Actual Results

`5e-324` — sign silently flipped.

Version

0.4.1

Environments

Node.js

Browser Version

No response

Node.js / npm Version

Node v22.22.2, npm 10.9.7

Platform

No response

Checklist

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions