1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
//! This crate defines [`Rational`]s. The name of this crate refers to the mathematical symbol for
//! rational numbers, $$\mathbb{Q}$$.
//! - There are many functions defined on [`Rational`]s.
//! These include
//! - All the ones you'd expect, like addition, subtraction, multiplication, and division;
//! - Functions related to conversion between [`Rational`]s and other kinds of numbers, including
//! primitive floats;
//! - Functions for Diophantine approximation;
//! - Functions for expressing [`Rational`]s in scientific notation.
//! - The numerators and denominators of [`Rational`]s are stored as
//! [`Natural`](malachite_nz::natural::Natural)s, so [`Rational`]s with small numerators and
//! denominators can be stored entirely on the stack.
//! - Most arithmetic involving [`Rational`]s requires (automatically) reducing the numerator and
//! denominator. This is done very efficiently by using the high performance GCD and exact
//! division algorithms implemented by [`Natural`](malachite_nz::natural::Natural)s.
//!
//! # Demos and benchmarks
//! This crate comes with a `bin` target that can be used for running demos and benchmarks.
//! - Almost all of the public functions in this crate have an associated demo. Running a demo
//! shows you a function's behavior on a large number of inputs. For example, to demo
//! [`Rational`] addition, you can use the following command:
//! ```text
//! cargo run --features bin_build --release -- -l 10000 -m exhaustive -d demo_rational_add
//! ```
//! This command uses the `exhaustive` mode, which generates every possible input, generally
//! starting with the simplest input and progressing to more complex ones. Another mode is
//! `random`. The `-l` flag specifies how many inputs should be generated.
//! - You can use a similar command to run benchmarks. The following command benchmarks various
//! addition algorithms:
//! ```text
//! cargo run --features bin_build --release -- -l 1000000 -m random -b \
//! benchmark_rational_add_algorithms -o add-bench.gp
//! ```
//! or addition implementations of other libraries:
//! ```text
//! cargo run --features bin_build --release -- -l 1000000 -m random -b \
//! benchmark_rational_add_assign_library_comparison -o add-bench.gp
//! ```
//! This creates a file called gcd-bench.gp. You can use gnuplot to create an SVG from it like
//! so:
//! ```text
//! gnuplot -e "set terminal svg; l \"gcd-bench.gp\"" > gcd-bench.svg
//! ```
//!
//! The list of available demos and benchmarks is not documented anywhere; you must find them by
//! browsing through
//! [`bin_util/demo_and_bench`](https://github.com/mhogrefe/malachite/tree/master/malachite-q/src/bin_util/demo_and_bench).
//!
//! # Features
//! - `32_bit_limbs`: Sets the type of [`Limb`](malachite_nz#limbs) to [`u32`] instead of the
//! default, [`u64`].
//! - `test_build`: A large proportion of the code in this crate is only used for testing. For a
//! typical user, building this code would result in an unnecessarily long compilation time and
//! an unnecessarily large binary. My solution is to only build this code when the `test_build`
//! feature is enabled. If you want to run unit tests, you must enable `test_build`. However,
//! doctests don't require it, since they only test the public interface.
//! - `bin_build`: This feature is used to build the code for demos and benchmarks, which also
//! takes a long time to build. Enabling this feature also enables `test_build`.
extern crate alloc;
extern crate malachite_base;
extern crate malachite_nz;
extern crate serde;
extern crate itertools;
extern crate num;
extern crate rug;
/// [`Rational`], the crate's rational-number type, and everything defined on it.
pub use Rational;