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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// 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/>.
use crateFinite;
use String;
use ;
use Deref;
use DivisibleByPowerOf2;
use PrimitiveInt;
use SignificantBits;
use Natural;
use Limb;
/// A floating-point number.
///
/// `Float`s are not yet feature-complete, but the functions that are implemented are thoroughly
/// tested and documented.
///
/// `Float`s are similar to the primitive floats defined by the IEEE 754 standard. They include NaN,
/// $\infty$ and $-\infty$, and positive and negative zero. There is only one NaN; there is no
/// concept of a NaN payload.
///
/// All the finite `Float`s are dyadic rationals (rational numbers whose denominator is a power of
/// 2). A finite `Float` consists of several fields:
/// - a sign, which denotes whether the `Float` is positive or negative;
/// - a significand, which is a [`Natural`] number whose value is equal to the `Float`'s absolute
/// value multiplied by a power of 2;
/// - an exponent, which is one more than the floor of the base-2 logarithm of the `Float`'s
/// absolute value;
/// - and finally, a precision, which is greater than zero and indicates the number of significant
/// bits. It is common to think of a `Float` as an approximation of some real number, and the
/// precision indicates how good the approximation is intended to be.
///
/// `Float`s inherit some odd behavior from the IEEE 754 standard regarding comparison. A `NaN` is
/// not equal to any `Float`, including itself. Positive and negative zero compare as equal, despite
/// being two distinct values. Additionally, (and this is not IEEE 754's fault), `Float`s with
/// different precisions compare as equal if they represent the same numeric value.
///
/// In many cases, the above behavior is unsatisfactory, so the [`ComparableFloat`] and
/// [`ComparableFloat`] wrappers are provided. See their documentation for a description of their
/// comparison behavior.
///
/// In documentation, we will use the '$=$' sign to mean that two `Float`s are identical, writing
/// things like $-\text{NaN}=\text{NaN}$ and $-(0.0) = -0.0$.
///
/// The `Float` type is designed to be very similar to the `mpfr_t` type in
/// [MPFR](https://www.mpfr.org/mpfr-current/mpfr.html#Nomenclature-and-Types), and all Malachite
/// functions produce exactly the same result as their counterparts in MPFR, unless otherwise noted.
///
/// Here are the structural difference between `Float` and `mpfr_t`:
/// - `Float` can only represent a single `NaN` value, with no sign or payload.
/// - Only finite, nonzero `Float`s have a significand, precision, and exponent. For other `Float`s,
/// these concepts are undefined. In particular, unlike `mpfr_t` zeros, `Float` zeros do not have
/// a precision.
/// - The types of `mpfr_t` components are configuration- and platform-dependent. The types of
/// `Float` components are platform-independent, although the `Limb` type is
/// configuration-dependent: it is `u64` by default, but may be changed to `u32` using the
/// `--32_bit_limbs` compiler flag. The type of the exponent is always `i32` and the type of the
/// precision is always `u64`. The `Limb` type only has a visible effect on the functions that
/// extract the raw significand. All other functions have the same interface when compiled with
/// either `Limb` type.
///
/// `Float`s whose precision is 64 bits or less can be represented without any memory allocation.
/// (Unless Malachite is compiled with `32_bit_limbs`, in which case the limit is 32).
InnerFloat);
// A `Float` is serialized as the string `ComparableFloat`'s `Display` writes in base 16, for
// example `0x1.8#2`. Going through a string rather than the fields is what `Natural` and `Integer`
// do too, and here it also keeps the encoding independent of `Limb`'s width: the stored significand
// is padded out to a whole number of limbs, so its digits would differ between 32- and 64-bit
// builds, while the digits of the value itself do not. Reading it back parses, so a deserialized
// `Float` cannot violate the invariants that `is_valid` checks.
pub ;
// We want to limit the visibility of the `NaN`, `Zero`, `Infinity`, and `Finite` constructors to
// within this crate. To do this, we wrap the `InnerFloat` enum in a struct that gets compiled away.
pub
pub
// Given the `(Float, Ordering)` pair from a computation that rounded toward negative infinity (the
// `Float` is the rounded-down value and the `Ordering` compares it to the exact result, as the
// `*_prec_round` functions return), returns the `(floor, ceiling)` pair of `Float`s bracketing the
// exact result. When the value is inexact the ceiling is the next `Float` above the floor, so it is
// obtained by incrementing the floor rather than recomputing — much cheaper when the value comes
// from, for example, a transcendental function.
pub
// `Limb::WIDTH`-derived bit counts, shared across the crate so each is written out only once.
pub const WIDTH_MINUS_1: u64 = WIDTH - 1;
pub const TWICE_WIDTH: u64 = WIDTH << 1;
/// `ComparableFloat` is a wrapper around a [`Float`], taking the [`Float`] by value.
///
/// `CompatableFloat` has different comparison behavior than [`Float`]. See the [`Float`]
/// documentation for its comparison behavior, which is largely derived from the IEEE 754
/// specification; the `ComparableFloat` behavior, on the other hand, is more mathematically
/// well-behaved, and respects the principle that equality should be the finest equivalence
/// relation: that is, that two equal objects should not be different in any way.
///
/// To be more specific: when a [`Float`] is wrapped in a `ComparableFloat`,
/// - `NaN` is not equal to any other [`Float`], but equal to itself;
/// - Positive and negative zero are not equal to each other;
/// - Ordering is total. Negative zero is ordered to be smaller than positive zero, and `NaN` is
/// arbitrarily ordered to be between the two zeros;
/// - Two [`Float`]s with different precisions but representing the same value are unequal, and the
/// one with the greater precision is ordered to be larger;
/// - The hashing function is compatible with equality.
///
/// The analogous wrapper for primitive floats is
/// [`NiceFloat`](malachite_base::num::float::NiceFloat). However,
/// [`NiceFloat`](malachite_base::num::float::NiceFloat) also facilitates better string conversion,
/// something that isn't necessary for [`Float`]s
///
/// `ComparableFloat` owns its float. This is useful in many cases, for example if you want to use
/// [`Float`]s as keys in a hash map. In other situations, it is better to use
/// [`ComparableFloatRef`], which only has a reference to its float.
// Serialized as its inner `Float`, that is as the same hexadecimal string, since the wrapper adds
// no data of its own. That the string carries a precision is what makes the round trip preserve
// everything `ComparableFloat` compares by.
;
/// `ComparableFloatRef` is a wrapper around a [`Float`], taking the [`Float`] be reference.
///
/// See the [`ComparableFloat`] documentation for details.
;
/// Traits for arithmetic.
/// Basic traits for working with [`Float`]s.
/// Traits for comparing [`Float`]s for equality or order.
/// Functions that produce [`Float`] approximations of mathematical constants, using a given
/// precision and rounding mode.
/// Traits for converting to and from [`Float`]s, including converting [`Float`]s to and from
/// strings.
/// Iterators that generate [`Float`]s without repetition.
/// Iterators that generate [`Float`]s randomly.