authorgravatar for philipp@luehmann.devPhilipp Lühmann <philipp@luehmann.dev> 2023-09-29 07:09:47+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-09-29 18:09:47+13:00
loged19ebc3605ec7e50166adf45f162dcf5540c42e
tree3b8080f5874a0198f35d3ac5e3c5b94215251c9a
parentacac685621fc427e4e962f9b54b7f822a3d00dd0
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.math.big.int.Const.order 0 == -0 (#17299)


2 files changed, 38 insertions(+), 1 deletions(-)

lib/std/math/big/int.zig+5-1
......@@ -2452,7 +2452,11 @@ pub const Const = struct {
24522452 /// Returns `math.Order.lt`, `math.Order.eq`, `math.Order.gt` if `a < b`, `a == b` or `a > b` respectively.
24532453 pub fn order(a: Const, b: Const) math.Order {
24542454 if (a.positive != b.positive) {
2455 return if (a.positive) .gt else .lt;
2455 if (eqlZero(a) and eqlZero(b)) {
2456 return .eq;
2457 } else {
2458 return if (a.positive) .gt else .lt;
2459 }
24562460 } else {
24572461 const r = orderAbs(a, b);
24582462 return if (a.positive) r else switch (r) {
lib/std/math/big/int_test.zig+33
......@@ -3105,3 +3105,36 @@ test "big.int sqr multi alias r with a" {
31053105 try testing.expectEqual(@as(usize, 5), a.limbs.len);
31063106 }
31073107}
3108
3109test "big.int eql zeroes #17296" {
3110 var zero = try Managed.init(testing.allocator);
3111 defer zero.deinit();
3112 try zero.setString(10, "0");
3113 try std.testing.expect(zero.eql(zero));
3114
3115 {
3116 var sum = try Managed.init(testing.allocator);
3117 defer sum.deinit();
3118 try sum.add(&zero, &zero);
3119 try std.testing.expect(zero.eql(sum));
3120 }
3121
3122 {
3123 var diff = try Managed.init(testing.allocator);
3124 defer diff.deinit();
3125 try diff.sub(&zero, &zero);
3126 try std.testing.expect(zero.eql(diff));
3127 }
3128}
3129
3130test "big.int.Const.order 0 == -0" {
3131 const a = std.math.big.int.Const{
3132 .limbs = &.{0},
3133 .positive = true,
3134 };
3135 const b = std.math.big.int.Const{
3136 .limbs = &.{0},
3137 .positive = false,
3138 };
3139 try std.testing.expectEqual(std.math.Order.eq, a.order(b));
3140}