malachite_q/arithmetic/
traits.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::Rational;
10use malachite_nz::natural::Natural;
11
12/// Replaces a number with the closest [`Rational`] whose denominator does not exceed the specified
13/// maximum.
14pub trait ApproximateAssign {
15    fn approximate_assign(&mut self, max_denominator: &Natural);
16}
17
18/// Returns the closest [`Rational`] whose denominator does not exceed the specified maximum.
19pub trait Approximate {
20    fn approximate(self, max_denominator: &Natural) -> Rational;
21}
22
23/// Finds the simplest [`Rational`] contained in an interval.
24pub trait SimplestRationalInInterval {
25    /// Finds the simplest [`Rational`] contained in an open interval.
26    ///
27    /// Simplicity is defined as follows: If two [`Rational`]s have different denominators, then the
28    /// one with the smaller denominator is simpler. If they have the same denominator, then the one
29    /// whose numerator is closer to zero is simpler. Finally, if $q > 0$, then $q$ is simpler than
30    /// $-q$.
31    fn simplest_rational_in_open_interval(x: &Self, y: &Self) -> Rational;
32
33    /// Finds the simplest [`Rational`] contained in a closed interval.
34    ///
35    /// Simplicity is defined as follows: If two [`Rational`]s have different denominators, then the
36    /// one with the smaller denominator is simpler. If they have the same denominator, then the one
37    /// whose numerator is closer to zero is simpler. Finally, if $q > 0$, then $q$ is simpler than
38    /// $-q$.
39    fn simplest_rational_in_closed_interval(x: &Self, y: &Self) -> Rational;
40}
41
42// Returns an iterator of all denominators that appear in the [`Rational`]s contained in a closed
43// interval.
44pub trait DenominatorsInClosedInterval {
45    type Denominators: Iterator<Item = Natural>;
46
47    fn denominators_in_closed_interval(a: Rational, b: Rational) -> Self::Denominators;
48}