pub struct Rational { /* private fields */ }
Expand description

A rational number.

Implementations

Calculates the approximate natural logarithm of a positive Rational.

$f(x) = (1+\epsilon)(\log x)$, where $|\epsilon| < 2^{-52}.$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::{Pow, PowerOf2};
use malachite_base::num::float::NiceFloat;
use malachite_q::Rational;

assert_eq!(NiceFloat(Rational::from(10i32).approx_log()), NiceFloat(2.3025850929940455));
assert_eq!(
    NiceFloat(Rational::from(10i32).pow(100u64).approx_log()),
    NiceFloat(230.25850929940455)
);
assert_eq!(
    NiceFloat(Rational::power_of_2(1000000u64).approx_log()),
    NiceFloat(693147.1805599453)
);
assert_eq!(
    NiceFloat(Rational::power_of_2(-1000000i64).approx_log()),
    NiceFloat(-693147.1805599453)
);

This is equivalent to fmpz_dlog from fmpz/dlog.c, FLINT 2.7.1.

Compares two Rationals according to their complexity.

Complexity is defined as follows: If two Rationals have different denominators, then the one with the larger denominator is more complex. If they have the same denominator, then the one whose numerator is further from zero is more complex. Finally, if $q > 0$, then $q$ is simpler than $-q$.

The Rationals ordered by complexity look like this: $$ 0, 1, -1, 2, -2, \ldots, 1/2, -1/2, 3/2, -3/2, \ldots, 1/3, -1/3, 2/3, -2/3, \ldots, \ldots. $$ This order is a well-order, and the order type of the Rationals under this order is $\omega^2$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
use malachite_q::Rational;
use std::cmp::Ordering;

assert_eq!(
    Rational::from_signeds(1, 2).cmp_complexity(&Rational::from_signeds(1, 3)),
    Ordering::Less
);
assert_eq!(
    Rational::from_signeds(1, 2).cmp_complexity(&Rational::from_signeds(3, 2)),
    Ordering::Less
);
assert_eq!(
    Rational::from_signeds(1, 2).cmp_complexity(&Rational::from_signeds(-1, 2)),
    Ordering::Less
);

Converts a finite continued fraction to a Rational, taking the inputs by value.

The input has two components. The first is the first value of the continued fraction, which may be any Integer and is equal to the floor of the Rational. The second is an iterator of the remaining values, which must all be positive. Using the standard notation for continued fractions, the first value is the number before the semicolon, and the second value contains the remaining numbers.

Each rational number has two continued fraction representations. Either one is a valid input.

$f(a_0, (a_1, a_2, a_3, \ldots)) = [a_0; a_1, a_2, a_3, \ldots]$.

Worst-case complexity

$T(n, m) = O((nm)^2 \log (nm) \log\log (nm))$

$M(n, m) = O(nm \log (nm))$

where $T$ is time, $M$ is additional memory, $n$ is max(floor.significant_bits(), xs.map(Natural::significant_bits).max()), and $m$ is xs.count().

Panics

Panics if any Natural in xs is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_base::vecs::vec_from_str;
use malachite_nz::integer::Integer;
use malachite_q::Rational;

let xs = vec_from_str("[1, 2]").unwrap().into_iter();
assert_eq!(Rational::from_continued_fraction(Integer::ZERO, xs).to_string(), "2/3");

let xs = vec_from_str("[7, 16]").unwrap().into_iter();
assert_eq!(Rational::from_continued_fraction(Integer::from(3), xs).to_string(), "355/113");

Converts a finite continued fraction to a Rational, taking the inputs by reference.

The input has two components. The first is the first value of the continued fraction, which may be any Integer and is equal to the floor of the Rational. The second is an iterator of the remaining values, which must all be positive. Using the standard notation for continued fractions, the first value is the number before the semicolon, and the second value contains the remaining numbers.

Each rational number has two continued fraction representations. Either one is a valid input.

$f(a_0, (a_1, a_2, a_3, \ldots)) = [a_0; a_1, a_2, a_3, \ldots]$.

Worst-case complexity

$T(n, m) = O((nm)^2 \log (nm) \log\log (nm))$

$M(n, m) = O(nm \log (nm))$

where $T$ is time, $M$ is additional memory, $n$ is max(floor.significant_bits(), xs.map(Natural::significant_bits).max()), and $m$ is xs.count().

Panics

Panics if any Natural in xs is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_base::vecs::vec_from_str;
use malachite_nz::integer::Integer;
use malachite_q::Rational;

let xs = vec_from_str("[1, 2]").unwrap();
assert_eq!(
    Rational::from_continued_fraction_ref(&Integer::ZERO, xs.iter()).to_string(),
    "2/3"
);

let xs = vec_from_str("[7, 16]").unwrap();
assert_eq!(
    Rational::from_continued_fraction_ref(&Integer::from(3), xs.iter()).to_string(),
    "355/113"
);

Returns the base-$b$ digits of a Rational.

The output has two components. The first is a Vec of the digits of the integer portion of the Rational, least- to most-significant. The second is an iterator of the digits of the fractional portion.

The output is in its simplest form: the integer-portion digits do not end with a zero, and the fractional-portion digits do not end with infinitely many zeros or $(b-1)$s.

If the Rational has a small denominator, it may be more efficient to use to_digits or into_digits instead. These functions compute the entire repeating portion of the repeating digits.

For example, consider these two expressions:

  • Rational::from_signeds(1, 7).digits(Natural::from(10u32)).1.nth(1000)
  • Rational::from_signeds(1, 7).into_digits(Natural::from(10u32)).1[1000]

Both get the thousandth digit after the decimal point of 1/7. The first way explicitly calculates each digit after the decimal point, whereas the second way determines that 1/7 is 0.(142857), with the 142857 repeating, and takes 1000 % 6 == 4 to determine that the thousandth digit is 5. But when the Rational has a large denominator, the second way is less efficient.

Worst-case complexity per iteration

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), base.significant_bits()).

Panics

Panics if base is less than 2.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::iterators::prefix_to_string;
use malachite_base::strings::ToDebugString;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let (before_point, after_point) = Rational::from(3u32).digits(&Natural::from(10u32));
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(prefix_to_string(after_point, 10), "[]");

let (before_point, after_point) =
        Rational::from_signeds(22, 7).digits(&Natural::from(10u32));
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(prefix_to_string(after_point, 10), "[1, 4, 2, 8, 5, 7, 1, 4, 2, 8, ...]");

Converts base-$b$ digits to a Rational. The inputs are taken by value.

The input consists of the digits of the integer portion of the Rational and the digits of the fractional portion. The integer-portion digits are ordered from least- to most-significant, and the fractional-portion digits from most- to least.

The fractional-portion digits may end in infinitely many zeros or $(b-1)$s; these are handled correctly.

Worst-case complexity

$T(n, m) = O(nm \log (nm)^2 \log\log (nm))$

$M(n, m) = O(nm \log (nm))$

where $T$ is time, $M$ is additional memory, $n$ is max(before_point.len(), after_point.component_len()), and $m$ is base.significant_bits().

Panics

Panics if base is less than 2.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::rational_sequences::RationalSequence;
use malachite_base::vecs::vec_from_str;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let before_point = vec_from_str("[3]").unwrap();
let after_point = RationalSequence::from_vecs(
    Vec::new(),
    vec_from_str("[1, 4, 2, 8, 5, 7]").unwrap(),
);
assert_eq!(
    Rational::from_digits(&Natural::from(10u32), before_point, after_point).to_string(),
    "22/7"
);

// 21.34565656...
let before_point = vec_from_str("[1, 2]").unwrap();
let after_point = RationalSequence::from_vecs(
    vec_from_str("[3, 4]").unwrap(),
    vec_from_str("[5, 6]").unwrap(),
);
assert_eq!(
    Rational::from_digits(&Natural::from(10u32), before_point, after_point).to_string(),
    "105661/4950"
);

Converts base-$b$ digits to a Rational. The inputs are taken by reference.

The input consists of the digits of the integer portion of the Rational and the digits of the fractional portion. The integer-portion digits are ordered from least- to most-significant, and the fractional-portion digits from most- to least.

The fractional-portion digits may end in infinitely many zeros or $(b-1)$s; these are handled correctly.

Worst-case complexity

$T(n, m) = O(nm \log (nm)^2 \log\log (nm))$

$M(n, m) = O(nm \log (nm))$

where $T$ is time, $M$ is additional memory, $n$ is max(before_point.len(), after_point.component_len()), and $m$ is base.significant_bits().

Panics

Panics if base is less than 2.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::rational_sequences::RationalSequence;
use malachite_base::vecs::vec_from_str;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let before_point = vec_from_str("[3]").unwrap();
let after_point = RationalSequence::from_vecs(
    Vec::new(),
    vec_from_str("[1, 4, 2, 8, 5, 7]").unwrap(),
);
assert_eq!(
    Rational::from_digits_ref(&Natural::from(10u32), &before_point, &after_point)
        .to_string(),
    "22/7"
);

// 21.34565656...
let before_point = vec_from_str("[1, 2]").unwrap();
let after_point = RationalSequence::from_vecs(
    vec_from_str("[3, 4]").unwrap(),
    vec_from_str("[5, 6]").unwrap(),
);
assert_eq!(
    Rational::from_digits_ref(&Natural::from(10u32), &before_point, &after_point)
        .to_string(),
    "105661/4950"
);

Converts base-$2^k$ digits to a Rational. The inputs are taken by value.

The input consists of the digits of the integer portion of the Rational and the digits of the fractional portion. The integer-portion digits are ordered from least- to most-significant, and the fractional-portion digits from most- to least.

The fractional-portion digits may end in infinitely many zeros or $(2^k-1)$s; these are handled correctly.

Worst-case complexity

$T(n, m) = O(nm)$

$M(n, m) = O(nm)$

where $T$ is time, $M$ is additional memory, $n$ is max(before_point.len(), after_point.component_len()), and $m$ is base.significant_bits().

Panics

Panics if log_base is zero.

Examples
extern crate malachite_base;

use malachite_base::rational_sequences::RationalSequence;
use malachite_base::vecs::vec_from_str;
use malachite_q::Rational;

let before_point = vec_from_str("[1, 1]").unwrap();
let after_point = RationalSequence::from_vecs(
    vec_from_str("[0]").unwrap(),
    vec_from_str("[0, 0, 1]").unwrap(),
);
assert_eq!(
    Rational::from_power_of_2_digits(1, before_point, after_point).to_string(),
    "43/14"
);

// 21.34565656..._32
let before_point = vec_from_str("[1, 2]").unwrap();
let after_point = RationalSequence::from_vecs(
    vec_from_str("[3, 4]").unwrap(),
    vec_from_str("[5, 6]").unwrap(),
);
assert_eq!(
    Rational::from_power_of_2_digits(5, before_point, after_point).to_string(),
    "34096673/523776"
);

Converts base-$2^k$ digits to a Rational. The inputs are taken by reference.

The input consists of the digits of the integer portion of the Rational and the digits of the fractional portion. The integer-portion digits are ordered from least- to most-significant, and the fractional-portion digits from most- to least.

The fractional-portion digits may end in infinitely many zeros or $(2^k-1)$s; these are handled correctly.

Worst-case complexity

$T(n, m) = O(nm)$

$M(n, m) = O(nm)$

where $T$ is time, $M$ is additional memory, $n$ is max(before_point.len(), after_point.component_len()), and $m$ is base.significant_bits().

