Repository navigation
Fix copysign() dropping the magnitude when y is zero - #214
Merged
Merged
Conversation
copysign() in src/library/numpy.ts was `absolute(x).mul(sign(y))`. sign() returns 0 for a zero input, so any zero in y zeroed the whole result: np.copysign([5, -5], [0, 0]) returned [0, 0] where numpy returns [5, 5]. sign() also cannot see the sign bit, so -0 and negative NaN in y were treated as positive. Read the sign from signbit() instead, which sits a few lines above in the same file and bitcasts to get the sign bit for every value class. Checked all 64 combinations of x in [1, -1, 5, -5, 0, -0, 3.5, -3.5] and y in [0, -0, 2, -2, inf, -inf, 1e-45, -1e-45] against numpy 2.4.6; cpu and wasm now agree with numpy on all of them. The existing "copysign with zero" test asserted the old [0, 0]; it now asserts [5, 5], with two more tests for signed zero and for infinite and NaN sign arguments.
MaxFreedomPollard
force-pushed
the
fix-copysign-zero
branch
from
September 6, 2026 09:20
0c50976 to
7323cd0
Compare
ekzhang
approved these changes
Sep 6, 2026
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
np.copysign(x, y)returns zero wheneveryis zero. It is implemented insrc/library/numpy.tsasabsolute(x).mul(sign(y)), andsign(0)is0, so the magnitude ofxis multiplied away.sign()also cannot see the sign bit, so-0inycame back positive.signbit()sits a few lines abovecopysign()in the same file and bitcasts to read the sign bit for every value class, including-0and NaN. Reading the sign from it instead of fromsign()fixes all of these cases and needs no new machinery.Changes
src/library/numpy.ts:copysign()is nowwhere(signbit(y), -abs(x), abs(x)).test/numpy.test.ts: the existing "copysign with zero" test asserted the old[0, 0]result, so it now asserts numpy's[5, 5]. Added a test for signed zero inyand one for infinite and NaNy.Verification
Node 22 on macOS. I ran vitest with
--no-browser, which exercises the cpu and wasm devices.pnpm test run test/numpy.test.ts -t "copysign" --no-browsergives 4 failed and 4 passed before the source change (the two affected tests on both devices) and 8 passed after.pnpm test run test/numpy.test.ts test/refcount.test.ts test/dtype.test.ts test/random.test.ts src/library/numpy/einsum.test.ts --no-browsergives 1327 passed with the change and 1323 on unmodified main, with the same 4 failures on both. Those 4 areReferenceError: Float16Array is not defined, which is Node 22 on this machine rather than anything in the diff; CI is on Node 26.pnpm check,pnpm lint --max-warnings 0andpnpm exec prettier --checkare all clean.xin[1, -1, 5, -5, 0, -0, 3.5, -3.5]withyin[0, -0, 2, -2, inf, -inf, 1e-45, -1e-45]against numpy 2.4.6. Every pair matches on both cpu and wasm, andsignbit(np.copysign(np.array([0]), np.array([-1])))istrue, so the signed zero result keeps its sign.