Skip to content

fix: clamp negative forced exponent instead of returning NaN#304

Open
chatman-media wants to merge 1 commit into
avoidwork:masterfrom
chatman-media:fix/negative-exponent-clamp
Open

fix: clamp negative forced exponent instead of returning NaN#304
chatman-media wants to merge 1 commit into
avoidwork:masterfrom
chatman-media:fix/negative-exponent-clamp

Conversation

@chatman-media

Copy link
Copy Markdown

calculateExponent clamps e > 8 down to 8, but nothing clamps the other side — any forced exponent below -1 (the documented auto sentinel) sails past both checks and ends up indexing DECIMAL_POWERS/BINARY_POWERS out of bounds. filesize(1024, {exponent: -3}) currently returns "NaN undefined", and with output: "object" it silently drops symbol/unit from the result ({value: NaN, exponent: -3, symbol: undefined, unit: undefined}), which breaks the documented shape.

Fix just clamps e < 0 to 0 in the same spot, mirroring the existing e > 8 clamp. Added tests for the string, object, and array output shapes with a couple of out-of-range values (-2, -3, -100).

calculateExponent only clamps e > 8, but any exponent below -1 (the
documented auto sentinel) skips that too and indexes DECIMAL_POWERS /
BINARY_POWERS out of bounds, so e.g. filesize(1024, {exponent: -3})
returns "NaN undefined" instead of a sane string, and object output
silently drops symbol/unit. Clamp e < 0 to 0 the same way e > 8 is
already clamped to 8.
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.

1 participant