Panics

Panics if log_base is zero.

Examples
extern crate malachite_base;

use malachite_base::rational_sequences::RationalSequence;
use malachite_base::vecs::vec_from_str;
use malachite_q::Rational;

let before_point = vec_from_str("[1, 1]").unwrap();
let after_point = RationalSequence::from_vecs(
    vec_from_str("[0]").unwrap(),
    vec_from_str("[0, 0, 1]").unwrap(),
);
assert_eq!(
    Rational::from_power_of_2_digits_ref(1, &before_point, &after_point).to_string(),
    "43/14"
);

// 21.34565656..._32
let before_point = vec_from_str("[1, 2]").unwrap();
let after_point = RationalSequence::from_vecs(
    vec_from_str("[3, 4]").unwrap(),
    vec_from_str("[5, 6]").unwrap(),
);
assert_eq!(
    Rational::from_power_of_2_digits_ref(5, &before_point, &after_point).to_string(),
    "34096673/523776"
);

Returns the base-$2^k$ digits of a Rational.

The output has two components. The first is a Vec of the digits of the integer portion of the Rational, least- to most-significant. The second is an iterator of the digits of the fractional portion.

The output is in its simplest form: the integer-portion digits do not end with a zero, and the fractional-portion digits do not end with infinitely many zeros or $(2^k-1)$s.

If the Rational has a small denominator, it may be more efficient to use to_power_of_2_digits or into_power_of_2_digits instead. These functions compute the entire repeating portion of the repeating digits.

For example, consider these two expressions:

  • Rational::from_signeds(1, 7).power_of_2_digits(1).1.nth(1000)
  • Rational::from_signeds(1, 7).into_power_of_2_digits(1).1[1000]

Both get the thousandth digit after the binary point of 1/7. The first way explicitly calculates each bit after the binary point, whereas the second way determines that 1/7 is 0.(001), with the 001 repeating, and takes 1000 % 3 == 1 to determine that the thousandth bit is 0. But when the Rational has a large denominator, the second way is less efficient.

Worst-case complexity per iteration

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), base).

Panics

Panics if log_base is zero.

Examples
extern crate malachite_base;

use malachite_base::iterators::prefix_to_string;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

let (before_point, after_point) = Rational::from(3u32).power_of_2_digits(1);
assert_eq!(before_point.to_debug_string(), "[1, 1]");
assert_eq!(prefix_to_string(after_point, 10), "[]");

let (before_point, after_point) = Rational::from_signeds(22, 7).power_of_2_digits(1);
assert_eq!(before_point.to_debug_string(), "[1, 1]");
assert_eq!(prefix_to_string(after_point, 10), "[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, ...]");

let (before_point, after_point) = Rational::from_signeds(22, 7).power_of_2_digits(10);
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(
    prefix_to_string(after_point, 10),
    "[146, 292, 585, 146, 292, 585, 146, 292, 585, 146, ...]"
);

Returns the base-$b$ digits of a Rational, taking the Rational by value.

The output has two components. The first is a Vec of the digits of the integer portion of the Rational, least- to most-significant. The second is a RationalSequence of the digits of the fractional portion.

The output is in its simplest form: the integer-portion digits do not end with a zero, and the fractional-portion digits do not end with infinitely many zeros or $(b-1)$s.

The fractional portion may be very large; the length of the repeating part may be almost as large as the denominator. If the Rational has a large denominator, consider using digits instead, which returns an iterator. That function computes the fractional digits lazily and doesn’t need to compute the entire repeating part.

Worst-case complexity

$T(n, m) = O(m2^n)$

$M(n, m) = O(m2^n)$

where $T$ is time, $M$ is additional memory, $n$ is self.significant_bits(), and $m$ is base.significant_bits().

Panics

Panics if base is less than 2.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::strings::ToDebugString;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let (before_point, after_point) = Rational::from(3u32).into_digits(&Natural::from(10u32));
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(after_point.to_string(), "[]");

let (before_point, after_point) =
        Rational::from_signeds(22, 7).into_digits(&Natural::from(10u32));
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(after_point.to_string(), "[[1, 4, 2, 8, 5, 7]]");

Returns the base-$b$ digits of a Rational, taking the Rational by reference.

The output has two components. The first is a Vec of the digits of the integer portion of the Rational, least- to most-significant. The second is a RationalSequence of the digits of the fractional portion.

The output is in its simplest form: the integer-portion digits do not end with a zero, and the fractional-portion digits do not end with infinitely many zeros or $(b-1)$s.

The fractional portion may be very large; the length of the repeating part may be almost as large as the denominator. If the Rational has a large denominator, consider using digits instead, which returns an iterator. That function computes the fractional digits lazily and doesn’t need to compute the entire repeating part.

Worst-case complexity

$T(n, m) = O(m2^n)$

$M(n, m) = O(m2^n)$

where $T$ is time, $M$ is additional memory, $n$ is self.significant_bits(), and $m$ is base.significant_bits().

Panics

Panics if base is less than 2.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::strings::ToDebugString;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let (before_point, after_point) = Rational::from(3u32).to_digits(&Natural::from(10u32));
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(after_point.to_string(), "[]");

let (before_point, after_point) =
        Rational::from_signeds(22, 7).to_digits(&Natural::from(10u32));
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(after_point.to_string(), "[[1, 4, 2, 8, 5, 7]]");

Returns the base-$2^k$ digits of a Rational, taking the Rational by value.

The output has two components. The first is a Vec of the digits of the integer portion of the Rational, least- to most-significant. The second is a RationalSequence of the digits of the fractional portion.

The output is in its simplest form: the integer-portion digits do not end with a zero, and the fractional-portion digits do not end with infinitely many zeros or $(2^k-1)$s.

The fractional portion may be very large; the length of the repeating part may be almost as large as the denominator. If the Rational has a large denominator, consider using power_of_2_digits instead, which returns an iterator. That function computes the fractional digits lazily and doesn’t need to compute the entire repeating part.

Worst-case complexity

$T(n, m) = O(m2^n)$

$M(n, m) = O(m2^n)$

where $T$ is time, $M$ is additional memory, $n$ is self.significant_bits(), and $m$ is log_base.

Panics

Panics if log_base is zero.

Examples
extern crate malachite_base;

use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

let (before_point, after_point) = Rational::from(3u32).into_power_of_2_digits(1);
assert_eq!(before_point.to_debug_string(), "[1, 1]");
assert_eq!(after_point.to_string(), "[]");

let (before_point, after_point) = Rational::from_signeds(22, 7).into_power_of_2_digits(1);
assert_eq!(before_point.to_debug_string(), "[1, 1]");
assert_eq!(after_point.to_string(), "[[0, 0, 1]]");

let (before_point, after_point) = Rational::from_signeds(22, 7).into_power_of_2_digits(10);
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(after_point.to_string(), "[[146, 292, 585]]");

Returns the base-$2^k$ digits of a Rational, taking the Rational by reference.

The output has two components. The first is a Vec of the digits of the integer portion of the Rational, least- to most-significant. The second is a RationalSequence of the digits of the fractional portion.

The output is in its simplest form: the integer-portion digits do not end with a zero, and the fractional-portion digits do not end with infinitely many zeros or $(2^k-1)$s.

The fractional portion may be very large; the length of the repeating part may be almost as large as the denominator. If the Rational has a large denominator, consider using power_of_2_digits instead, which returns an iterator. That function computes the fractional digits lazily and doesn’t need to compute the entire repeating part.

Worst-case complexity

$T(n, m) = O(m2^n)$

$M(n, m) = O(m2^n)$

where $T$ is time, $M$ is additional memory, $n$ is self.significant_bits(), and $m$ is log_base.

Panics

Panics if log_base is zero.

Examples
extern crate malachite_base;

use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

let (before_point, after_point) = Rational::from(3u32).to_power_of_2_digits(1);
assert_eq!(before_point.to_debug_string(), "[1, 1]");
assert_eq!(after_point.to_string(), "[]");

let (before_point, after_point) = Rational::from_signeds(22, 7).to_power_of_2_digits(1);
assert_eq!(before_point.to_debug_string(), "[1, 1]");
assert_eq!(after_point.to_string(), "[[0, 0, 1]]");

let (before_point, after_point) = Rational::from_signeds(22, 7).to_power_of_2_digits(10);
assert_eq!(before_point.to_debug_string(), "[3]");
assert_eq!(after_point.to_string(), "[[146, 292, 585]]");

Converts a primitive float to the simplest Rational that rounds to that value.

To be more specific: Suppose the floating-point input is $x$. If $x$ is an integer, its Rational equivalent is returned. Otherwise, this function finds $a$ and $b$, which are the floating point predecessor and successor of $x$, and finds the simplest Rational in the open interval $(\frac{x + a}{2}, \frac{x + b}{2})$. “Simplicity” refers to low complexity. See Rational::cmp_complexity for a definition of complexity.

For example, 0.1f32 is converted to $1/10$ rather than to the exact value of the float, which is $13421773/134217728$. If you want the exact value, use Rational::from instead.

If the floating point value cannot be NaN or infinite, and error is returned.

Worst-case complexity

