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

This commit is contained in:
Yujiri 2022-09-02 07:51:41 +00:00 committed by Veikka Tuominen
parent 349cf54b32
commit ae3a5ff7f9

View File

@ -334,13 +334,13 @@ pub const Rational = struct {
/// Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a
/// > b respectively.
pub fn order(a: Rational, b: Rational) !math.Order {
return cmpInternal(a, b, true);
return cmpInternal(a, b, false);
}
/// Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| ==
/// |b| or |a| > |b| respectively.
pub fn orderAbs(a: Rational, b: Rational) !math.Order {
return cmpInternal(a, b, false);
return cmpInternal(a, b, true);
}
// p/q > x/y iff p*y > x*q
@ -704,6 +704,18 @@ test "big.rational order" {
try testing.expect((try a.order(b)) == .eq);
}
test "big.rational order/orderAbs with negative" {
var a = try Rational.init(testing.allocator);
defer a.deinit();
var b = try Rational.init(testing.allocator);
defer b.deinit();
try a.setRatio(1, 1);
try b.setRatio(-2, 1);
try testing.expect((try a.order(b)) == .gt);
try testing.expect((try a.orderAbs(b)) == .lt);
}
test "big.rational add single-limb" {
var a = try Rational.init(testing.allocator);
defer a.deinit();