Skip to content

Correctly handle unsigned integers in constructor#40

Merged
anj1 merged 1 commit intoanj1:masterfrom
TZdyrski:patch-1
Aug 30, 2025
Merged

Correctly handle unsigned integers in constructor#40
anj1 merged 1 commit intoanj1:masterfrom
TZdyrski:patch-1

Conversation

@TZdyrski
Copy link
Contributor

@TZdyrski TZdyrski commented Aug 29, 2025

Currently, the AlgebraicNumber constructor gives surprising behavior when using unsigned integers. For example, AlgebraicNumber(UInt8(3)) != AlgebraicNumber(Int(3)).

This occurs because the constructor AlgebraicNumber(x) uses -x when calculating the coefficients, which causes UInt8(3) = 0x03 to be wrap around to 0xfd. Thus, the coefficients of Int(3) are [-3,1] (correct), but UInt(3) gives [253,1].

Note: while the internal representations and equality test differ, AlgebraicNumber(UInt(3)) - AlgebraicNumber(Int(3)) == 0, so testing equality by subtracting is a (slower) workaround.

This fix promotes the input of the AlgebraicNumber constructor to BigInt to avoid this issue (simply using signed(x) would not work as signed doesn't check for overflow).

@TZdyrski TZdyrski changed the title Correctly handle unsighed integers in constructor Correctly handle unsigned integers in constructor Aug 29, 2025
@anj1 anj1 merged commit b623480 into anj1:master Aug 30, 2025
3 checks passed
@coveralls
Copy link

Coverage Status

coverage: 92.149% (-0.4%) from 92.593%
when pulling ad32ce5 on TZdyrski:patch-1
into c8c4677 on anj1:master.

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.

3 participants