authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2020-03-24 04:16:57+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-03-23 23:16:57-04:00
log5acc8afb5f9674b9b7b290635e9c2837872b8a93
tree43e6271ad868abb6cc09dbb0df314ddd089bf76c
parentdc44fe053c609f389e375f6857f96b6bb3794897
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Use math.Order for comparing bigints instead of i8 (#4791)


2 files changed, 54 insertions(+), 46 deletions(-)

lib/std/math/big/int.zig+34-28
......@@ -534,13 +534,14 @@ pub const Int = struct {
534534 return out_stream.writeAll(str);
535535 }
536536
537 /// Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively.
538 pub fn cmpAbs(a: Int, b: Int) i8 {
537 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| ==
538 /// |b| or |a| > |b| respectively.
539 pub fn cmpAbs(a: Int, b: Int) math.Order {
539540 if (a.len() < b.len()) {
540 return -1;
541 return .lt;
541542 }
542543 if (a.len() > b.len()) {
543 return 1;
544 return .gt;
544545 }
545546
546547 var i: usize = a.len() - 1;
......@@ -551,21 +552,26 @@ pub const Int = struct {
551552 }
552553
553554 if (a.limbs[i] < b.limbs[i]) {
554 return -1;
555 return .lt;
555556 } else if (a.limbs[i] > b.limbs[i]) {
556 return 1;
557 return .gt;
557558 } else {
558 return 0;
559 return .eq;
559560 }
560561 }
561562
562 /// Returns -1, 0, 1 if a < b, a == b or a > b respectively.
563 pub fn cmp(a: Int, b: Int) i8 {
563 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a
564 /// > b respectively.
565 pub fn cmp(a: Int, b: Int) math.Order {
564566 if (a.isPositive() != b.isPositive()) {
565 return if (a.isPositive()) @as(i8, 1) else -1;
567 return if (a.isPositive()) .gt else .lt;
566568 } else {
567569 const r = cmpAbs(a, b);
568 return if (a.isPositive()) r else -r;
570 return if (a.isPositive()) r else switch (r) {
571 .lt => math.Order.gt,
572 .eq => math.Order.eq,
573 .gt => math.Order.lt,
574 };
569575 }
570576 }
571577
......@@ -576,12 +582,12 @@ pub const Int = struct {
576582
577583 /// Returns true if |a| == |b|.
578584 pub fn eqAbs(a: Int, b: Int) bool {
579 return cmpAbs(a, b) == 0;
585 return cmpAbs(a, b) == .eq;
580586 }
581587
582588 /// Returns true if a == b.
583589 pub fn eq(a: Int, b: Int) bool {
584 return cmp(a, b) == 0;
590 return cmp(a, b) == .eq;
585591 }
586592
587593 // Normalize a possible sequence of leading zeros.
......@@ -694,7 +700,7 @@ pub const Int = struct {
694700 } else {
695701 if (a.isPositive()) {
696702 // (a) - (b) => a - b
697 if (a.cmp(b) >= 0) {
703 if (a.cmp(b) != .lt) {
698704 try r.ensureCapacity(a.len() + 1);
699705 llsub(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]);
700706 r.normalize(a.len());
......@@ -707,7 +713,7 @@ pub const Int = struct {
707713 }
708714 } else {
709715 // (-a) - (-b) => -(a - b)
710 if (a.cmp(b) < 0) {
716 if (a.cmp(b) == .lt) {
711717 try r.ensureCapacity(a.len() + 1);
712718 llsub(r.limbs[0..], a.limbs[0..a.len()], b.limbs[0..b.len()]);
713719 r.normalize(a.len());
......@@ -1010,7 +1016,7 @@ pub const Int = struct {
10101016 @panic("quo and rem cannot be same variable");
10111017 }
10121018
1013 if (a.cmpAbs(b) < 0) {
1019 if (a.cmpAbs(b) == .lt) {
10141020 // quo may alias a so handle rem first
10151021 try rem.copy(a);
10161022 rem.setSign(a.isPositive() == b.isPositive());
......@@ -1133,7 +1139,7 @@ pub const Int = struct {
11331139
11341140 // 2.
11351141 try tmp.shiftLeft(y.*, Limb.bit_count * (n - t));
1136 while (x.cmp(tmp) >= 0) {
1142 while (x.cmp(tmp) != .lt) {
11371143 q.limbs[n - t] += 1;
11381144 try x.sub(x.*, tmp);
11391145 }
......@@ -1164,7 +1170,7 @@ pub const Int = struct {
11641170 r.limbs[2] = carry;
11651171 r.normalize(3);
11661172
1167 if (r.cmpAbs(tmp) <= 0) {
1173 if (r.cmpAbs(tmp) != .gt) {
11681174 break;
11691175 }
11701176
......@@ -1719,8 +1725,8 @@ test "big.int compare" {
17191725 var b = try Int.initSet(testing.allocator, 10);
17201726 defer b.deinit();
17211727
1722 testing.expect(a.cmpAbs(b) == 1);
1723 testing.expect(a.cmp(b) == -1);
1728 testing.expect(a.cmpAbs(b) == .gt);
1729 testing.expect(a.cmp(b) == .lt);
17241730}
17251731
17261732test "big.int compare similar" {
......@@ -1729,8 +1735,8 @@ test "big.int compare similar" {
17291735 var b = try Int.initSet(testing.allocator, 0xffffffffeeeeeeeeffffffffeeeeeeef);
17301736 defer b.deinit();
17311737
1732 testing.expect(a.cmpAbs(b) == -1);
1733 testing.expect(b.cmpAbs(a) == 1);
1738 testing.expect(a.cmpAbs(b) == .lt);
1739 testing.expect(b.cmpAbs(a) == .gt);
17341740}
17351741
17361742test "big.int compare different limb size" {
......@@ -1739,8 +1745,8 @@ test "big.int compare different limb size" {
17391745 var b = try Int.initSet(testing.allocator, 1);
17401746 defer b.deinit();
17411747
1742 testing.expect(a.cmpAbs(b) == 1);
1743 testing.expect(b.cmpAbs(a) == -1);
1748 testing.expect(a.cmpAbs(b) == .gt);
1749 testing.expect(b.cmpAbs(a) == .lt);
17441750}
17451751
17461752test "big.int compare multi-limb" {
......@@ -1749,8 +1755,8 @@ test "big.int compare multi-limb" {
17491755 var b = try Int.initSet(testing.allocator, 0x7777777799999999ffffeeeeffffeeeeffffeeeee);
17501756 defer b.deinit();
17511757
1752 testing.expect(a.cmpAbs(b) == 1);
1753 testing.expect(a.cmp(b) == -1);
1758 testing.expect(a.cmpAbs(b) == .gt);
1759 testing.expect(a.cmp(b) == .lt);
17541760}
17551761
17561762test "big.int equality" {
......@@ -2726,9 +2732,9 @@ test "big.int var args" {
27262732
27272733 const c = try Int.initSet(testing.allocator, 11);
27282734 defer c.deinit();
2729 testing.expect(a.cmp(c) == 0);
2735 testing.expect(a.cmp(c) == .eq);
27302736
27312737 const d = try Int.initSet(testing.allocator, 14);
27322738 defer d.deinit();
2733 testing.expect(a.cmp(d) <= 0);
2739 testing.expect(a.cmp(d) != .gt);
27342740}
lib/std/math/big/rational.zig+20-18
......@@ -326,18 +326,20 @@ pub const Rational = struct {
326326 r.q.swap(&other.q);
327327 }
328328
329 /// Returns -1, 0, 1 if a < b, a == b or a > b respectively.
330 pub fn cmp(a: Rational, b: Rational) !i8 {
329 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a
330 /// > b respectively.
331 pub fn cmp(a: Rational, b: Rational) !math.Order {
331332 return cmpInternal(a, b, true);
332333 }
333334
334 /// Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively.
335 pub fn cmpAbs(a: Rational, b: Rational) !i8 {
335 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| ==
336 /// |b| or |a| > |b| respectively.
337 pub fn cmpAbs(a: Rational, b: Rational) !math.Order {
336338 return cmpInternal(a, b, false);
337339 }
338340
339341 // p/q > x/y iff p*y > x*q
340 fn cmpInternal(a: Rational, b: Rational, is_abs: bool) !i8 {
342 fn cmpInternal(a: Rational, b: Rational, is_abs: bool) !math.Order {
341343 // TODO: Would a div compare algorithm of sorts be viable and quicker? Can we avoid
342344 // the memory allocations here?
343345 var q = try Int.init(a.p.allocator.?);
......@@ -450,7 +452,7 @@ pub const Rational = struct {
450452 r.p.setSign(sign);
451453
452454 const one = Int.initFixed(([_]Limb{1})[0..]);
453 if (a.cmp(one) != 0) {
455 if (a.cmp(one) != .eq) {
454456 var unused = try Int.init(r.p.allocator.?);
455457 defer unused.deinit();
456458
......@@ -505,7 +507,7 @@ fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void {
505507 y.abs();
506508 defer y.deinit();
507509
508 if (x.cmp(y) < 0) {
510 if (x.cmp(y) == .lt) {
509511 x.swap(&y);
510512 }
511513
......@@ -573,7 +575,7 @@ fn gcdLehmer(r: *Int, xa: Int, ya: Int) !void {
573575 }
574576
575577 // euclidean algorithm
576 debug.assert(x.cmp(y) >= 0);
578 debug.assert(x.cmp(y) != .lt);
577579
578580 while (!y.eqZero()) {
579581 try Int.divTrunc(&T, r, x, y);
......@@ -874,11 +876,11 @@ test "big.rational cmp" {
874876
875877 try a.setRatio(500, 231);
876878 try b.setRatio(18903, 8584);
877 testing.expect((try a.cmp(b)) < 0);
879 testing.expect((try a.cmp(b)) == .lt);
878880
879881 try a.setRatio(890, 10);
880882 try b.setRatio(89, 1);
881 testing.expect((try a.cmp(b)) == 0);
883 testing.expect((try a.cmp(b)) == .eq);
882884}
883885
884886test "big.rational add single-limb" {
......@@ -889,11 +891,11 @@ test "big.rational add single-limb" {
889891
890892 try a.setRatio(500, 231);
891893 try b.setRatio(18903, 8584);
892 testing.expect((try a.cmp(b)) < 0);
894 testing.expect((try a.cmp(b)) == .lt);
893895
894896 try a.setRatio(890, 10);
895897 try b.setRatio(89, 1);
896 testing.expect((try a.cmp(b)) == 0);
898 testing.expect((try a.cmp(b)) == .eq);
897899}
898900
899901test "big.rational add" {
......@@ -909,7 +911,7 @@ test "big.rational add" {
909911 try a.add(a, b);
910912
911913 try r.setRatio(984786924199, 290395044174);
912 testing.expect((try a.cmp(r)) == 0);
914 testing.expect((try a.cmp(r)) == .eq);
913915}
914916
915917test "big.rational sub" {
......@@ -925,7 +927,7 @@ test "big.rational sub" {
925927 try a.sub(a, b);
926928
927929 try r.setRatio(979040510045, 290395044174);
928 testing.expect((try a.cmp(r)) == 0);
930 testing.expect((try a.cmp(r)) == .eq);
929931}
930932
931933test "big.rational mul" {
......@@ -941,7 +943,7 @@ test "big.rational mul" {
941943 try a.mul(a, b);
942944
943945 try r.setRatio(571481443, 17082061422);
944 testing.expect((try a.cmp(r)) == 0);
946 testing.expect((try a.cmp(r)) == .eq);
945947}
946948
947949test "big.rational div" {
......@@ -957,7 +959,7 @@ test "big.rational div" {
957959 try a.div(a, b);
958960
959961 try r.setRatio(75531824394, 221015929);
960 testing.expect((try a.cmp(r)) == 0);
962 testing.expect((try a.cmp(r)) == .eq);
961963}
962964
963965test "big.rational div" {
......@@ -970,11 +972,11 @@ test "big.rational div" {
970972 a.invert();
971973
972974 try r.setRatio(23341, 78923);
973 testing.expect((try a.cmp(r)) == 0);
975 testing.expect((try a.cmp(r)) == .eq);
974976
975977 try a.setRatio(-78923, 23341);
976978 a.invert();
977979
978980 try r.setRatio(-23341, 78923);
979 testing.expect((try a.cmp(r)) == 0);
981 testing.expect((try a.cmp(r)) == .eq);
980982}