commit ae3a5ff7f962cba1eb57423e37ffaf94dc394152 (tree)
parent 349cf54b3293bc4177253e96a4f84fea0251fa08
Author: Yujiri <yujiri@disroot.org>
Date: Fri, 2 Sep 2022 07:51:41 +0000
Fix #12440: std.math.big.Rational order/orderAbs
Diffstat:
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/lib/std/math/big/rational.zig b/lib/std/math/big/rational.zig
@@ -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();