Skip to content

Commit

Permalink
std.math.big.int.Const.order 0 == -0 (#17299)
Browse files Browse the repository at this point in the history
  • Loading branch information
luehmann authored Sep 29, 2023
1 parent acac685 commit ed19ebc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/std/math/big/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2452,7 +2452,11 @@ pub const Const = struct {
/// Returns `math.Order.lt`, `math.Order.eq`, `math.Order.gt` if `a < b`, `a == b` or `a > b` respectively.
pub fn order(a: Const, b: Const) math.Order {
if (a.positive != b.positive) {
return if (a.positive) .gt else .lt;
if (eqlZero(a) and eqlZero(b)) {
return .eq;
} else {
return if (a.positive) .gt else .lt;
}
} else {
const r = orderAbs(a, b);
return if (a.positive) r else switch (r) {
Expand Down
33 changes: 33 additions & 0 deletions lib/std/math/big/int_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3105,3 +3105,36 @@ test "big.int sqr multi alias r with a" {
try testing.expectEqual(@as(usize, 5), a.limbs.len);
}
}

test "big.int eql zeroes #17296" {
var zero = try Managed.init(testing.allocator);
defer zero.deinit();
try zero.setString(10, "0");
try std.testing.expect(zero.eql(zero));

{
var sum = try Managed.init(testing.allocator);
defer sum.deinit();
try sum.add(&zero, &zero);
try std.testing.expect(zero.eql(sum));
}

{
var diff = try Managed.init(testing.allocator);
defer diff.deinit();
try diff.sub(&zero, &zero);
try std.testing.expect(zero.eql(diff));
}
}

test "big.int.Const.order 0 == -0" {
const a = std.math.big.int.Const{
.limbs = &.{0},
.positive = true,
};
const b = std.math.big.int.Const{
.limbs = &.{0},
.positive = false,
};
try std.testing.expectEqual(std.math.Order.eq, a.order(b));
}

0 comments on commit ed19ebc

Please sign in to comment.