authorgravatar for yujiri@disroot.orgYujiri <yujiri@disroot.org> 2022-09-02 07:51:41+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-04 18:45:20+03:00
logae3a5ff7f962cba1eb57423e37ffaf94dc394152
tree4bc9271a36d2796e2bc4f5052eb3bb5383b663a3
parent349cf54b3293bc4177253e96a4f84fea0251fa08

Fix #12440: std.math.big.Rational order/orderAbs


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

lib/std/math/big/rational.zig+14-2
...@@ -334,13 +334,13 @@ pub const Rational = struct {...@@ -334,13 +334,13 @@ pub const Rational = struct {
334 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a334 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a
335 /// > b respectively.335 /// > b respectively.
336 pub fn order(a: Rational, b: Rational) !math.Order {336 pub fn order(a: Rational, b: Rational) !math.Order {
337 return cmpInternal(a, b, true);337 return cmpInternal(a, b, false);
338 }338 }
339339
340 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| ==340 /// Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| ==
341 /// |b| or |a| > |b| respectively.341 /// |b| or |a| > |b| respectively.
342 pub fn orderAbs(a: Rational, b: Rational) !math.Order {342 pub fn orderAbs(a: Rational, b: Rational) !math.Order {
343 return cmpInternal(a, b, false);343 return cmpInternal(a, b, true);
344 }344 }
345345
346 // p/q > x/y iff p*y > x*q346 // p/q > x/y iff p*y > x*q
...@@ -704,6 +704,18 @@ test "big.rational order" {...@@ -704,6 +704,18 @@ test "big.rational order" {
704 try testing.expect((try a.order(b)) == .eq);704 try testing.expect((try a.order(b)) == .eq);
705}705}
706706
707test "big.rational order/orderAbs with negative" {
708 var a = try Rational.init(testing.allocator);
709 defer a.deinit();
710 var b = try Rational.init(testing.allocator);
711 defer b.deinit();
712
713 try a.setRatio(1, 1);
714 try b.setRatio(-2, 1);
715 try testing.expect((try a.order(b)) == .gt);
716 try testing.expect((try a.orderAbs(b)) == .lt);
717}
718
707test "big.rational add single-limb" {719test "big.rational add single-limb" {
708 var a = try Rational.init(testing.allocator);720 var a = try Rational.init(testing.allocator);
709 defer a.deinit();721 defer a.deinit();