You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(math): preserve signed BN values and formatting - #3982
Fixed negative BN values losing their sign during copying and arithmetic, and corrected signed amount formatting.
Summary
Copy BN magnitudes without aliasing their words and restore the sign. Preserve signed operands/results across arithmetic wrappers, clone, two's-complement conversion and extended arithmetic. Format the sign separately from decimal digits and return whole numbers when units=0. Includes a patch changeset.
Validation: four new regression tests fail on main; all 37 math tests pass with the fix. Math and dependency builds, math TypeScript check, ESLint and Prettier pass. Tested on Node 25.9.0 (outside the declared supported range).
Checklist
Changes covered by regression tests
Changes documented in a changeset
No API signature changes
AI assistance: implementation, review and tests prepared with Codex; no human review is claimed.
Medium Risk
Touches core BN construction and amount formatting used across the stack; fixes incorrect signs on negatives but changes behavior for code that relied on the buggy paths.
Overview
Fixes a sign-loss bug in @fuel-ts/mathBN when values are copied or wrapped after arithmetic. Constructing from another BN now decodes via the existing array path but forces decimal/big-endian interpretation and re-applies negativity with ineg() when the source was negative, so clones/copies (bn(other), clone(), arithmetic helpers) keep the correct sign instead of treating magnitudes as positive.
Arithmetic wrappers (caller, sqr, neg, abs, two's-complement helpers, mulTo, egcd, divmod) now build results with new BN(superResult) (or decimal string for some low-level paths) rather than toArray(), preserving signs through the constructor.
Display:formatUnits and format now handle units === 0 as plain integers, format negative amounts with an explicit sign (including small fractional values), and treat -0 like 0 in precision trimming. A patch changeset and four regression tests cover negatives, isolated copies, extended arithmetic, and formatting.
Reviewed by Cursor Bugbot for commit 348ff3b. Bugbot is set up for automated code reviews on this repo. Configure here.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3981.
Release notes
Fixed negative BN values losing their sign during copying and arithmetic, and corrected signed amount formatting.
Summary
Copy BN magnitudes without aliasing their words and restore the sign. Preserve signed operands/results across arithmetic wrappers, clone, two's-complement conversion and extended arithmetic. Format the sign separately from decimal digits and return whole numbers when units=0. Includes a patch changeset.
Validation: four new regression tests fail on main; all 37 math tests pass with the fix. Math and dependency builds, math TypeScript check, ESLint and Prettier pass. Tested on Node 25.9.0 (outside the declared supported range).
Checklist
AI assistance: implementation, review and tests prepared with Codex; no human review is claimed.