Skip to content

Commit

Permalink
Fix comparison logic in Int.Cmp to correctly handle 0 and -0 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
louisgthier committed May 20, 2024
1 parent 8537a3d commit d7d8d5a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions int256.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ func (z *Int) Cmp(x *Int) (r int) {
if z.neg {
r = -r
}
case z.abs.IsZero() && x.abs.IsZero():
r = 0
case z.neg:
r = -1
default:
Expand Down
13 changes: 13 additions & 0 deletions int256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,19 @@ func TestInt_Cmp(t *testing.T) {
},
wantR: 1,
},
{
name: "Should return correct value",
fields: fields{
abs: uint256.NewInt(0),
neg: true,
},
args: args{
x: &Int{
abs: uint256.NewInt(0),
},
},
wantR: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit d7d8d5a

Please sign in to comment.