$T(n) = O(n^2 \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is x.sci_exponent().

Examples
use malachite_base::strings::ToDebugString;
use malachite_q::conversion::from_primitive_float::RationalFromPrimitiveFloatError;
use malachite_q::Rational;

assert_eq!(Rational::try_from_float_simplest(0.0).to_debug_string(), "Ok(0)");
assert_eq!(Rational::try_from_float_simplest(1.5).to_debug_string(), "Ok(3/2)");
assert_eq!(Rational::try_from_float_simplest(-1.5).to_debug_string(), "Ok(-3/2)");
assert_eq!(Rational::try_from_float_simplest(0.1f32).to_debug_string(), "Ok(1/10)");
assert_eq!(Rational::try_from_float_simplest(0.33333334f32).to_debug_string(), "Ok(1/3)");
assert_eq!(
    Rational::try_from_float_simplest(f32::NAN),
    Err(RationalFromPrimitiveFloatError)
);

Converts two Naturals to a Rational, taking the Naturals by value.

The Naturals become the Rational’s numerator and denominator. Only non-negative Rationals can be produced with this function.

The denominator may not be zero.

The input Naturals may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(numerator.significant_bits(), denominator.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert_eq!(
    Rational::from_naturals(Natural::from(4u32), Natural::from(6u32)).to_string(),
    "2/3"
);
assert_eq!(Rational::from_naturals(Natural::ZERO, Natural::from(6u32)), 0);

Converts two Naturals to a Rational, taking the Naturals by reference.

The Naturals become the Rational’s numerator and denominator. Only non-negative Rationals can be produced with this function.

The denominator may not be zero.

The input Naturals may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(numerator.significant_bits(), denominator.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert_eq!(
    Rational::from_naturals_ref(&Natural::from(4u32), &Natural::from(6u32)).to_string(),
    "2/3"
);
assert_eq!(Rational::from_naturals_ref(&Natural::ZERO, &Natural::from(6u32)), 0);

Converts two unsigned primitive integers to a Rational.

The integers become the Rational’s numerator and denominator. Only non-negative Rationals can be produced with this function.

The denominator may not be zero.

The input integers may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n^2)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(Rational::from_unsigneds(4u32, 6).to_string(), "2/3");
assert_eq!(Rational::from_unsigneds(0u32, 6), 0);

Converts two Integers to a Rational, taking the Integers by value.

The absolute values of the Integers become the Rational’s numerator and denominator. The sign of the Rational is the sign of the Integers’ quotient.

The denominator may not be zero.

The input Integers may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(numerator.significant_bits(), denominator.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert_eq!(
    Rational::from_integers(Integer::from(4), Integer::from(6)).to_string(),
    "2/3"
);
assert_eq!(
    Rational::from_integers(Integer::from(4), Integer::from(-6)).to_string(),
    "-2/3"
);
assert_eq!(Rational::from_integers(Integer::ZERO, Integer::from(6)), 0);
assert_eq!(Rational::from_integers(Integer::ZERO, Integer::from(-6)), 0);

Converts two Integers to a Rational, taking the Integers by reference.

The absolute values of the Integers become the Rational’s numerator and denominator. The sign of the Rational is the sign of the Integers’ quotient.

The denominator may not be zero.

The input Integers may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(numerator.significant_bits(), denominator.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert_eq!(
    Rational::from_integers_ref(&Integer::from(4), &Integer::from(6)).to_string(),
    "2/3"
);
assert_eq!(
    Rational::from_integers_ref(&Integer::from(4), &Integer::from(-6)).to_string(),
    "-2/3"
);
assert_eq!(Rational::from_integers_ref(&Integer::ZERO, &Integer::from(6)), 0);
assert_eq!(Rational::from_integers_ref(&Integer::ZERO, &Integer::from(-6)), 0);

Converts two signed primitive integers to a [Rational].

The absolute values of the integers become the Rational’s numerator and denominator. The sign of the Rational is the sign of the integers’ quotient.

The denominator may not be zero.

The input integers may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n^2)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(Rational::from_signeds(4i8, 6).to_string(), "2/3");
assert_eq!(Rational::from_signeds(4i8, -6).to_string(), "-2/3");
assert_eq!(Rational::from_signeds(0i8, 6), 0);
assert_eq!(Rational::from_signeds(0i8, -6), 0);

Converts a sign and two Naturals to a Rational, taking the Naturals by value.

The Naturals become the Rational’s numerator and denominator, and the sign indicates whether the Rational should be non-negative. If the numerator is zero, then the Rational will be non-negative regardless of the sign.

The denominator may not be zero.

The input Naturals may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(numerator.significant_bits(), denominator.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert_eq!(
    Rational::from_sign_and_naturals(
        true,
        Natural::from(4u32),
        Natural::from(6u32)
    ).to_string(),
    "2/3"
);
assert_eq!(
    Rational::from_sign_and_naturals(
        false,
        Natural::from(4u32),
        Natural::from(6u32)
    ).to_string(),
    "-2/3"
);

Converts a sign and two Naturals to a Rational, taking the Naturals by reference.

The Naturals become the Rational’s numerator and denominator, and the sign indicates whether the Rational should be non-negative. If the numerator is zero, then the Rational will be non-negative regardless of the sign.

The denominator may not be zero.

The input Naturals may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(numerator.significant_bits(), denominator.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert_eq!(
    Rational::from_sign_and_naturals_ref(
        true,
        &Natural::from(4u32),
        &Natural::from(6u32)
    ).to_string(),
    "2/3"
);
assert_eq!(
    Rational::from_sign_and_naturals_ref(
        false,
        &Natural::from(4u32),
        &Natural::from(6u32)
    ).to_string(),
    "-2/3"
);

Converts a sign and two primitive unsigned integers to a Rational.

The integers become the Rational’s numerator and denominator, and the sign indicates whether the Rational should be non-negative. If the numerator is zero, then the Rational will be non-negative regardless of the sign.

The denominator may not be zero.

The input integers may have common factors; this function reduces them.

Worst-case complexity

$T(n) = O(n^2)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if denominator is zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(Rational::from_sign_and_unsigneds(true, 4u32, 6).to_string(), "2/3");
assert_eq!(Rational::from_sign_and_unsigneds(false, 4u32, 6).to_string(), "-2/3");

Returns a Rational’s scientific mantissa and exponent, taking the Rational by value.

When $x$ is positive, we can write $x = 2^{e_s}m_s$, where $e_s$ is an integer and $m_s$ is a rational number with $1 \leq m_s < 2$. We represent the rational mantissa as a float. The conversion might not be exact, so we round to the nearest float using the provided rounding mode. If the rounding mode is Exact but the conversion is not exact, None is returned. $$ f(x, r) \approx \left (\frac{x}{2^{\lfloor \log_2 x \rfloor}}, \lfloor \log_2 x \rfloor\right ). $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::conversion::traits::SciMantissaAndExponent;
use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode;
use malachite_q::Rational;

let test = |n: Rational, rm: RoundingMode, out: Option<(f32, i64)>| {
    assert_eq!(
        n.sci_mantissa_and_exponent_with_rounding(rm)
            .map(|(m, e)| (NiceFloat(m), e)),
        out.map(|(m, e)| (NiceFloat(m), e))
    );
};
test(Rational::from(3u32), RoundingMode::Down, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Ceiling, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Up, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Nearest, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Exact, Some((1.5, 1)));

test(Rational::from_signeds(1, 3), RoundingMode::Floor, Some((1.3333333, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Down, Some((1.3333333, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Ceiling, Some((1.3333334, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Up, Some((1.3333334, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Nearest, Some((1.3333334, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Exact, None);

Returns a Rational’s scientific mantissa and exponent, taking the Rational by reference.

When $x$ is positive, we can write $x = 2^{e_s}m_s$, where $e_s$ is an integer and $m_s$ is a rational number with $1 \leq m_s < 2$. We represent the rational mantissa as a float. The conversion might not be exact, so we round to the nearest float using the provided rounding mode. If the rounding mode is Exact but the conversion is not exact, None is returned. $$ f(x, r) \approx \left (\frac{x}{2^{\lfloor \log_2 x \rfloor}}, \lfloor \log_2 x \rfloor\right ). $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::conversion::traits::SciMantissaAndExponent;
use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode;
use malachite_q::Rational;

let test = |n: Rational, rm: RoundingMode, out: Option<(f32, i64)>| {
    assert_eq!(
        (&n).sci_mantissa_and_exponent_with_rounding_ref(rm)
            .map(|(m, e)| (NiceFloat(m), e)),
        out.map(|(m, e)| (NiceFloat(m), e))
    );
};
test(Rational::from(3u32), RoundingMode::Down, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Ceiling, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Up, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Nearest, Some((1.5, 1)));
test(Rational::from(3u32), RoundingMode::Exact, Some((1.5, 1)));

test(Rational::from_signeds(1, 3), RoundingMode::Floor, Some((1.3333333, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Down, Some((1.3333333, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Ceiling, Some((1.3333334, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Up, Some((1.3333334, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Nearest, Some((1.3333334, -2)));
test(Rational::from_signeds(1, 3), RoundingMode::Exact, None);

Mutates the numerator of a Rational using a provided closure, and then returns whatever the closure returns.

After the closure executes, this function reduces the Rational.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::One;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let mut q = Rational::from_signeds(22, 7);
let ret = q.mutate_numerator(|x| {
    *x -= Natural::ONE;
    true
});
assert_eq!(q, 3);
assert_eq!(ret, true);

Mutates the denominator of a Rational using a provided closure.

After the closure executes, this function reduces the Rational.

Panics

Panics if the closure sets the denominator to zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::One;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let mut q = Rational::from_signeds(22, 7);
let ret = q.mutate_denominator(|x| {
    *x -= Natural::ONE;
    true
});
assert_eq!(q.to_string(), "11/3");
assert_eq!(ret, true);

Mutates the numerator and denominator of a Rational using a provided closure.

After the closure executes, this function reduces the Rational.

Panics

Panics if the closure sets the denominator to zero.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::basic::traits::One;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

let mut q = Rational::from_signeds(22, 7);
let ret = q.mutate_numerator_and_denominator(|x, y| {
    *x -= Natural::ONE;
    *y -= Natural::ONE;
    true
});
assert_eq!(q.to_string(), "7/2");
assert_eq!(ret, true);

Converts a string, possibly in scientfic notation, to a Rational. This function finds the simplest Rational which rounds to the target string according to the precision implied by the string.

Use FromSciStringOptions to specify the base (from 2 to 36, inclusive). The rounding mode option is ignored.

If the base is greater than 10, the higher digits are represented by the letters 'a' through 'z' or 'A' through 'Z'; the case doesn’t matter and doesn’t need to be consistent.

Exponents are allowed, and are indicated using the character 'e' or 'E'. If the base is 15 or greater, an ambiguity arises where it may not be clear whether 'e' is a digit or an exponent indicator. To resolve this ambiguity, always use a '+' or '-' sign after the exponent indicator when the base is 15 or greater.

The exponent itself is always parsed using base 10.

Decimal (or other-base) points are allowed.

If the string is unparseable, None is returned.

Here’s a more precise description of the function’s behavior. Suppose we are using base $b$, and the literal value of the string (as parsed by from_sci_string) is $q$, and the implied scale is $s$ (meaning $s$ digits are provided after the point; if the string is "123.456", then $s$ is 3). Then this function computes $\epsilon = b^{-s}/2$ and finds the simplest Rational in the closed interval $[q - \epsilon, q + \epsilon]$. The simplest Rational is the one with minimal denominator; if there are multiple such Rationals, the one with the smallest absolute numerator is chosen.

The following discussion assumes base 10.

This method allows the function to convert "0.333" to $1/3$, since $1/3$ is the simplest Rational in the interval $[0.3325, 0.3335]$. But note that if the scale of the input is low, some unexpected behavior may occur. For example, "0.1" will be converted into $1/7$ rather than $1/10$, since $1/7$ is the simplest Rational in $[0.05, 0.15]$. If you’d prefer that result be $1/10$, you have a few options:

Worst-case complexity

$T(n, m) = O(m^n n \log m (\log n + \log\log m))$

$M(n, m) = O(m^n n \log m)$

where $T$ is time, $M$ is additional memory, $n$ is s.len(), and $m$ is options.base.

Examples
extern crate malachite_base;

use malachite_base::num::conversion::string::options::FromSciStringOptions;
use malachite_base::rounding_modes::RoundingMode;
use malachite_q::Rational;

let mut options = FromSciStringOptions::default();
options.set_base(16);
assert_eq!(Rational::from_sci_string_simplest_with_options("ff", options).unwrap(), 255);
assert_eq!(
    Rational::from_sci_string_simplest_with_options("ffE+5", options).unwrap(),
    267386880
);
// 1/4105 is 0.000ff705..._16
assert_eq!(
    Rational::from_sci_string_simplest_with_options("ffE-5", options).unwrap().to_string(),
    "1/4105"
);

Converts a string, possibly in scientfic notation, to a Rational. This function finds the simplest Rational which rounds to the target string according to the precision implied by the string.

The string is parsed using base 10. To use other bases, try from_sci_string_simplest_with_options instead.

Exponents are allowed, and are indicated using the character 'e' or 'E'.

The exponent itself is also parsed using base 10.

Decimal points are allowed.

If the string is unparseable, None is returned.

Here’s a more precise description of the function’s behavior. Suppose that the literal value of the string (as parsed by from_sci_string) is $q$, and the implied scale is $s$ (meaning $s$ digits are provided after the point; if the string is "123.456", then $s$ is 3). Then this function computes $\epsilon = 10^{-s}/2$ and finds the simplest Rational in the closed interval $[q - \epsilon, q + \epsilon]$. The simplest Rational is the one with minimal denominator; if there are multiple such Rationals, the one with the smallest absolute numerator is chosen.

This method allows the function to convert "0.333" to $1/3$, since $1/3$ is the simplest Rational in the interval $[0.3325, 0.3335]$. But note that if the scale of the input is low, some unexpected behavior may occur. For example, "0.1" will be converted into $1/7$ rather than $1/10$, since $1/7$ is the simplest Rational in $[0.05, 0.15]$. If you’d prefer that result be $1/10$, you have a few options:

Worst-case complexity

$T(n) = O(10^n n \log n)$

$M(n) = O(10^n n)$

where $T$ is time, $M$ is additional memory, and $n$ is s.len().

Examples
use malachite_q::Rational;

assert_eq!(Rational::from_sci_string_simplest("123").unwrap(), 123);
assert_eq!(Rational::from_sci_string_simplest("0.1").unwrap().to_string(), "1/7");
assert_eq!(Rational::from_sci_string_simplest("0.10").unwrap().to_string(), "1/10");
assert_eq!(Rational::from_sci_string_simplest("0.333").unwrap().to_string(), "1/3");
assert_eq!(Rational::from_sci_string_simplest("1.2e5").unwrap(), 120000);
assert_eq!(Rational::from_sci_string_simplest("1.2e-5").unwrap().to_string(), "1/80000");

When expanding a Rational in a small base $b$, determines how many digits after the decimal (or other-base) point are in the base-$b$ expansion.

If the expansion is non-terminating, this method returns None. This happens iff the Rational’s denominator has prime factors not present in $b$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if base is less than 2 or greater than 36.

Examples
use malachite_q::Rational;

// 3/8 is 0.375 in base 10.
assert_eq!(Rational::from_signeds(3, 8).length_after_point_in_small_base(10), Some(3));
// 1/20 is 0.05 in base 10.
assert_eq!(Rational::from_signeds(1, 20).length_after_point_in_small_base(10), Some(2));
// 1/7 is non-terminating in base 10.
assert_eq!(Rational::from_signeds(1, 7).length_after_point_in_small_base(10), None);
// 1/7 is 0.3 in base 21.
assert_eq!(Rational::from_signeds(1, 7).length_after_point_in_small_base(21), Some(1));

Extracts the numerator of a Rational, taking the Rational by reference and cloning.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(Rational::from_str("2/3").unwrap().to_numerator(), 2);
assert_eq!(Rational::from_str("0").unwrap().to_numerator(), 0);

Extracts the denominator of a Rational, taking the Rational by reference and cloning.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(Rational::from_str("2/3").unwrap().to_denominator(), 3);
assert_eq!(Rational::from_str("0").unwrap().to_denominator(), 1);

Extracts the numerator and denominator of a Rational, taking the Rational by reference and cloning.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::strings::ToDebugString;
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(
    Rational::from_str("2/3").unwrap().to_numerator_and_denominator().to_debug_string(),
    "(2, 3)"
);
assert_eq!(
    Rational::from_str("0").unwrap().to_numerator_and_denominator().to_debug_string(),
    "(0, 1)"
);

Extracts the numerator of a Rational, taking the Rational by value.

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(Rational::from_str("2/3").unwrap().into_numerator(), 2);
assert_eq!(Rational::from_str("0").unwrap().into_numerator(), 0);

Extracts the denominator of a Rational, taking the Rational by value.

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(Rational::from_str("2/3").unwrap().into_denominator(), 3);
assert_eq!(Rational::from_str("0").unwrap().into_denominator(), 1);

Extracts the numerator and denominator of a Rational, taking the Rational by value.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::strings::ToDebugString;
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(
    Rational::from_str("2/3").unwrap().into_numerator_and_denominator().to_debug_string(),
    "(2, 3)"
);
assert_eq!(
    Rational::from_str("0").unwrap().into_numerator_and_denominator().to_debug_string(),
    "(0, 1)"
);

Returns a reference to the numerator of a Rational.

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(*Rational::from_str("2/3").unwrap().numerator_ref(), 2);
assert_eq!(*Rational::from_str("0").unwrap().numerator_ref(), 0);

Returns a reference to the denominator of a Rational.

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(*Rational::from_str("2/3").unwrap().denominator_ref(), 3);
assert_eq!(*Rational::from_str("0").unwrap().denominator_ref(), 1);

Returns references to the numeraror and denominator of a Rational.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::strings::ToDebugString;
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(
    Rational::from_str("2/3").unwrap().numerator_and_denominator_ref().to_debug_string(),
    "(2, 3)"
);
assert_eq!(
    Rational::from_str("0").unwrap().numerator_and_denominator_ref().to_debug_string(),
    "(0, 1)"
);

Trait Implementations

Takes the absolute value of a Rational, taking the Rational by value.

$$ f(x) = |x|. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Abs;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!((&Rational::ZERO).abs(), 0);
assert_eq!((&Rational::from_signeds(22, 7)).abs().to_string(), "22/7");
assert_eq!((&Rational::from_signeds(-22, 7)).abs().to_string(), "22/7");

Takes the absolute value of a Rational, taking the Rational by value.

$$ f(x) = |x|. $$

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Abs;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(Rational::ZERO.abs(), 0);
assert_eq!(Rational::from_signeds(22, 7).abs().to_string(), "22/7");
assert_eq!(Rational::from_signeds(-22, 7).abs().to_string(), "22/7");

Replaces a Rational with its absolute value.

$$ x \gets |x|. $$

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::AbsAssign;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

let mut x = Rational::ZERO;
x.abs_assign();
assert_eq!(x, 0);

let mut x = Rational::from_signeds(22, 7);
x.abs_assign();
assert_eq!(x.to_string(), "22/7");

let mut x = Rational::from_signeds(-22, 7);
x.abs_assign();
assert_eq!(x.to_string(), "22/7");

Adds two Rationals, taking both by reference.

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

assert_eq!(&Rational::ONE_HALF + &Rational::ONE_HALF, 1);
assert_eq!(
    (&Rational::from_signeds(22, 7) + &Rational::from_signeds(99, 100)).to_string(),
    "2893/700"
);
The resulting type after applying the + operator.

Adds two Rationals, taking both by the first by value and the second by reference.

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

assert_eq!(Rational::ONE_HALF + &Rational::ONE_HALF, 1);
assert_eq!(
    (Rational::from_signeds(22, 7) + &Rational::from_signeds(99, 100)).to_string(),
    "2893/700"
);
The resulting type after applying the + operator.

Adds two Rationals, taking the first by reference and the second by value

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

assert_eq!(&Rational::ONE_HALF + Rational::ONE_HALF, 1);
assert_eq!(
    (&Rational::from_signeds(22, 7) + Rational::from_signeds(99, 100)).to_string(),
    "2893/700"
);
The resulting type after applying the + operator.

Adds two Rationals, taking both by value.

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

assert_eq!(Rational::ONE_HALF + Rational::ONE_HALF, 1);
assert_eq!(
    (Rational::from_signeds(22, 7) + Rational::from_signeds(99, 100)).to_string(),
    "2893/700"
);
The resulting type after applying the + operator.

Adds a Rational to a Rational in place, taking the Rational on the right-hand side by reference.

$$ x \gets x + y. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

let mut x = Rational::ONE_HALF;
x += &Rational::ONE_HALF;
assert_eq!(x, 1);

let mut x = Rational::from_signeds(22, 7);
x += &Rational::from_signeds(99, 100);
assert_eq!(x.to_string(), "2893/700");

Adds a Rational to a Rational in place, taking the Rational on the right-hand side by value.

$$ x \gets x + y. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

let mut x = Rational::ONE_HALF;
x += Rational::ONE_HALF;
assert_eq!(x, 1);

let mut x = Rational::from_signeds(22, 7);
x += Rational::from_signeds(99, 100);
assert_eq!(x.to_string(), "2893/700");

Finds the best approximation of a Rational using a denominator no greater than a specified maximum, taking the Rational by reference.

Let $f(x, d) = p/q$, with $p$ and $q$ relatively prime. Then the following properties hold:

  • $q \leq d$
  • For all $n \in \Z$ and all $m \in \Z$ with $0 < m \leq d$, $|x - p/q| \leq |x - n/m|$.
  • If $|x - n/m| = |x - p/q|$, then $q \leq m$.
  • If $|x - n/q| = |x - p/q|$, then $p$ is even and $n$ is either equal to $p$ or odd.
Worst-case complexity

$T(n) = O(n^2 \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics
  • If max_denominator is zero.
Examples
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_nz::natural::Natural;
use malachite_q::arithmetic::traits::Approximate;
use malachite_q::Rational;

assert_eq!(
    (&Rational::exact_from(std::f64::consts::PI)).approximate(&Natural::from(1000u32))
            .to_string(),
    "355/113"
);
assert_eq!(
    (&Rational::from_signeds(333i32, 1000)).approximate(&Natural::from(100u32))
            .to_string(),
    "1/3"
);
Implementation notes

This algorithm follows the description in https://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations. One part of the algorithm not mentioned in that article is that if the last term $n$ in the continued fraction needs to be reduced, the optimal replacement term $m$ may be found using division.

Finds the best approximation of a Rational using a denominator no greater than a specified maximum, taking the Rational by value.

Let $f(x, d) = p/q$, with $p$ and $q$ relatively prime. Then the following properties hold:

  • $q \leq d$
  • For all $n \in \Z$ and all $m \in \Z$ with $0 < m \leq d$, $|x - p/q| \leq |x - n/m|$.
  • If $|x - n/m| = |x - p/q|$, then $q \leq m$.
  • If $|x - n/q| = |x - p/q|$, then $p$ is even and $n$ is either equal to $p$ or odd.
Worst-case complexity

$T(n) = O(n^2 \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics
  • If max_denominator is zero.
Examples
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_nz::natural::Natural;
use malachite_q::arithmetic::traits::Approximate;
use malachite_q::Rational;

assert_eq!(
    Rational::exact_from(std::f64::consts::PI).approximate(&Natural::from(1000u32))
        .to_string(),
    "355/113"
);
assert_eq!(
    Rational::from_signeds(333i32, 1000).approximate(&Natural::from(100u32)).to_string(),
    "1/3"
);
Implementation notes

This algorithm follows the description in https://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations. One part of the algorithm not mentioned in that article is that if the last term $n$ in the continued fraction needs to be reduced, the optimal replacement term $m$ may be found using division.

Finds the best approximation of a Rational using a denominator no greater than a specified maximum, mutating the Rational in place.

See Rational::approximate for more information.

Worst-case complexity

$T(n) = O(n^2 \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics
  • If max_denominator is zero.
Examples
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_nz::natural::Natural;
use malachite_q::arithmetic::traits::ApproximateAssign;
use malachite_q::Rational;

let mut x = Rational::exact_from(std::f64::consts::PI);
x.approximate_assign(&Natural::from(1000u32));
assert_eq!(x.to_string(), "355/113");

let mut x = Rational::from_signeds(333i32, 1000);
x.approximate_assign(&Natural::from(100u32));
assert_eq!(x.to_string(), "1/3");

Finds the ceiling of a Rational, taking the Rational by reference.

$$ f(x) = \lceil x \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Ceiling;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!((&Rational::ZERO).ceiling(), 0);
assert_eq!((&Rational::from_signeds(22, 7)).ceiling(), 4);
assert_eq!((&Rational::from_signeds(-22, 7)).ceiling(), -3);

Finds the ceiling of a Rational, taking the Rational by value.

$$ f(x) = \lceil x \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Ceiling;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(Rational::ZERO.ceiling(), 0);
assert_eq!(Rational::from_signeds(22, 7).ceiling(), 4);
assert_eq!(Rational::from_signeds(-22, 7).ceiling(), -3);

Replaces a Rational with its ceiling.

$$ x \gets \lceil x \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingAssign;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

let mut x = Rational::ZERO;
x.ceiling_assign();
assert_eq!(x, 0);

let mut x = Rational::from_signeds(22, 7);
x.ceiling_assign();
assert_eq!(x, 4);

let mut x = Rational::from_signeds(-22, 7);
x.ceiling_assign();
assert_eq!(x, -3);

Returns the ceiling of the base-$b$ logarithm of a positive Rational.

Note that this function may be slow if the base is very close to 1.

$f(x, b) = \lceil\log_b x\rceil$.

Worst-case complexity

$T(n, m) = O(nm \log (nm) \log\log (nm))$

$M(n, m) = O(nm \log (nm))$

where $T$ is time, $M$ is additional memory, $n$ is base.significant_bits(), and $m$ is $|\log_b x|$, where $b$ is base and $x$ is x.

Panics

Panics if self less than or equal to zero or base is 1.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingLogBase;
use malachite_q::Rational;

assert_eq!(Rational::from(80u32).ceiling_log_base(&Rational::from(3u32)), 4);
assert_eq!(Rational::from(81u32).ceiling_log_base(&Rational::from(3u32)), 4);
assert_eq!(Rational::from(82u32).ceiling_log_base(&Rational::from(3u32)), 5);
assert_eq!(Rational::from(4294967296u64).ceiling_log_base(&Rational::from(10u32)), 10);
assert_eq!(
    Rational::from_signeds(936851431250i64, 1397).ceiling_log_base(&Rational::from(10u32)),
    9
);
assert_eq!(
    Rational::from_signeds(5153632, 16807)
            .ceiling_log_base(&Rational::from_signeds(22, 7)),
    5
);

Returns the ceiling of the base-2 logarithm of a positive Rational.

$f(x) = \lfloor\log_2 x\rfloor$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self less than or equal to zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingLogBase2;
use malachite_q::Rational;

assert_eq!(Rational::from(3u32).ceiling_log_base_2(), 2);
assert_eq!(Rational::from_signeds(1, 3).ceiling_log_base_2(), -1);
assert_eq!(Rational::from_signeds(1, 4).ceiling_log_base_2(), -2);
assert_eq!(Rational::from_signeds(1, 5).ceiling_log_base_2(), -2);

Returns the ceiling of the base-$2^k$ logarithm of a positive Rational.

$k$ may be negative.

$f(x, p) = \lceil\log_{2^p} x\rceil$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is less than or equal to 0 or pow is 0.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingLogBasePowerOf2;
use malachite_q::Rational;

assert_eq!(Rational::from(100).ceiling_log_base_power_of_2(2), 4);
assert_eq!(Rational::from(4294967296u64).ceiling_log_base_power_of_2(8), 4);

// 4^(-2) < 1/10 < 4^(-1)
assert_eq!(Rational::from_signeds(1, 10).ceiling_log_base_power_of_2(2), -1);
// (1/4)^2 < 1/10 < (1/4)^1
assert_eq!(Rational::from_signeds(1, 10).ceiling_log_base_power_of_2(-2), 2);

Returns the base-$b$ logarithm of a positive Rational. If the Rational is not a power of $b$, then None is returned.

Note that this function may be slow if the base is very close to 1.

$$ f(x, b) = \begin{cases} \operatorname{Some}(\log_b x) & \text{if} \quad \log_b x \in \Z, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n, m) = O(nm \log (nm) \log\log (nm))$

$M(n, m) = O(nm \log (nm))$

where $T$ is time, $M$ is additional memory, $n$ is base.significant_bits(), and $m$ is $|\log_b x|$, where $b$ is base and $x$ is x.

Panics

Panics if self less than or equal to zero or base is 1.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedLogBase;
use malachite_q::Rational;

assert_eq!(Rational::from(80u32).checked_log_base(&Rational::from(3u32)), None);
assert_eq!(Rational::from(81u32).checked_log_base(&Rational::from(3u32)), Some(4));
assert_eq!(Rational::from(82u32).checked_log_base(&Rational::from(3u32)), None);
assert_eq!(Rational::from(4294967296u64).checked_log_base(&Rational::from(10u32)), None);
assert_eq!(
    Rational::from_signeds(936851431250i64, 1397).checked_log_base(&Rational::from(10u32)),
    None
);
assert_eq!(
    Rational::from_signeds(5153632, 16807)
            .checked_log_base(&Rational::from_signeds(22, 7)),
    Some(5)
);

Returns the base-2 logarithm of a positive Rational. If the Rational is not a power of 2, then None is returned.

$$ f(x) = \begin{cases} \operatorname{Some}(\log_2 x) & \text{if} \quad \log_2 x \in \Z, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is less than or equal to zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedLogBase2;
use malachite_q::Rational;

assert_eq!(Rational::from(3u32).checked_log_base_2(), None);
assert_eq!(Rational::from_signeds(1, 3).checked_log_base_2(), None);
assert_eq!(Rational::from_signeds(1, 4).checked_log_base_2(), Some(-2));
assert_eq!(Rational::from_signeds(1, 5).checked_log_base_2(), None);

Returns the base-$2^k$ logarithm of a positive Rational. If the Rational is not a power of $2^k$, then None is returned.

$k$ may be negative.

$$ f(x, p) = \begin{cases} \operatorname{Some}(\log_{2^p} x) & \text{if} \quad \log_{2^p} x \in \Z, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is 0 or pow is 0.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedLogBasePowerOf2;
use malachite_q::Rational;

assert_eq!(Rational::from(100).checked_log_base_power_of_2(2), None);
assert_eq!(Rational::from(4294967296u64).checked_log_base_power_of_2(8), Some(4));

// 4^(-2) < 1/10 < 4^(-1)
assert_eq!(Rational::from_signeds(1, 10).checked_log_base_power_of_2(2), None);
assert_eq!(Rational::from_signeds(1, 16).checked_log_base_power_of_2(2), Some(-2));
// (1/4)^2 < 1/10 < (1/4)^1
assert_eq!(Rational::from_signeds(1, 10).checked_log_base_power_of_2(-2), None);
assert_eq!(Rational::from_signeds(1, 16).checked_log_base_power_of_2(-2), Some(2));

Returns the the $n$th root of a Rational, or None if the Rational is not a perfect $n$th power. The Rational is taken by reference.

$$ f(x, n) = \begin{cases} \operatorname{Some}(sqrt[n]{x}) & \text{if} \quad \sqrt[n]{x} \in \mathbb{Q}, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, if exp is even and self is negative, or if self is zero and exp is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedRoot;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

assert_eq!((&Rational::from(999i32)).checked_root(3i64).to_debug_string(), "None");
assert_eq!((&Rational::from(1000i32)).checked_root(3i64).to_debug_string(), "Some(10)");
assert_eq!((&Rational::from(1001i32)).checked_root(3i64).to_debug_string(), "None");
assert_eq!((&Rational::from(-1000i32)).checked_root(3i64).to_debug_string(), "Some(-10)");
assert_eq!((&Rational::from_signeds(22, 7)).checked_root(3i64).to_debug_string(), "None");
assert_eq!(
    (&Rational::from_signeds(27, 8)).checked_root(3i64).to_debug_string(),
    "Some(3/2)"
);
assert_eq!(
    (&Rational::from_signeds(-27, 8)).checked_root(3i64).to_debug_string(),
    "Some(-3/2)"
);

assert_eq!((&Rational::from(1000i32)).checked_root(-3i64).to_debug_string(), "Some(1/10)");
assert_eq!(
    (&Rational::from_signeds(-27, 8)).checked_root(-3i64).to_debug_string(),
    "Some(-2/3)"
);

Returns the the $n$th root of a Rational, or None if the Rational is not a perfect $n$th power. The Rational is taken by value.

$$ f(x, n) = \begin{cases} \operatorname{Some}(sqrt[n]{x}) & \text{if} \quad \sqrt[n]{x} \in \mathbb{Q}, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, if exp is even and self is negative, or if self is zero and exp is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedRoot;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

assert_eq!(Rational::from(999i32).checked_root(3i64).to_debug_string(), "None");
assert_eq!(Rational::from(1000i32).checked_root(3i64).to_debug_string(), "Some(10)");
assert_eq!(Rational::from(1001i32).checked_root(3i64).to_debug_string(), "None");
assert_eq!(Rational::from(-1000i32).checked_root(3i64).to_debug_string(), "Some(-10)");
assert_eq!(Rational::from_signeds(22, 7).checked_root(3i64).to_debug_string(), "None");
assert_eq!(
    Rational::from_signeds(27, 8).checked_root(3i64).to_debug_string(),
    "Some(3/2)"
);
assert_eq!(
    Rational::from_signeds(-27, 8).checked_root(3i64).to_debug_string(),
    "Some(-3/2)"
);

assert_eq!(Rational::from(1000i32).checked_root(-3i64).to_debug_string(), "Some(1/10)");
assert_eq!(
    Rational::from_signeds(-27, 8).checked_root(-3i64).to_debug_string(),
    "Some(-2/3)"
);

Returns the the $n$th root of a Rational, or None if the Rational is not a perfect $n$th power. The Rational is taken by reference.

$$ f(x, n) = \begin{cases} \operatorname{Some}(sqrt[n]{x}) & \text{if} \quad \sqrt[n]{x} \in \mathbb{Q}, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedRoot;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

assert_eq!(Rational::from(999i32).checked_root(3u64).to_debug_string(), "None");
assert_eq!(Rational::from(1000i32).checked_root(3u64).to_debug_string(), "Some(10)");
assert_eq!(Rational::from(1001i32).checked_root(3u64).to_debug_string(), "None");
assert_eq!(Rational::from(-1000i32).checked_root(3u64).to_debug_string(), "Some(-10)");
assert_eq!(Rational::from_signeds(22, 7).checked_root(3u64).to_debug_string(), "None");
assert_eq!(
    Rational::from_signeds(27, 8).checked_root(3u64).to_debug_string(),
    "Some(3/2)"
);
assert_eq!(
    Rational::from_signeds(-27, 8).checked_root(3u64).to_debug_string(),
    "Some(-3/2)"
);

Returns the the $n$th root of a Rational, or None if the Rational is not a perfect $n$th power. The Rational is taken by value.

$$ f(x, n) = \begin{cases} \operatorname{Some}(sqrt[n]{x}) & \text{if} \quad \sqrt[n]{x} \in \mathbb{Q}, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedRoot;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

assert_eq!(Rational::from(999i32).checked_root(3u64).to_debug_string(), "None");
assert_eq!(Rational::from(1000i32).checked_root(3u64).to_debug_string(), "Some(10)");
assert_eq!(Rational::from(1001i32).checked_root(3u64).to_debug_string(), "None");
assert_eq!(Rational::from(-1000i32).checked_root(3u64).to_debug_string(), "Some(-10)");
assert_eq!(Rational::from_signeds(22, 7).checked_root(3u64).to_debug_string(), "None");
assert_eq!(
    Rational::from_signeds(27, 8).checked_root(3u64).to_debug_string(),
    "Some(3/2)"
);
assert_eq!(
    Rational::from_signeds(-27, 8).checked_root(3u64).to_debug_string(),
    "Some(-3/2)"
);

Returns the the square root of a Rational, or None if it is not a perfect square. The Rational is taken by reference.

$$ f(x) = \begin{cases} \operatorname{Some}(sqrt{x}) & \text{if} \quad \sqrt{x} \in \mathbb{Q}, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedSqrt;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

assert_eq!((&Rational::from(99u8)).checked_sqrt().to_debug_string(), "None");
assert_eq!((&Rational::from(100u8)).checked_sqrt().to_debug_string(), "Some(10)");
assert_eq!((&Rational::from(101u8)).checked_sqrt().to_debug_string(), "None");
assert_eq!((&Rational::from_signeds(22, 7)).checked_sqrt().to_debug_string(), "None");
assert_eq!((&Rational::from_signeds(25, 9)).checked_sqrt().to_debug_string(), "Some(5/3)");

Returns the the square root of a Rational, or None if it is not a perfect square. The Rational is taken by value.

$$ f(x) = \begin{cases} \operatorname{Some}(sqrt{x}) & \text{if} \quad \sqrt{x} \in \mathbb{Q}, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedSqrt;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

assert_eq!(Rational::from(99u8).checked_sqrt().to_debug_string(), "None");
assert_eq!(Rational::from(100u8).checked_sqrt().to_debug_string(), "Some(10)");
assert_eq!(Rational::from(101u8).checked_sqrt().to_debug_string(), "None");
assert_eq!(Rational::from_signeds(22, 7).checked_sqrt().to_debug_string(), "None");
assert_eq!(Rational::from_signeds(25, 9).checked_sqrt().to_debug_string(), "Some(5/3)");
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Returns the continued fraction of a Rational, taking the Rational by reference.

The output has two components. The first is the first value of the continued fraction, which may be any Integer and is equal to the floor of the Rational. The second is an iterator that produces the remaining values, which are all positive. Using the standard notation for continued fractions, the first value is the number before the semicolon, and the second value produces the remaining numbers.

Each rational number has two continued fraction representations. The shorter of the two representations (the one that does not end in 1) is returned.

$f(x) = (a_0, (a_1, a_2, \ldots, a_3)),$ where $x = [a_0; a_1, a_2, \ldots, a_3]$ and $a_3 \neq 1$.

The output length is $O(n)$, where $n$ is self.significant_bits().

Worst-case complexity per iteration

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate itertools;
extern crate malachite_base;

use itertools::Itertools;
use malachite_base::strings::ToDebugString;
use malachite_q::conversion::traits::ContinuedFraction;
use malachite_q::Rational;

let (head, tail) = (&Rational::from_signeds(2, 3)).continued_fraction();
let tail = tail.collect_vec();
assert_eq!(head, 0);
assert_eq!(tail.to_debug_string(), "[1, 2]");

let (head, tail) = (&Rational::from_signeds(355, 113)).continued_fraction();
let tail = tail.collect_vec();
assert_eq!(head, 3);
assert_eq!(tail.to_debug_string(), "[7, 16]");

Returns the continued fraction of a Rational, taking the Rational by value.

The output has two components. The first is the first value of the continued fraction, which may be any Integer and is equal to the floor of the Rational. The second is an iterator that produces the remaining values, which are all positive. Using the standard notation for continued fractions, the first value is the number before the semicolon, and the second value produces the remaining numbers.

Each rational number has two continued fraction representations. The shorter of the two representations (the one that does not end in 1) is returned.

$f(x) = (a_0, (a_1, a_2, \ldots, a_3)),$ where $x = [a_0; a_1, a_2, \ldots, a_3]$ and $a_3 \neq 1$.

The output length is $O(n)$, where $n$ is self.significant_bits().

Worst-case complexity per iteration

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate itertools;
extern crate malachite_base;

use itertools::Itertools;
use malachite_base::strings::ToDebugString;
use malachite_q::conversion::traits::ContinuedFraction;
use malachite_q::Rational;

let (head, tail) = Rational::from_signeds(2, 3).continued_fraction();
let tail = tail.collect_vec();
assert_eq!(head, 0);
assert_eq!(tail.to_debug_string(), "[1, 2]");

let (head, tail) = Rational::from_signeds(355, 113).continued_fraction();
let tail = tail.collect_vec();
assert_eq!(head, 3);
assert_eq!(tail.to_debug_string(), "[7, 16]");

Returns the convergents of a Rational, taking the Rational by reference.

The convergents of a number are the sequence of rational numbers whose continued fractions are the prefixes of the number’s continued fraction. The first convergent is the floor of the number. The sequence of convergents is finite iff the number is rational, in which case the last convergent is the number itself. Each convergent is closer to the number than the previous convergent is. The even-indexed convergents are less than or equal to the number, and the odd-indexed ones are greater than or equal to it.

$f(x) = ([a_0; a_1, a_2, \ldots, a_i])_{i=0}^{n}$, where $x = [a_0; a_1, a_2, \ldots, a_n]$ and $a_n \neq 1$.

The output length is $O(n)$, where $n$ is self.significant_bits().

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate itertools;
extern crate malachite_base;

use itertools::Itertools;
use malachite_base::strings::ToDebugString;
use malachite_q::conversion::traits::Convergents;
use malachite_q::Rational;

assert_eq!(
    (&Rational::from_signeds(2, 3)).convergents().collect_vec().to_debug_string(),
    "[0, 1, 2/3]"
);
assert_eq!(
    (&Rational::from_signeds(355, 113)).convergents().collect_vec().to_debug_string(),
    "[3, 22/7, 355/113]",
);

Returns the convergents of a Rational, taking the Rational by value.

The convergents of a number are the sequence of rational numbers whose continued fractions are the prefixes of the number’s continued fraction. The first convergent is the floor of the number. The sequence of convergents is finite iff the number is rational, in which case the last convergent is the number itself. Each convergent is closer to the number than the previous convergent is. The even-indexed convergents are less than or equal to the number, and the odd-indexed ones are greater than or equal to it.

$f(x) = ([a_0; a_1, a_2, \ldots, a_i])_{i=0}^{n}$, where $x = [a_0; a_1, a_2, \ldots, a_n]$ and $a_n \neq 1$.

The output length is $O(n)$, where $n$ is self.significant_bits().

Worst-case complexity per iteration

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate itertools;
extern crate malachite_base;

use itertools::Itertools;
use malachite_base::strings::ToDebugString;
use malachite_q::conversion::traits::Convergents;
use malachite_q::Rational;

assert_eq!(
    Rational::from_signeds(2, 3).convergents().collect_vec().to_debug_string(),
    "[0, 1, 2/3]"
);
assert_eq!(
    Rational::from_signeds(355, 113).convergents().collect_vec().to_debug_string(),
    "[3, 22/7, 355/113]",
);

Determines whether a Rational can be converted to an Integer, taking the Rational by reference.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert_eq!(Integer::convertible_from(&Rational::from(123)), true);
assert_eq!(Integer::convertible_from(&Rational::from(-123)), true);
assert_eq!(Integer::convertible_from(&Rational::from_signeds(22, 7)), false);

Determines whether a Rational can be converted to a Natural (when the Rational is non-negative and an integer), taking the Rational by reference.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert_eq!(Natural::convertible_from(&Rational::from(123)), true);
assert_eq!(Natural::convertible_from(&Rational::from(-123)), false);
assert_eq!(Natural::convertible_from(&Rational::from_signeds(22, 7)), false);

Determines whether a Rational can be exactly converted to a primitive float, taking the Rational by reference.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples

See here.

Determines whether a Rational can be exactly converted to a primitive float, taking the Rational by reference.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples

See here.

Determines whether a Rational can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a Rational can be exactly converted to a primitive float, taking the Rational by value.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples

See here.

Determines whether a Rational can be exactly converted to a primitive float, taking the Rational by value.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples

See here.

Converts a Rational to a String.

This is the same implementation as for Display.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::strings::ToDebugString;
use malachite_q::Rational;

assert_eq!(Rational::ZERO.to_debug_string(), "0");
assert_eq!(Rational::from(123).to_debug_string(), "123");
assert_eq!(Rational::from_signeds(22, 7).to_debug_string(), "22/7");

The default value of a Rational, 0.

Returns an iterator of all denominators that appear in the Rationals contained in a closed interval.

For example, consider the interval $[1/3, 1/2]$. It contains no integers, so no Rationals with denominator 1. It does contain Rationals with denominators 2 and 3 (the endpoints). It contains none with denominator 4, but it does contain $2/5$. It contains none with denominator 6 (though $1/3$ and $1/2$ are $2/6$ and $3/6$, those representations are not reduced). It contains $3/7$, $3/8$, and $4/9$ but none with denominator 10 ($0.4$ does not count because it is $2/5$). It contains all denominators greater than 10, so the complete list is $2, 3, 5, 7, 8, 9, 11, 12, 13, \ldots$.

Worst-case complexity per iteration

$T(n, i) = O(n + \log i)$

$M(n, i) = O(n + \log i)$

where $T$ is time, $M$ is additional memory, $i$ is the iteration number, and $n$ is max(a.significant_bits(), b.significant_bits()).

Panics

Panics if $a \geq b$.

extern crate malachite_base;

use malachite_base::iterators::prefix_to_string;
use malachite_base::num::basic::traits::{One, Two};
use malachite_q::arithmetic::traits::DenominatorsInClosedInterval;
use malachite_q::Rational;

assert_eq!(
    prefix_to_string(
        Rational::denominators_in_closed_interval(
            &Rational::ONE,
            &Rational::TWO
        ),
        20
    ),
    "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...]"
);
assert_eq!(
    prefix_to_string(
        Rational::denominators_in_closed_interval(
            &Rational::from_signeds(1, 3),
            &Rational::from_signeds(1, 2)
        ),
        20
    ),
    "[2, 3, 5, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, ...]"
);
assert_eq!(
    prefix_to_string(
        Rational::denominators_in_closed_interval(
            &Rational::from_signeds(1, 1000001),
            &Rational::from_signeds(1, 1000000)
        ),
        20
    ),
    "[1000000, 1000001, 3000001, 3000002, 4000001, 4000003, 5000001, 5000002, 5000003, \
    5000004, 6000001, 6000005, 7000001, 7000002, 7000003, 7000004, 7000005, 7000006, \
    8000001, 8000003, ...]"
);

Converts a Rational to a String.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(Rational::ZERO.to_string(), "0");
assert_eq!(Rational::from(123).to_string(), "123");
assert_eq!(Rational::from_str("22/7").unwrap().to_string(), "22/7");

Divides a Rational by another Rational, taking both by reference.

$$ f(x, y) = \frac{x}{y}. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if the second Rational is zero.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Two;
use malachite_q::Rational;

assert_eq!(&Rational::TWO / &Rational::TWO, 1);
assert_eq!(
    (&Rational::from_signeds(22, 7) / &Rational::from_signeds(99, 100)).to_string(),
    "200/63"
);
The resulting type after applying the / operator.

Divides a Rational by another Rational, taking the first by value and the second by reference.

$$ f(x, y) = \frac{x}{y}. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if the second Rational is zero.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Two;
use malachite_q::Rational;

assert_eq!(Rational::TWO / &Rational::TWO, 1);
assert_eq!(
    (Rational::from_signeds(22, 7) / &Rational::from_signeds(99, 100)).to_string(),
    "200/63"
);
The resulting type after applying the / operator.

Divides a Rational by another Rational, taking the first by reference and the second by value.

$$ f(x, y) = \frac{x}{y}. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if the second Rational is zero.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Two;
use malachite_q::Rational;

assert_eq!(&Rational::TWO / Rational::TWO, 1);
assert_eq!(
    (&Rational::from_signeds(22, 7) / Rational::from_signeds(99, 100)).to_string(),
    "200/63"
);
The resulting type after applying the / operator.

Divides a Rational by another Rational, taking both by value.

$$ f(x, y) = \frac{x}{y}. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if the second Rational is zero.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Two;
use malachite_q::Rational;

assert_eq!(Rational::TWO / Rational::TWO, 1);
assert_eq!(
    (Rational::from_signeds(22, 7) / Rational::from_signeds(99, 100)).to_string(),
    "200/63"
);
The resulting type after applying the / operator.

Divides a Rational by a Rational in place, taking the Rational on the right-hand side by reference.

$$ x \gets \frac{x}{y}. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if the second Rational is zero.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Two;
use malachite_q::Rational;

let mut x = Rational::TWO;
x /= &Rational::TWO;
assert_eq!(x, 1);

let mut x = Rational::from_signeds(22, 7);
x /= &Rational::from_signeds(99, 100);
assert_eq!(x.to_string(), "200/63");

Divides a Rational by a Rational in place, taking the Rational on the right-hand side by value.

$$ x \gets \frac{x}{y}. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if the second Rational is zero.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Two;
use malachite_q::Rational;

let mut x = Rational::TWO;
x /= Rational::TWO;
assert_eq!(x, 1);

let mut x = Rational::from_signeds(22, 7);
x /= Rational::from_signeds(99, 100);
assert_eq!(x.to_string(), "200/63");

Finds the floor of a Rational, taking the Rational by reference.

$$ f(x) = \lfloor x \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Floor;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!((&Rational::ZERO).floor(), 0);
assert_eq!((&Rational::from_signeds(22, 7)).floor(), 3);
assert_eq!((&Rational::from_signeds(-22, 7)).floor(), -4);

Finds the floor of a Rational, taking the Rational by value.

$$ f(x) = \lfloor x \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Floor;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(Rational::ZERO.floor(), 0);
assert_eq!(Rational::from_signeds(22, 7).floor(), 3);
assert_eq!(Rational::from_signeds(-22, 7).floor(), -4);

Replaces a Rational with its floor.

$$ x \gets \lfloor x \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorAssign;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;
use std::str::FromStr;

let mut x = Rational::ZERO;
x.floor_assign();
assert_eq!(x, 0);

let mut x = Rational::from_signeds(22, 7);
x.floor_assign();
assert_eq!(x, 3);

let mut x = Rational::from_signeds(-22, 7);
x.floor_assign();
assert_eq!(x, -4);

Returns the floor of the base-$b$ logarithm of a positive Rational.

Note that this function may be slow if the base is very close to 1.

$f(x, b) = \lfloor\log_b x\rfloor$.

Worst-case complexity

$T(n, m) = O(nm \log (nm) \log\log (nm))$

$M(n, m) = O(nm \log (nm))$

where $T$ is time, $M$ is additional memory, $n$ is base.significant_bits(), and $m$ is $|\log_b x|$, where $b$ is base and $x$ is x.

Panics

Panics if self less than or equal to zero or base is 1.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorLogBase;
use malachite_q::Rational;

assert_eq!(Rational::from(80u32).floor_log_base(&Rational::from(3u32)), 3);
assert_eq!(Rational::from(81u32).floor_log_base(&Rational::from(3u32)), 4);
assert_eq!(Rational::from(82u32).floor_log_base(&Rational::from(3u32)), 4);
assert_eq!(Rational::from(4294967296u64).floor_log_base(&Rational::from(10u32)), 9);
assert_eq!(
    Rational::from_signeds(936851431250i64, 1397).floor_log_base(&Rational::from(10u32)),
    8
);
assert_eq!(
    Rational::from_signeds(5153632, 16807).floor_log_base(&Rational::from_signeds(22, 7)),
    5
);

Returns the floor of the base-2 logarithm of a positive Rational.

$f(x) = \lfloor\log_2 x\rfloor$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self less than or equal to zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorLogBase2;
use malachite_q::Rational;

assert_eq!(Rational::from(3u32).floor_log_base_2(), 1);
assert_eq!(Rational::from_signeds(1, 3).floor_log_base_2(), -2);
assert_eq!(Rational::from_signeds(1, 4).floor_log_base_2(), -2);
assert_eq!(Rational::from_signeds(1, 5).floor_log_base_2(), -3);

Returns the floor of the base-$2^k$ logarithm of a positive Rational.

$k$ may be negative.

$f(x, k) = \lfloor\log_{2^k} x\rfloor$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is less than or equal to 0 or pow is 0.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorLogBasePowerOf2;
use malachite_q::Rational;

assert_eq!(Rational::from(100).floor_log_base_power_of_2(2), 3);
assert_eq!(Rational::from(4294967296u64).floor_log_base_power_of_2(8), 4);

// 4^(-2) < 1/10 < 4^(-1)
assert_eq!(Rational::from_signeds(1, 10).floor_log_base_power_of_2(2), -2);
// (1/4)^2 < 1/10 < (1/4)^1
assert_eq!(Rational::from_signeds(1, 10).floor_log_base_power_of_2(-2), 1);

Converts an Integer to a Rational, taking the Integer by reference.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples
extern crate malachite_nz;

use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert_eq!(Rational::from(&Integer::from(123)), 123);
assert_eq!(Rational::from(&Integer::from(-123)), -123);

Converts a Natural to a Rational, taking the Natural by reference.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples
extern crate malachite_nz;

use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert_eq!(Rational::from(&Natural::from(123u32)), 123);

Converts an Integer to a Rational, taking the Integer by value.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_nz;

use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert_eq!(Rational::from(Integer::from(123)), 123);
assert_eq!(Rational::from(Integer::from(-123)), -123);

Converts a Natural to a Rational, taking the Natural by value.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_nz;

use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert_eq!(Rational::from(Natural::from(123u32)), 123);

Converts a signed primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to a Rational.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a string, possibly in scientfic notation, to a Rational.

Use FromSciStringOptions to specify the base (from 2 to 36, inclusive). The rounding mode option is ignored.

If the base is greater than 10, the higher digits are represented by the letters 'a' through 'z' or 'A' through 'Z'; the case doesn’t matter and doesn’t need to be consistent.

Exponents are allowed, and are indicated using the character 'e' or 'E'. If the base is 15 or greater, an ambiguity arises where it may not be clear whether 'e' is a digit or an exponent indicator. To resolve this ambiguity, always use a '+' or '-' sign after the exponent indicator when the base is 15 or greater.

The exponent itself is always parsed using base 10.

Decimal (or other-base) points are allowed.

If the string is unparseable, None is returned.

This function is very literal; given "0.333", it will return $333/1000$ rather than $1/3$. If you’d prefer that it return $1/3$, consider using from_sci_string_simplest instead. However, that function has its quirks too: given "0.1", it will not return $1/10$ (see its documentation for an explanation of this behavior). This function does return $1/10$.

Worst-case complexity

$T(n, m) = O(m^n n \log m (\log n + \log\log m))$

$M(n, m) = O(m^n n \log m)$

where $T$ is time, $M$ is additional memory, $n$ is s.len(), and $m$ is options.base.

Examples
extern crate malachite_base;

use malachite_base::num::conversion::string::options::FromSciStringOptions;
use malachite_base::num::conversion::traits::FromSciString;
use malachite_base::rounding_modes::RoundingMode;
use malachite_q::Rational;

assert_eq!(Rational::from_sci_string("123").unwrap(), 123);
assert_eq!(Rational::from_sci_string("0.1").unwrap().to_string(), "1/10");
assert_eq!(Rational::from_sci_string("0.10").unwrap().to_string(), "1/10");
assert_eq!(Rational::from_sci_string("0.333").unwrap().to_string(), "333/1000");
assert_eq!(Rational::from_sci_string("1.2e5").unwrap(), 120000);
assert_eq!(Rational::from_sci_string("1.2e-5").unwrap().to_string(), "3/250000");

let mut options = FromSciStringOptions::default();
options.set_base(16);
assert_eq!(Rational::from_sci_string_with_options("ff", options).unwrap(), 255);
assert_eq!(Rational::from_sci_string_with_options("ffE+5", options).unwrap(), 267386880);
assert_eq!(
    Rational::from_sci_string_with_options("ffE-5", options).unwrap().to_string(),
    "255/1048576"
);
Converts a &str, possibly in scientific notation, to a number, using the default FromSciStringOptions. Read more

Converts an string to a Rational.

If the string does not represent a valid Rational, an Err is returned. The numerator and denominator do not need to be in lowest terms, but the denominator must be nonzero. A negative sign is only allowed at the 0th position of the string.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is s.len().

Examples
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(Rational::from_str("123456").unwrap(), 123456);
assert_eq!(Rational::from_str("00123456").unwrap(), 123456);
assert_eq!(Rational::from_str("0").unwrap(), 0);
assert_eq!(Rational::from_str("-123456").unwrap(), -123456);
assert_eq!(Rational::from_str("-00123456").unwrap(), -123456);
assert_eq!(Rational::from_str("-0").unwrap(), 0);
assert_eq!(Rational::from_str("22/7").unwrap().to_string(), "22/7");
assert_eq!(Rational::from_str("01/02").unwrap().to_string(), "1/2");
assert_eq!(Rational::from_str("3/21").unwrap().to_string(), "1/7");
assert_eq!(Rational::from_str("-22/7").unwrap().to_string(), "-22/7");
assert_eq!(Rational::from_str("-01/02").unwrap().to_string(), "-1/2");
assert_eq!(Rational::from_str("-3/21").unwrap().to_string(), "-1/7");

assert!(Rational::from_str("").is_err());
assert!(Rational::from_str("a").is_err());
assert!(Rational::from_str("1/0").is_err());
assert!(Rational::from_str("/1").is_err());
assert!(Rational::from_str("1/").is_err());
assert!(Rational::from_str("--1").is_err());
assert!(Rational::from_str("1/-2").is_err());
The associated error which can be returned from parsing.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

Determines whether a Rational is an integer.

$f(x) = x \in \Z$.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{One, Zero};
use malachite_base::num::conversion::traits::IsInteger;
use malachite_q::Rational;

assert_eq!(Rational::ZERO.is_integer(), true);
assert_eq!(Rational::ONE.is_integer(), true);
assert_eq!(Rational::from(100).is_integer(), true);
assert_eq!(Rational::from_signeds(22, 7).is_integer(), false);

Determines whether a Rational is an integer power of 2.

$f(x) = (\exists n \in \Z : 2^n = x)$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::IsPowerOf2;
use malachite_q::Rational;

assert_eq!(Rational::from(0x80).is_power_of_2(), true);
assert_eq!(Rational::from_signeds(1, 8).is_power_of_2(), true);
assert_eq!(Rational::from_signeds(-1, 8).is_power_of_2(), false);
assert_eq!(Rational::from_signeds(22, 7).is_power_of_2(), false);

Multiplies two Rationals, taking both by reference.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{OneHalf, Two};
use malachite_q::Rational;

assert_eq!(&Rational::ONE_HALF * &Rational::TWO, 1);
assert_eq!(
    (&Rational::from_signeds(22, 7) * &Rational::from_signeds(99, 100)).to_string(),
    "1089/350"
);
The resulting type after applying the * operator.

Multiplies two Rationals, taking the first by value and the second by reference.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{OneHalf, Two};
use malachite_q::Rational;

assert_eq!(Rational::ONE_HALF * &Rational::TWO, 1);
assert_eq!(
    (Rational::from_signeds(22, 7) * &Rational::from_signeds(99, 100)).to_string(),
    "1089/350"
);
The resulting type after applying the * operator.

Multiplies two Rationals, taking the first by reference and the second by value.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{OneHalf, Two};
use malachite_q::Rational;

assert_eq!(&Rational::ONE_HALF * Rational::TWO, 1);
assert_eq!(
    (&Rational::from_signeds(22, 7) * Rational::from_signeds(99, 100)).to_string(),
    "1089/350"
);
The resulting type after applying the * operator.

Multiplies two Rationals, taking both by value.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{OneHalf, Two};
use malachite_q::Rational;

assert_eq!(Rational::ONE_HALF * Rational::TWO, 1);
assert_eq!(
    (Rational::from_signeds(22, 7) * Rational::from_signeds(99, 100)).to_string(),
    "1089/350"
);
The resulting type after applying the * operator.

Multiplies a Rational by a Rational in place, taking the Rational on the right-hand side by reference.

$$ x \gets xy. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{OneHalf, Two};
use malachite_q::Rational;

let mut x = Rational::ONE_HALF;
x *= &Rational::TWO;
assert_eq!(x, 1);

let mut x = Rational::from_signeds(22, 7);
x *= &Rational::from_signeds(99, 100);
assert_eq!(x.to_string(), "1089/350");

Multiplies a Rational by a Rational in place, taking the Rational on the right-hand side by value.

$$ x \gets xy. $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{OneHalf, Two};
use malachite_q::Rational;

let mut x = Rational::ONE_HALF;
x *= Rational::TWO;
assert_eq!(x, 1);

let mut x = Rational::from_signeds(22, 7);
x *= Rational::from_signeds(99, 100);
assert_eq!(x.to_string(), "1089/350");

The name of this type, as given by the stringify macro.

See the documentation for impl_named for more details.

Negates a Rational, taking it by reference.

$$ f(x) = -x. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(-&Rational::ZERO, 0);
assert_eq!((-&Rational::from_signeds(22, 7)).to_string(), "-22/7");
assert_eq!((-&Rational::from_signeds(-22, 7)).to_string(), "22/7");
The resulting type after applying the - operator.

Negates a Rational, taking it by value.

$$ f(x) = -x. $$

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

assert_eq!(-Rational::ZERO, 0);
assert_eq!((-Rational::from_signeds(22, 7)).to_string(), "-22/7");
assert_eq!((-Rational::from_signeds(-22, 7)).to_string(), "22/7");
The resulting type after applying the - operator.

Negates a Rational in place.

$$ x \gets -x. $$

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::NegAssign;
use malachite_base::num::basic::traits::Zero;
use malachite_q::Rational;

let mut x = Rational::ZERO;
x.neg_assign();
assert_eq!(x, 0);

let mut x = Rational::from_signeds(22, 7);
x.neg_assign();
assert_eq!(x.to_string(), "-22/7");

let mut x = Rational::from_signeds(-22, 7);
x.neg_assign();
assert_eq!(x.to_string(), "22/7");

The constant -1.

Finds the smallest power of 2 greater than or equal to a Rational. The Rational is taken by reference.

$f(x) = 2^{\lceil \log_2 x \rceil}$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is less than or equal to zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::NextPowerOf2;
use malachite_q::Rational;

assert_eq!((&Rational::from(123)).next_power_of_2(), 128);
assert_eq!((&Rational::from_signeds(1, 10)).next_power_of_2().to_string(), "1/8");

Finds the smallest power of 2 greater than or equal to a Rational. The Rational is taken by value.

$f(x) = 2^{\lceil \log_2 x \rceil}$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is less than or equal to zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::NextPowerOf2;
use malachite_q::Rational;

assert_eq!(Rational::from(123).next_power_of_2(), 128);
assert_eq!(Rational::from_signeds(1, 10).next_power_of_2().to_string(), "1/8");

Finds the smallest power of 2 greater than or equal to a Rational. The Rational is taken by reference.

$f(x) = 2^{\lceil \log_2 x \rceil}$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is less than or equal to zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::NextPowerOf2Assign;
use malachite_q::Rational;
use std::str::FromStr;

let mut x = Rational::from(123);
x.next_power_of_2_assign();
assert_eq!(x, 128);

let mut x = Rational::from_signeds(1, 10);
x.next_power_of_2_assign();
assert_eq!(x.to_string(), "1/8");

The constant 1.

The constant 1/2.

Compares two Rationals.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;
use std::str::FromStr;

assert!(Rational::from_str("2/3").unwrap() > Rational::ONE_HALF);
assert!(Rational::from_str("-2/3").unwrap() < Rational::ONE_HALF);
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more

Compares the absolute values of two Rationals.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::OneHalf;
use malachite_base::num::comparison::traits::OrdAbs;
use malachite_q::Rational;
use std::cmp::Ordering;
use std::str::FromStr;

assert_eq!(
    Rational::from_str("2/3").unwrap().cmp_abs(&Rational::ONE_HALF),
    Ordering::Greater
);
assert_eq!(
    Rational::from_str("-2/3").unwrap().cmp_abs(&Rational::ONE_HALF),
    Ordering::Greater
);

Determines whether a Rational is equal to an Integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert!(Rational::from(-123) == Integer::from(-123));
assert!(Rational::from_signeds(22, 7) != Integer::from(5));
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a Natural.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert!(Rational::from(123) == Natural::from(123u32));
assert!(Rational::from_signeds(22, 7) != Natural::from(5u32));
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an Integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert!(Integer::from(-123) == Rational::from(-123));
assert!(Integer::from(5) != Rational::from_signeds(22, 7));
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Natural is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert!(Natural::from(123u32) == Rational::from(123));
assert!(Natural::from(5u32) != Rational::from_signeds(22, 7));
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a primitive float is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.sci_exponent(), other.significant_bits()), and $m$ is self.sci_exponent().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a primitive float is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.sci_exponent(), other.significant_bits()), and $m$ is self.sci_exponent().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to a Rational.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a primitive float.

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.sci_exponent()), and $m$ is other.sci_exponent().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a primitive float.

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.sci_exponent()), and $m$ is other.sci_exponent().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a signed primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a signed primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a signed primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a signed primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a signed primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to a signed primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Rational is equal to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Compares a Rational to an Integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert!(Rational::from_signeds(22, 7) > Integer::from(3));
assert!(Rational::from_signeds(22, 7) < Integer::from(4));
assert!(Rational::from_signeds(-22, 7) < Integer::from(-3));
assert!(Rational::from_signeds(-22, 7) > Integer::from(-4));
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a Natural.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert!(Rational::from_signeds(22, 7) > Natural::from(3u32));
assert!(Rational::from_signeds(22, 7) < Natural::from(4u32));
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares an Integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::integer::Integer;
use malachite_q::Rational;

assert!(Integer::from(3) < Rational::from_signeds(22, 7));
assert!(Integer::from(4) > Rational::from_signeds(22, 7));
assert!(Integer::from(-3) > Rational::from_signeds(-22, 7));
assert!(Integer::from(-4) < Rational::from_signeds(-22, 7));
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Natural to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_nz;

use malachite_nz::natural::Natural;
use malachite_q::Rational;

assert!(Natural::from(3u32) < Rational::from_signeds(22, 7));
assert!(Natural::from(4u32) > Rational::from_signeds(22, 7));
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares two Rationals.

See the documentation for the Ord implementation.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a primitive float to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(other.sci_exponent(), self.significant_bits()).

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a primitive float to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(other.sci_exponent(), self.significant_bits()).

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a signed primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a signed primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a signed primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a signed primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a signed primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a signed primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares an unsigned primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares an unsigned primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares an unsigned primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares an unsigned primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares an unsigned primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares an unsigned primitive integer to a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a primitive float.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.sci_exponent()).

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a primitive float.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.sci_exponent()).

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares a Rational to an unsigned primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Compares the absolute values of a Rational and an Integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_nz::integer::Integer;
use malachite_q::Rational;
use std::cmp::Ordering;

assert_eq!(
    Rational::from_signeds(22, 7).partial_cmp_abs(&Integer::from(3)),
    Some(Ordering::Greater)
);
assert_eq!(
    Rational::from_signeds(-22, 7).partial_cmp_abs(&Integer::from(-3)),
    Some(Ordering::Greater)
);
Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a Natural.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_nz::natural::Natural;
use malachite_q::Rational;
use std::cmp::Ordering;

assert_eq!(
    Rational::from_signeds(22, 7).partial_cmp_abs(&Natural::from(3u32)),
    Some(Ordering::Greater)
);
assert_eq!(
    Rational::from_signeds(-22, 7).partial_cmp_abs(&Natural::from(3u32)),
    Some(Ordering::Greater)
);
Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of an Integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_nz::integer::Integer;
use malachite_q::Rational;
use std::cmp::Ordering;

assert_eq!(
    Integer::from(3).partial_cmp_abs(&Rational::from_signeds(22, 7)),
    Some(Ordering::Less)
);
assert_eq!(
    Integer::from(-3).partial_cmp_abs(&Rational::from_signeds(-22, 7)),
    Some(Ordering::Less)
);
Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Natural and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;
extern crate malachite_nz;

use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_nz::natural::Natural;
use malachite_q::Rational;
use std::cmp::Ordering;

assert_eq!(
    Natural::from(3u32).partial_cmp_abs(&Rational::from_signeds(22, 7)),
    Some(Ordering::Less)
);
assert_eq!(
    Natural::from(3u32).partial_cmp_abs(&Rational::from_signeds(-22, 7)),
    Some(Ordering::Less)
);
Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of two Rationals.

See the documentation for the OrdAbs implementation.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a primitive float and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.sci_exponent(), other.significant_bits()).

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a primitive float and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.sci_exponent(), other.significant_bits()).

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a signed primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a signed primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a signed primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a signed primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a signed primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a signed primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of an unsigned primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of an unsigned primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of an unsigned primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of an unsigned primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of an unsigned primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of an unsigned primitive integer and a Rational.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a primitive float.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.sci_exponent()).

Examples

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a primitive float.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.sci_exponent()).

Examples

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more
Determines whether the absolute value of one number is greater than the absolute value of another. Read more
Determines whether the absolute value of one number is greater than or equal to the absolute value of another. Read more

Compares the absolute values of a Rational and a signed primitive integer.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples

See here.

Determines whether the absolute value of one number is less than the absolute value of another. Read more
Determines whether the absolute value of one number is less than or equal to the absolute value of another. Read more