| author | |
| committer | |
| log | ed19ebc3605ec7e50166adf45f162dcf5540c42e |
| tree | 3b8080f5874a0198f35d3ac5e3c5b94215251c9a |
| parent | acac685621fc427e4e962f9b54b7f822a3d00dd0 |
| signature | Signed by PGP key 4AEE18F83AFDEB23 |
2 files changed, 38 insertions(+), 1 deletions(-)
lib/std/math/big/int.zig+5-1| ... | @@ -2452,7 +2452,11 @@ pub const Const = struct { | ... | @@ -2452,7 +2452,11 @@ pub const Const = struct { |
| 2452 | /// Returns `math.Order.lt`, `math.Order.eq`, `math.Order.gt` if `a < b`, `a == b` or `a > b` respectively. | 2452 | /// Returns `math.Order.lt`, `math.Order.eq`, `math.Order.gt` if `a < b`, `a == b` or `a > b` respectively. |
| 2453 | pub fn order(a: Const, b: Const) math.Order { | 2453 | pub fn order(a: Const, b: Const) math.Order { |
| 2454 | if (a.positive != b.positive) { | 2454 | 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 | } | ||
| 2456 | } else { | 2460 | } else { |
| 2457 | const r = orderAbs(a, b); | 2461 | const r = orderAbs(a, b); |
| 2458 | return if (a.positive) r else switch (r) { | 2462 | 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" { | ... | @@ -3105,3 +3105,36 @@ test "big.int sqr multi alias r with a" { |
| 3105 | try testing.expectEqual(@as(usize, 5), a.limbs.len); | 3105 | try testing.expectEqual(@as(usize, 5), a.limbs.len); |
| 3106 | } | 3106 | } |
| 3107 | } | 3107 | } |
| 3108 | |||
| 3109 | test "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 | |||
| 3130 | test "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 | } |