Skip to main content

malachite_base/num/exhaustive/
mod.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::iterators::{NonzeroValues, nonzero_values};
10use crate::num::arithmetic::traits::{PowerOf2, RoundToMultipleOfPowerOf2};
11use crate::num::basic::floats::PrimitiveFloat;
12use crate::num::basic::integers::PrimitiveInt;
13use crate::num::basic::signeds::PrimitiveSigned;
14use crate::num::basic::unsigneds::PrimitiveUnsigned;
15use crate::num::conversion::traits::{ExactFrom, WrappingFrom};
16use crate::num::float::NiceFloat;
17use crate::num::iterators::{RulerSequence, ruler_sequence};
18use crate::num::logic::traits::{BitAccess, NotAssign, SignificantBits};
19use crate::rounding_modes::RoundingMode::*;
20use crate::tuples::exhaustive::{
21    ExhaustiveDependentPairs, ExhaustiveDependentPairsYsGenerator, LexDependentPairs,
22    exhaustive_dependent_pairs, lex_dependent_pairs,
23};
24use alloc::vec::{IntoIter, Vec};
25use core::iter::{Chain, Once, Rev, once};
26use core::marker::PhantomData;
27use itertools::{Interleave, Itertools};
28
29/// Generates all primitive integers in an interval.
30///
31/// This `struct` is created by [`primitive_int_increasing_range`] and
32/// [`primitive_int_increasing_inclusive_range`]; see their documentation for more.
33#[derive(Clone, Debug, Eq, Hash, PartialEq)]
34pub struct PrimitiveIntIncreasingRange<T: PrimitiveInt> {
35    a: Option<T>,
36    b: Option<T>,
37}
38
39impl<T: PrimitiveInt> Iterator for PrimitiveIntIncreasingRange<T> {
40    type Item = T;
41
42    fn next(&mut self) -> Option<T> {
43        if self.a == self.b {
44            None
45        } else {
46            let result = self.a;
47            self.a = result.and_then(|x| x.checked_add(T::ONE));
48            result
49        }
50    }
51}
52
53impl<T: PrimitiveInt> DoubleEndedIterator for PrimitiveIntIncreasingRange<T> {
54    fn next_back(&mut self) -> Option<T> {
55        if self.a == self.b {
56            None
57        } else {
58            self.b = Some(self.b.map_or(T::MAX, |b| b - T::ONE));
59            self.b
60        }
61    }
62}
63
64/// Generates all values of a signed integer type in an interval, in order of increasing absolute
65/// value.
66///
67/// This `enum` is created by [`exhaustive_signed_range`] and [`exhaustive_signed_inclusive_range`];
68/// see their documentation for more.
69#[derive(Clone, Debug)]
70pub enum ExhaustiveSignedRange<T: PrimitiveSigned> {
71    NonNegative(PrimitiveIntIncreasingRange<T>),
72    NonPositive(Rev<PrimitiveIntIncreasingRange<T>>),
73    BothSigns(ExhaustiveSigneds<T>),
74}
75
76impl<T: PrimitiveSigned> Iterator for ExhaustiveSignedRange<T> {
77    type Item = T;
78
79    fn next(&mut self) -> Option<T> {
80        match self {
81            Self::NonNegative(xs) => xs.next(),
82            Self::NonPositive(xs) => xs.next(),
83            Self::BothSigns(xs) => xs.next(),
84        }
85    }
86}
87
88#[doc(hidden)]
89pub type PrimitiveIntUpDown<T> =
90    Interleave<PrimitiveIntIncreasingRange<T>, Rev<PrimitiveIntIncreasingRange<T>>>;
91
92/// Generates all unsigned integers in ascending order.
93///
94/// The output is $(k)_{k=0}^{2^W-1}$, where $W$ is the width of the type.
95///
96/// The output length is $2^W$.
97///
98/// # Complexity per iteration
99/// Constant time and additional memory.
100///
101/// # Worst-case complexity per iteration
102/// Constant time and additional memory.
103///
104/// # Examples
105/// ```
106/// use malachite_base::iterators::prefix_to_string;
107/// use malachite_base::num::exhaustive::exhaustive_unsigneds;
108///
109/// assert_eq!(
110///     prefix_to_string(exhaustive_unsigneds::<u8>(), 10),
111///     "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...]"
112/// )
113/// ```
114#[inline]
115pub fn exhaustive_unsigneds<T: PrimitiveUnsigned>() -> PrimitiveIntIncreasingRange<T> {
116    primitive_int_increasing_inclusive_range(T::ZERO, T::MAX)
117}
118
119/// Generates all positive primitive integers in ascending order.
120///
121/// Let $L=2^W-1$ if `T` is unsigned and $L=2^{W-1}-1$ if `T` is signed, where $W$ is the width of
122/// the type.
123///
124/// The output is $(k)_{k=1}^{L}$.
125///
126/// The output length is $L$.
127///
128/// # Complexity per iteration
129/// Constant time and additional memory.
130///
131/// # Worst-case complexity per iteration
132/// Constant time and additional memory.
133///
134/// # Examples
135/// ```
136/// use malachite_base::iterators::prefix_to_string;
137/// use malachite_base::num::exhaustive::exhaustive_positive_primitive_ints;
138///
139/// assert_eq!(
140///     prefix_to_string(exhaustive_positive_primitive_ints::<u8>(), 10),
141///     "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]"
142/// )
143/// ```
144#[inline]
145pub fn exhaustive_positive_primitive_ints<T: PrimitiveInt>() -> PrimitiveIntIncreasingRange<T> {
146    primitive_int_increasing_inclusive_range(T::ONE, T::MAX)
147}
148
149pub type ExhaustiveSigneds<T> = Chain<Once<T>, PrimitiveIntUpDown<T>>;
150
151/// Generates all signed integers in order of increasing absolute value.
152///
153/// When two numbers have the same absolute value, the positive one comes first.
154///
155/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
156/// \operatorname{sgn}(-x_j))$ whenever $i, j \\in [-2^{W-1}, 2^{W-1})$, where $W$ is the width of
157/// the type, and $i < j$.
158///
159/// The output length is $2^W$.
160///
161/// # Complexity per iteration
162/// Constant time and additional memory.
163///
164/// # Worst-case complexity per iteration
165/// Constant time and additional memory.
166///
167/// # Examples
168/// ```
169/// use malachite_base::iterators::prefix_to_string;
170/// use malachite_base::num::exhaustive::exhaustive_signeds;
171///
172/// assert_eq!(
173///     prefix_to_string(exhaustive_signeds::<i8>(), 10),
174///     "[0, 1, -1, 2, -2, 3, -3, 4, -4, 5, ...]"
175/// )
176/// ```
177#[inline]
178pub fn exhaustive_signeds<T: PrimitiveSigned>() -> ExhaustiveSigneds<T> {
179    once(T::ZERO).chain(exhaustive_nonzero_signeds())
180}
181
182/// Generates all natural (non-negative) signed integers in ascending order.
183///
184/// The output is $(k)_{k=0}^{2^{W-1}-1}$, where $W$ is the width of the type.
185///
186/// The output length is $2^{W-1}$.
187///
188/// # Complexity per iteration
189/// Constant time and additional memory.
190///
191/// # Worst-case complexity per iteration
192/// Constant time and additional memory.
193///
194/// # Examples
195/// ```
196/// use malachite_base::iterators::prefix_to_string;
197/// use malachite_base::num::exhaustive::exhaustive_natural_signeds;
198///
199/// assert_eq!(
200///     prefix_to_string(exhaustive_natural_signeds::<i8>(), 10),
201///     "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...]"
202/// )
203/// ```
204#[inline]
205pub fn exhaustive_natural_signeds<T: PrimitiveSigned>() -> PrimitiveIntIncreasingRange<T> {
206    primitive_int_increasing_inclusive_range(T::ZERO, T::MAX)
207}
208
209/// Generates all negative signed integers in descending order.
210///
211/// The output is $(-k)_{k=1}^{2^{W-1}}$, where $W$ is the width of the type.
212///
213/// The output length is $2^{W-1}$.
214///
215/// # Complexity per iteration
216/// Constant time and additional memory.
217///
218/// # Worst-case complexity per iteration
219/// Constant time and additional memory.
220///
221/// # Examples
222/// ```
223/// use malachite_base::iterators::prefix_to_string;
224/// use malachite_base::num::exhaustive::exhaustive_negative_signeds;
225///
226/// assert_eq!(
227///     prefix_to_string(exhaustive_negative_signeds::<i8>(), 10),
228///     "[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, ...]"
229/// )
230/// ```
231#[inline]
232pub fn exhaustive_negative_signeds<T: PrimitiveSigned>() -> Rev<PrimitiveIntIncreasingRange<T>> {
233    primitive_int_increasing_range(T::MIN, T::ZERO).rev()
234}
235
236/// Generates all nonzero signed integers in order of increasing absolute value.
237///
238/// When two numbers have the same absolute value, the positive one comes first.
239///
240/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
241/// \operatorname{sgn}(-x_j))$ whenever $i, j \\in [-2^{W-1}, 2^{W-1}) \\setminus \\{0\\}$, where
242/// $W$ is the width of the type, and $i < j$.
243///
244/// The output length is $2^W-1$.
245///
246/// # Complexity per iteration
247/// Constant time and additional memory.
248///
249/// # Worst-case complexity per iteration
250/// Constant time and additional memory.
251///
252/// # Examples
253/// ```
254/// use malachite_base::iterators::prefix_to_string;
255/// use malachite_base::num::exhaustive::exhaustive_nonzero_signeds;
256///
257/// assert_eq!(
258///     prefix_to_string(exhaustive_nonzero_signeds::<i8>(), 10),
259///     "[1, -1, 2, -2, 3, -3, 4, -4, 5, -5, ...]"
260/// )
261/// ```
262#[inline]
263pub fn exhaustive_nonzero_signeds<T: PrimitiveSigned>() -> PrimitiveIntUpDown<T> {
264    exhaustive_positive_primitive_ints().interleave(exhaustive_negative_signeds())
265}
266
267/// Generates all primitive integers in the half-open interval $[a, b)$, in ascending order.
268///
269/// $a$ must be less than or equal to $b$. If $a$ and $b$ are equal, the range is empty. This
270/// function cannot create a range that includes `T::MAX`; for that, use
271/// [`primitive_int_increasing_inclusive_range`].
272///
273/// The output is $(k)_{k=a}^{b-1}$.
274///
275/// The output length is $b - a$.
276///
277/// # Complexity per iteration
278/// Constant time and additional memory.
279///
280/// # Worst-case complexity per iteration
281/// Constant time and additional memory.
282///
283/// # Panics
284/// Panics if $a > b$.
285///
286/// # Examples
287/// ```
288/// use itertools::Itertools;
289/// use malachite_base::num::exhaustive::primitive_int_increasing_range;
290///
291/// assert_eq!(
292///     primitive_int_increasing_range::<i8>(-5, 5).collect_vec(),
293///     &[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
294/// )
295/// ```
296#[inline]
297pub fn primitive_int_increasing_range<T: PrimitiveInt>(
298    a: T,
299    b: T,
300) -> PrimitiveIntIncreasingRange<T> {
301    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
302    PrimitiveIntIncreasingRange {
303        a: Some(a),
304        b: Some(b),
305    }
306}
307
308/// Generates all primitive integers in the closed interval $[a, b]$, in ascending order.
309///
310/// $a$ must be less than or equal to $b$. If $a$ and $b$ are equal, the range contains a single
311/// element.
312///
313/// The output is $(k)_{k=a}^{b}$.
314///
315/// The output length is $b - a + 1$.
316///
317/// # Complexity per iteration
318/// Constant time and additional memory.
319///
320/// # Worst-case complexity per iteration
321/// Constant time and additional memory.
322///
323/// # Panics
324/// Panics if $a > b$.
325///
326/// # Examples
327/// ```
328/// use itertools::Itertools;
329/// use malachite_base::num::exhaustive::primitive_int_increasing_inclusive_range;
330///
331/// assert_eq!(
332///     primitive_int_increasing_inclusive_range::<i8>(-5, 5).collect_vec(),
333///     &[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
334/// )
335/// ```
336#[inline]
337pub fn primitive_int_increasing_inclusive_range<T: PrimitiveInt>(
338    a: T,
339    b: T,
340) -> PrimitiveIntIncreasingRange<T> {
341    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
342    PrimitiveIntIncreasingRange {
343        a: Some(a),
344        b: b.checked_add(T::ONE),
345    }
346}
347
348/// Generates all signed integers in the half-open interval $[a, b)$, in order of increasing
349/// absolute value.
350///
351/// When two numbers have the same absolute value, the positive one comes first. $a$ must be less
352/// than or equal to $b$. If $a$ and $b$ are equal, the range is empty. This function cannot create
353/// a range that includes `T::MAX`; for that, use [`exhaustive_signed_inclusive_range`].
354///
355/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
356/// \operatorname{sgn}(-x_j))$ whenever $i, j \\in [0, b - a)$ and $i < j$.
357///
358/// The output length is $b - a$.
359///
360/// # Complexity per iteration
361/// Constant time and additional memory.
362///
363/// # Worst-case complexity per iteration
364/// Constant time and additional memory.
365///
366/// # Panics
367/// Panics if $a > b$.
368///
369/// # Examples
370/// ```
371/// use itertools::Itertools;
372/// use malachite_base::num::exhaustive::exhaustive_signed_range;
373///
374/// assert_eq!(
375///     exhaustive_signed_range::<i8>(-5, 5).collect_vec(),
376///     &[0, 1, -1, 2, -2, 3, -3, 4, -4, -5]
377/// )
378/// ```
379pub fn exhaustive_signed_range<T: PrimitiveSigned>(a: T, b: T) -> ExhaustiveSignedRange<T> {
380    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
381    if a >= T::ZERO {
382        ExhaustiveSignedRange::NonNegative(primitive_int_increasing_range(a, b))
383    } else if b <= T::ZERO {
384        ExhaustiveSignedRange::NonPositive(primitive_int_increasing_range(a, b).rev())
385    } else {
386        ExhaustiveSignedRange::BothSigns(
387            once(T::ZERO).chain(
388                primitive_int_increasing_range(T::ONE, b)
389                    .interleave(primitive_int_increasing_range(a, T::ZERO).rev()),
390            ),
391        )
392    }
393}
394
395/// Generates all signed integers in the closed interval $[a, b]$, in order of increasing absolute
396/// value.
397///
398/// When two numbers have the same absolute value, the positive one comes first. $a$ must be less
399/// than or equal to $b$. If $a$ and $b$ are equal, the range contains a single element.
400///
401/// The output satisfies $(|x_i|, \operatorname{sgn}(-x_i)) <_\mathrm{lex} (|x_j|,
402/// \operatorname{sgn}(-x_j))$ whenever $i, j \\in [0, b - a]$ and $i < j$.
403///
404/// The output length is $b - a + 1$.
405///
406/// # Complexity per iteration
407/// Constant time and additional memory.
408///
409/// # Worst-case complexity per iteration
410/// Constant time and additional memory.
411///
412/// # Panics
413/// Panics if $a > b$.
414///
415/// # Examples
416/// ```
417/// use itertools::Itertools;
418/// use malachite_base::num::exhaustive::exhaustive_signed_inclusive_range;
419///
420/// assert_eq!(
421///     exhaustive_signed_inclusive_range::<i8>(-5, 5).collect_vec(),
422///     &[0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5]
423/// )
424/// ```
425pub fn exhaustive_signed_inclusive_range<T: PrimitiveSigned>(
426    a: T,
427    b: T,
428) -> ExhaustiveSignedRange<T> {
429    assert!(a <= b, "a must be less than or equal to b. a: {a}, b: {b}");
430    if a >= T::ZERO {
431        ExhaustiveSignedRange::NonNegative(primitive_int_increasing_inclusive_range(a, b))
432    } else if b <= T::ZERO {
433        ExhaustiveSignedRange::NonPositive(primitive_int_increasing_inclusive_range(a, b).rev())
434    } else {
435        ExhaustiveSignedRange::BothSigns(
436            once(T::ZERO).chain(
437                primitive_int_increasing_inclusive_range(T::ONE, b)
438                    .interleave(primitive_int_increasing_inclusive_range(a, T::NEGATIVE_ONE).rev()),
439            ),
440        )
441    }
442}
443
444/// Generates all primitive floats in an interval, in increasing order.
445///
446/// This `struct` implements [`DoubleEndedIterator`], so you can reverse it to generate floats in
447/// decreasing order.
448///
449/// Positive zero and negative zero are both generated. Negative zero is considered to be less than
450/// positive zero.
451///
452/// This `struct` is created by [`primitive_float_increasing_range`] and
453/// [`primitive_float_increasing_inclusive_range`]; see their documentation for more.
454#[derive(Clone, Debug, Eq, Hash, PartialEq)]
455pub struct PrimitiveFloatIncreasingRange<T: PrimitiveFloat> {
456    phantom: PhantomData<*const T>,
457    xs: PrimitiveIntIncreasingRange<u64>,
458}
459
460impl<T: PrimitiveFloat> Iterator for PrimitiveFloatIncreasingRange<T> {
461    type Item = T;
462
463    #[inline]
464    fn next(&mut self) -> Option<T> {
465        self.xs.next().map(T::from_ordered_representation)
466    }
467}
468
469impl<T: PrimitiveFloat> DoubleEndedIterator for PrimitiveFloatIncreasingRange<T> {
470    #[inline]
471    fn next_back(&mut self) -> Option<T> {
472        self.xs.next_back().map(T::from_ordered_representation)
473    }
474}
475
476/// Generates all primitive floats in the half-open interval $[a, b)$, in ascending order.
477///
478/// Positive and negative zero are treated as two distinct values, with negative zero being smaller
479/// than zero.
480///
481/// `NiceFloat(a)` must be less than or equal to `NiceFloat(b)`. If `NiceFloat(a)` and
482/// `NiceFloat(b)` are equal, the range is empty. This function cannot create a range that includes
483/// `INFINITY`; for that, use [`primitive_float_increasing_inclusive_range`].
484///
485/// Let $\varphi$ be
486/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
487///
488/// The output is $(\varphi^{-1}(k))_{k=\varphi(a)}^{\varphi(b)-1}$.
489///
490/// The output length is $\varphi(b) - \varphi(a)$.
491///
492/// # Complexity per iteration
493/// Constant time and additional memory.
494///
495/// # Worst-case complexity per iteration
496/// Constant time and additional memory.
497///
498/// # Panics
499/// Panics if `NiceFloat(a) > NiceFloat(b)`.
500///
501/// # Examples
502/// ```
503/// use malachite_base::iterators::prefix_to_string;
504/// use malachite_base::num::exhaustive::primitive_float_increasing_range;
505/// use malachite_base::num::float::NiceFloat;
506///
507/// assert_eq!(
508///     prefix_to_string(
509///         primitive_float_increasing_range::<f32>(1.0, 2.0).map(NiceFloat),
510///         20
511///     ),
512///     "[1.0, 1.0000001, 1.0000002, 1.0000004, 1.0000005, 1.0000006, 1.0000007, 1.0000008, \
513///     1.000001, 1.0000011, 1.0000012, 1.0000013, 1.0000014, 1.0000015, 1.0000017, 1.0000018, \
514///     1.0000019, 1.000002, 1.0000021, 1.0000023, ...]"
515/// );
516/// assert_eq!(
517///     prefix_to_string(
518///         primitive_float_increasing_range::<f32>(1.0, 2.0)
519///             .rev()
520///             .map(NiceFloat),
521///         20,
522///     ),
523///     "[1.9999999, 1.9999998, 1.9999996, 1.9999995, 1.9999994, 1.9999993, 1.9999992, 1.999999, \
524///     1.9999989, 1.9999988, 1.9999987, 1.9999986, 1.9999985, 1.9999983, 1.9999982, 1.9999981, \
525///     1.999998, 1.9999979, 1.9999977, 1.9999976, ...]",
526/// );
527/// ```
528pub fn primitive_float_increasing_range<T: PrimitiveFloat>(
529    a: T,
530    b: T,
531) -> PrimitiveFloatIncreasingRange<T> {
532    assert!(!a.is_nan());
533    assert!(!b.is_nan());
534    assert!(
535        NiceFloat(a) <= NiceFloat(b),
536        "a must be less than or equal to b. a: {}, b: {}",
537        NiceFloat(a),
538        NiceFloat(b)
539    );
540    PrimitiveFloatIncreasingRange {
541        phantom: PhantomData,
542        xs: primitive_int_increasing_range(
543            a.to_ordered_representation(),
544            b.to_ordered_representation(),
545        ),
546    }
547}
548
549/// Generates all primitive floats in the closed interval $[a, b]$, in ascending order.
550///
551/// Positive and negative zero are treated as two distinct values, with negative zero being smaller
552/// than zero.
553///
554/// `NiceFloat(a)` must be less than or equal to `NiceFloat(b)`. If `NiceFloat(a)` and
555/// `NiceFloat(b)` are equal, the range contains a single element.
556///
557/// Let $\varphi$ be
558/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
559///
560/// The output is $(\varphi^{-1}(k))_{k=\varphi(a)}^\varphi(b)$.
561///
562/// The output length is $\varphi(b) - \varphi(a) + 1$.
563///
564/// # Complexity per iteration
565/// Constant time and additional memory.
566///
567/// # Worst-case complexity per iteration
568/// Constant time and additional memory.
569///
570/// # Panics
571/// Panics if `NiceFloat(a) > NiceFloat(b)`.
572///
573/// # Examples
574/// ```
575/// use malachite_base::iterators::prefix_to_string;
576/// use malachite_base::num::exhaustive::primitive_float_increasing_inclusive_range;
577/// use malachite_base::num::float::NiceFloat;
578///
579/// assert_eq!(
580///     prefix_to_string(
581///         primitive_float_increasing_inclusive_range::<f32>(1.0, 2.0).map(NiceFloat),
582///         20
583///     ),
584///     "[1.0, 1.0000001, 1.0000002, 1.0000004, 1.0000005, 1.0000006, 1.0000007, 1.0000008, \
585///     1.000001, 1.0000011, 1.0000012, 1.0000013, 1.0000014, 1.0000015, 1.0000017, 1.0000018, \
586///     1.0000019, 1.000002, 1.0000021, 1.0000023, ...]"
587/// );
588/// assert_eq!(
589///     prefix_to_string(
590///         primitive_float_increasing_inclusive_range::<f32>(1.0, 2.0)
591///             .rev()
592///             .map(NiceFloat),
593///         20
594///     ),
595///     "[2.0, 1.9999999, 1.9999998, 1.9999996, 1.9999995, 1.9999994, 1.9999993, 1.9999992, \
596///     1.999999, 1.9999989, 1.9999988, 1.9999987, 1.9999986, 1.9999985, 1.9999983, 1.9999982, \
597///     1.9999981, 1.999998, 1.9999979, 1.9999977, ...]"
598/// );
599/// ```
600pub fn primitive_float_increasing_inclusive_range<T: PrimitiveFloat>(
601    a: T,
602    b: T,
603) -> PrimitiveFloatIncreasingRange<T> {
604    assert!(!a.is_nan());
605    assert!(!b.is_nan());
606    assert!(
607        NiceFloat(a) <= NiceFloat(b),
608        "a must be less than or equal to b. a: {}, b: {}",
609        NiceFloat(a),
610        NiceFloat(b)
611    );
612    PrimitiveFloatIncreasingRange {
613        phantom: PhantomData,
614        xs: primitive_int_increasing_inclusive_range(
615            a.to_ordered_representation(),
616            b.to_ordered_representation(),
617        ),
618    }
619}
620
621/// Generates all finite positive primitive floats, in ascending order.
622///
623/// Positive and negative zero are both excluded.
624///
625/// [`MIN_POSITIVE_SUBNORMAL`](super::basic::floats::PrimitiveFloat::MIN_POSITIVE_SUBNORMAL) is
626/// generated first and [`MAX_FINITE`](super::basic::floats::PrimitiveFloat::MAX_FINITE) is
627/// generated last. The returned iterator is double-ended, so it may be reversed.
628///
629/// Let $\varphi$ be
630/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
631///
632/// The output is $(\varphi^{-1}(k))_{k=2^M(2^E-1)+2}^{2^{M+1}(2^E-1)}$.
633///
634/// The output length is $2^M(2^E-1)-1$.
635/// - For [`f32`], this is $2^{31}-2^{23}-1$, or 2139095039.
636/// - For [`f64`], this is $2^{63}-2^{52}-1$, or 9218868437227405311.
637///
638/// # Complexity per iteration
639/// Constant time and additional memory.
640///
641/// # Worst-case complexity per iteration
642/// Constant time and additional memory.
643///
644/// # Examples
645/// ```
646/// use malachite_base::iterators::prefix_to_string;
647/// use malachite_base::num::exhaustive::positive_finite_primitive_floats_increasing;
648/// use malachite_base::num::float::NiceFloat;
649///
650/// assert_eq!(
651///     prefix_to_string(
652///         positive_finite_primitive_floats_increasing::<f32>().map(NiceFloat),
653///         20
654///     ),
655///     "[1.0e-45, 3.0e-45, 4.0e-45, 6.0e-45, 7.0e-45, 8.0e-45, 1.0e-44, 1.1e-44, 1.3e-44, \
656///     1.4e-44, 1.5e-44, 1.7e-44, 1.8e-44, 2.0e-44, 2.1e-44, 2.2e-44, 2.4e-44, 2.5e-44, 2.7e-44, \
657///     2.8e-44, ...]"
658/// );
659/// assert_eq!(
660///     prefix_to_string(
661///         positive_finite_primitive_floats_increasing::<f32>()
662///             .rev()
663///             .map(NiceFloat),
664///         20
665///     ),
666///     "[3.4028235e38, 3.4028233e38, 3.402823e38, 3.4028229e38, 3.4028227e38, 3.4028225e38, \
667///     3.4028222e38, 3.402822e38, 3.4028218e38, 3.4028216e38, 3.4028214e38, 3.4028212e38, \
668///     3.402821e38, 3.4028208e38, 3.4028206e38, 3.4028204e38, 3.4028202e38, 3.40282e38, \
669///     3.4028198e38, 3.4028196e38, ...]"
670/// );
671/// ```
672#[inline]
673pub fn positive_finite_primitive_floats_increasing<T: PrimitiveFloat>()
674-> PrimitiveFloatIncreasingRange<T> {
675    primitive_float_increasing_inclusive_range(T::MIN_POSITIVE_SUBNORMAL, T::MAX_FINITE)
676}
677
678/// Generates all finite negative primitive floats, in ascending order.
679///
680/// Positive and negative zero are both excluded.
681///
682/// [`-MAX_FINITE`](super::basic::floats::PrimitiveFloat::MAX_FINITE) is generated first and
683/// [`-MIN_POSITIVE_SUBNORMAL`](super::basic::floats::PrimitiveFloat::MIN_POSITIVE_SUBNORMAL) is
684/// generated last. The returned iterator is double-ended, so it may be reversed.
685///
686/// Let $\varphi$ be
687/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
688///
689/// The output is $(\varphi^{-1}(k))_{k=1}^{2^M(2^E-1)-1}$.
690///
691/// The output length is $2^M(2^E-1)-1$.
692/// - For [`f32`], this is $2^{31}-2^{23}-1$, or 2139095039.
693/// - For [`f64`], this is $2^{63}-2^{52}-1$, or 9218868437227405311.
694///
695/// # Complexity per iteration
696/// Constant time and additional memory.
697///
698/// # Worst-case complexity per iteration
699/// Constant time and additional memory.
700///
701/// # Examples
702/// ```
703/// use malachite_base::iterators::prefix_to_string;
704/// use malachite_base::num::exhaustive::negative_finite_primitive_floats_increasing;
705/// use malachite_base::num::float::NiceFloat;
706///
707/// assert_eq!(
708///     prefix_to_string(
709///         negative_finite_primitive_floats_increasing::<f32>().map(NiceFloat),
710///         20
711///     ),
712///     "[-3.4028235e38, -3.4028233e38, -3.402823e38, -3.4028229e38, -3.4028227e38, \
713///     -3.4028225e38, -3.4028222e38, -3.402822e38, -3.4028218e38, -3.4028216e38, -3.4028214e38, \
714///     -3.4028212e38, -3.402821e38, -3.4028208e38, -3.4028206e38, -3.4028204e38, -3.4028202e38, \
715///     -3.40282e38, -3.4028198e38, -3.4028196e38, ...]"
716/// );
717/// assert_eq!(
718///     prefix_to_string(
719///         negative_finite_primitive_floats_increasing::<f32>()
720///             .rev()
721///             .map(NiceFloat),
722///         20
723///     ),
724///     "[-1.0e-45, -3.0e-45, -4.0e-45, -6.0e-45, -7.0e-45, -8.0e-45, -1.0e-44, -1.1e-44, \
725///     -1.3e-44, -1.4e-44, -1.5e-44, -1.7e-44, -1.8e-44, -2.0e-44, -2.1e-44, -2.2e-44, -2.4e-44, \
726///     -2.5e-44, -2.7e-44, -2.8e-44, ...]"
727/// );
728/// ```
729#[inline]
730pub fn negative_finite_primitive_floats_increasing<T: PrimitiveFloat>()
731-> PrimitiveFloatIncreasingRange<T> {
732    primitive_float_increasing_inclusive_range(-T::MAX_FINITE, -T::MIN_POSITIVE_SUBNORMAL)
733}
734
735/// Generates all finite nonzero primitive floats, in ascending order.
736///
737/// Positive and negative zero are both excluded.
738///
739/// [-`MAX_FINITE`](super::basic::floats::PrimitiveFloat::MAX_FINITE) is generated first and
740/// [`MAX_FINITE`](super::basic::floats::PrimitiveFloat::MAX_FINITE) is generated last. The returned
741/// iterator is double-ended, so it may be reversed.
742///
743/// Let $\varphi$ be
744/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
745///
746/// The output is
747/// $$
748/// (\varphi^{-1}(k))_ {k=1}^{2^M(2^E-1)-1} ⧺ (\varphi^{-1}(k))_ {k=2^M(2^E-1)+2}^{2^{M+1}(2^E-1)}
749/// $$.
750///
751/// The output length is $2^{M+1}(2^E-1)-2$.
752/// - For [`f32`], this is $2^{32}-2^{24}-2$, or 4278190078.
753/// - For [`f64`], this is $2^{64}-2^{53}-2$, or 18437736874454810622.
754///
755/// # Complexity per iteration
756/// Constant time and additional memory.
757///
758/// # Worst-case complexity per iteration
759/// Constant time and additional memory.
760///
761/// # Examples
762/// ```
763/// use malachite_base::iterators::prefix_to_string;
764/// use malachite_base::num::exhaustive::nonzero_finite_primitive_floats_increasing;
765/// use malachite_base::num::float::NiceFloat;
766///
767/// assert_eq!(
768///     prefix_to_string(
769///         nonzero_finite_primitive_floats_increasing::<f32>().map(NiceFloat),
770///         20
771///     ),
772///     "[-3.4028235e38, -3.4028233e38, -3.402823e38, -3.4028229e38, -3.4028227e38, \
773///     -3.4028225e38, -3.4028222e38, -3.402822e38, -3.4028218e38, -3.4028216e38, -3.4028214e38, \
774///     -3.4028212e38, -3.402821e38, -3.4028208e38, -3.4028206e38, -3.4028204e38, -3.4028202e38, \
775///     -3.40282e38, -3.4028198e38, -3.4028196e38, ...]"
776/// );
777/// assert_eq!(
778///     prefix_to_string(
779///         nonzero_finite_primitive_floats_increasing::<f32>()
780///             .rev()
781///             .map(NiceFloat),
782///         20
783///     ),
784///     "[3.4028235e38, 3.4028233e38, 3.402823e38, 3.4028229e38, 3.4028227e38, 3.4028225e38, \
785///     3.4028222e38, 3.402822e38, 3.4028218e38, 3.4028216e38, 3.4028214e38, 3.4028212e38, \
786///     3.402821e38, 3.4028208e38, 3.4028206e38, 3.4028204e38, 3.4028202e38, 3.40282e38, \
787///     3.4028198e38, 3.4028196e38, ...]"
788/// );
789/// ```
790#[inline]
791pub fn nonzero_finite_primitive_floats_increasing<T: PrimitiveFloat>()
792-> NonzeroValues<PrimitiveFloatIncreasingRange<T>> {
793    nonzero_values(finite_primitive_floats_increasing())
794}
795
796/// Generates all finite primitive floats, in ascending order.
797///
798/// Positive and negative zero are both included. Negative zero comes first.
799///
800/// [`-MAX_FINITE`](super::basic::floats::PrimitiveFloat::MAX_FINITE) is generated first and
801/// [`MAX_FINITE`](super::basic::floats::PrimitiveFloat::MAX_FINITE) is generated last. The
802/// returned iterator is double-ended, so it may be reversed.
803///
804/// Let $\varphi$ be
805/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
806///
807/// The output is $(\varphi^{-1}(k))_{k=1}^{2^{M+1}(2^E-1)}$.
808///
809/// The output length is $2^{M+1}(2^E-1)$.
810/// - For [`f32`], this is $2^{32}-2^{24}$, or 4278190080.
811/// - For [`f64`], this is $2^{64}-2^{53}$, or 18437736874454810624.
812///
813/// # Complexity per iteration
814/// Constant time and additional memory.
815///
816/// # Worst-case complexity per iteration
817/// Constant time and additional memory.
818///
819/// # Examples
820/// ```
821/// use malachite_base::iterators::prefix_to_string;
822/// use malachite_base::num::exhaustive::finite_primitive_floats_increasing;
823/// use malachite_base::num::float::NiceFloat;
824///
825/// assert_eq!(
826///     prefix_to_string(
827///         finite_primitive_floats_increasing::<f32>().map(NiceFloat),
828///         20
829///     ),
830///     "[-3.4028235e38, -3.4028233e38, -3.402823e38, -3.4028229e38, -3.4028227e38, \
831///     -3.4028225e38, -3.4028222e38, -3.402822e38, -3.4028218e38, -3.4028216e38, -3.4028214e38, \
832///     -3.4028212e38, -3.402821e38, -3.4028208e38, -3.4028206e38, -3.4028204e38, -3.4028202e38, \
833///     -3.40282e38, -3.4028198e38, -3.4028196e38, ...]",
834/// );
835/// assert_eq!(
836///     prefix_to_string(
837///         finite_primitive_floats_increasing::<f32>()
838///             .rev()
839///             .map(NiceFloat),
840///         20
841///     ),
842///     "[3.4028235e38, 3.4028233e38, 3.402823e38, 3.4028229e38, 3.4028227e38, 3.4028225e38, \
843///     3.4028222e38, 3.402822e38, 3.4028218e38, 3.4028216e38, 3.4028214e38, 3.4028212e38, \
844///     3.402821e38, 3.4028208e38, 3.4028206e38, 3.4028204e38, 3.4028202e38, 3.40282e38, \
845///     3.4028198e38, 3.4028196e38, ...]"
846/// );
847/// ```
848#[inline]
849pub fn finite_primitive_floats_increasing<T: PrimitiveFloat>() -> PrimitiveFloatIncreasingRange<T> {
850    primitive_float_increasing_inclusive_range(-T::MAX_FINITE, T::MAX_FINITE)
851}
852
853/// Generates all positive primitive floats, in ascending order.
854///
855/// Positive and negative zero are both excluded.
856///
857/// [`MIN_POSITIVE_SUBNORMAL`](super::basic::floats::PrimitiveFloat::MIN_POSITIVE_SUBNORMAL) is
858/// generated first and `INFINITY` is generated last. The returned iterator is
859/// double-ended, so it may be reversed.
860///
861/// Let $\varphi$ be
862/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
863///
864/// The output is $(\varphi^{-1}(k))_{k=2^M(2^E-1)+2}^{2^{M+1}(2^E-1)+1}$.
865///
866/// The output length is $2^M(2^E-1)$.
867/// - For [`f32`], this is $2^{31}-2^{23}$, or 2139095040.
868/// - For [`f64`], this is $2^{63}-2^{52}$, or 9218868437227405312.
869///
870/// # Complexity per iteration
871/// Constant time and additional memory.
872///
873/// # Worst-case complexity per iteration
874/// Constant time and additional memory.
875///
876/// # Examples
877/// ```
878/// use malachite_base::iterators::prefix_to_string;
879/// use malachite_base::num::exhaustive::positive_primitive_floats_increasing;
880/// use malachite_base::num::float::NiceFloat;
881///
882/// assert_eq!(
883///     prefix_to_string(
884///         positive_primitive_floats_increasing::<f32>().map(NiceFloat),
885///         20
886///     ),
887///     "[1.0e-45, 3.0e-45, 4.0e-45, 6.0e-45, 7.0e-45, 8.0e-45, 1.0e-44, 1.1e-44, 1.3e-44, \
888///     1.4e-44, 1.5e-44, 1.7e-44, 1.8e-44, 2.0e-44, 2.1e-44, 2.2e-44, 2.4e-44, 2.5e-44, 2.7e-44, \
889///     2.8e-44, ...]"
890/// );
891/// assert_eq!(
892///     prefix_to_string(
893///         positive_primitive_floats_increasing::<f32>()
894///             .rev()
895///             .map(NiceFloat),
896///         20
897///     ),
898///     "[Infinity, 3.4028235e38, 3.4028233e38, 3.402823e38, 3.4028229e38, 3.4028227e38, \
899///     3.4028225e38, 3.4028222e38, 3.402822e38, 3.4028218e38, 3.4028216e38, 3.4028214e38, \
900///     3.4028212e38, 3.402821e38, 3.4028208e38, 3.4028206e38, 3.4028204e38, 3.4028202e38, \
901///     3.40282e38, 3.4028198e38, ...]"
902/// );
903/// ```
904#[inline]
905pub fn positive_primitive_floats_increasing<T: PrimitiveFloat>() -> PrimitiveFloatIncreasingRange<T>
906{
907    primitive_float_increasing_inclusive_range(T::MIN_POSITIVE_SUBNORMAL, T::INFINITY)
908}
909
910/// Generates all negative primitive floats, in ascending order.
911///
912/// Positive and negative zero are both excluded.
913///
914/// `NEGATIVE_INFINITY` is generated first and
915/// [`-MIN_POSITIVE_SUBNORMAL`](super::basic::floats::PrimitiveFloat::MIN_POSITIVE_SUBNORMAL) is
916/// generated last. The returned iterator is double-ended, so it may be reversed.
917///
918/// Let $\varphi$ be
919/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
920///
921/// The output is $(\varphi^{-1}(k))_{k=0}^{2^M(2^E-1)-1}$.
922///
923/// The output length is $2^M(2^E-1)$.
924/// - For [`f32`], this is $2^{31}-2^{23}$, or 2139095040.
925/// - For [`f64`], this is $2^{63}-2^{52}$, or 9218868437227405312.
926///
927/// # Complexity per iteration
928/// Constant time and additional memory.
929///
930/// # Worst-case complexity per iteration
931/// Constant time and additional memory.
932///
933/// # Examples
934/// ```
935/// use malachite_base::iterators::prefix_to_string;
936/// use malachite_base::num::exhaustive::negative_primitive_floats_increasing;
937/// use malachite_base::num::float::NiceFloat;
938///
939/// assert_eq!(
940///     prefix_to_string(
941///         negative_primitive_floats_increasing::<f32>().map(NiceFloat),
942///         20
943///     ),
944///     "[-Infinity, -3.4028235e38, -3.4028233e38, -3.402823e38, -3.4028229e38, -3.4028227e38, \
945///     -3.4028225e38, -3.4028222e38, -3.402822e38, -3.4028218e38, -3.4028216e38, -3.4028214e38, \
946///     -3.4028212e38, -3.402821e38, -3.4028208e38, -3.4028206e38, -3.4028204e38, -3.4028202e38, \
947///     -3.40282e38, -3.4028198e38, ...]"
948/// );
949/// assert_eq!(
950///     prefix_to_string(
951///         negative_primitive_floats_increasing::<f32>()
952///             .rev()
953///             .map(NiceFloat),
954///         20
955///     ),
956///     "[-1.0e-45, -3.0e-45, -4.0e-45, -6.0e-45, -7.0e-45, -8.0e-45, -1.0e-44, -1.1e-44, \
957///     -1.3e-44, -1.4e-44, -1.5e-44, -1.7e-44, -1.8e-44, -2.0e-44, -2.1e-44, -2.2e-44, -2.4e-44, \
958///     -2.5e-44, -2.7e-44, -2.8e-44, ...]"
959/// );
960/// ```
961#[inline]
962pub fn negative_primitive_floats_increasing<T: PrimitiveFloat>() -> PrimitiveFloatIncreasingRange<T>
963{
964    primitive_float_increasing_inclusive_range(T::NEGATIVE_INFINITY, -T::MIN_POSITIVE_SUBNORMAL)
965}
966
967/// Generates all nonzero primitive floats, in ascending order.
968///
969/// Positive and negative zero are both excluded.
970///
971/// `NEGATIVE_INFINITY` is generated first and `INFINITY` is generated last. The returned
972/// iterator is double-ended, so it may be reversed.
973///
974/// Let $\varphi$ be
975/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
976///
977/// The output is
978/// $$
979/// (\varphi^{-1}(k))_ {k=0}^{2^M(2^E-1)-1} ⧺ (\varphi^{-1}(k))_
980/// {k=2^M(2^E-1)+2}^{2^{M+1}(2^E-1)+1} $$.
981///
982/// The output length is $2^{M+1}(2^E-1)$.
983/// - For [`f32`], this is $2^{32}-2^{24}$, or 4278190080.
984/// - For [`f64`], this is $2^{64}-2^{53}$, or 18437736874454810624.
985///
986/// # Complexity per iteration
987/// Constant time and additional memory.
988///
989/// # Worst-case complexity per iteration
990/// Constant time and additional memory.
991///
992/// # Examples
993/// ```
994/// use malachite_base::iterators::prefix_to_string;
995/// use malachite_base::num::exhaustive::nonzero_primitive_floats_increasing;
996/// use malachite_base::num::float::NiceFloat;
997///
998/// assert_eq!(
999///     prefix_to_string(
1000///         nonzero_primitive_floats_increasing::<f32>().map(NiceFloat),
1001///         20
1002///     ),
1003///     "[-Infinity, -3.4028235e38, -3.4028233e38, -3.402823e38, -3.4028229e38, -3.4028227e38, \
1004///     -3.4028225e38, -3.4028222e38, -3.402822e38, -3.4028218e38, -3.4028216e38, -3.4028214e38, \
1005///     -3.4028212e38, -3.402821e38, -3.4028208e38, -3.4028206e38, -3.4028204e38, -3.4028202e38, \
1006///     -3.40282e38, -3.4028198e38, ...]"
1007/// );
1008/// assert_eq!(
1009///     prefix_to_string(
1010///         nonzero_primitive_floats_increasing::<f32>()
1011///             .rev()
1012///             .map(NiceFloat),
1013///         20
1014///     ),
1015///     "[Infinity, 3.4028235e38, 3.4028233e38, 3.402823e38, 3.4028229e38, 3.4028227e38, \
1016///     3.4028225e38, 3.4028222e38, 3.402822e38, 3.4028218e38, 3.4028216e38, 3.4028214e38, \
1017///     3.4028212e38, 3.402821e38, 3.4028208e38, 3.4028206e38, 3.4028204e38, 3.4028202e38, \
1018///     3.40282e38, 3.4028198e38, ...]"
1019/// );
1020/// ```
1021#[inline]
1022pub fn nonzero_primitive_floats_increasing<T: PrimitiveFloat>()
1023-> NonzeroValues<PrimitiveFloatIncreasingRange<T>> {
1024    nonzero_values(primitive_floats_increasing())
1025}
1026
1027/// Generates all primitive floats, except `NaN`, in ascending order.
1028///
1029/// Positive and negative zero are both included. Negative zero comes first.
1030///
1031/// `NEGATIVE_INFINITY` is generated first and `INFINITY` is generated last. The returned iterator
1032/// is double-ended, so it may be reversed.
1033///
1034/// Let $\varphi$ be
1035/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
1036///
1037/// The output is $(\varphi^{-1}(k))_{k=0}^{2^{M+1}(2^E-1)+1}$.
1038///
1039/// The output length is $2^{M+1}(2^E-1)+2$.
1040/// - For [`f32`], this is $2^{32}-2^{24}+2$, or 4278190082.
1041/// - For [`f64`], this is $2^{64}-2^{53}+2$, or 18437736874454810626.
1042///
1043/// # Complexity per iteration
1044/// Constant time and additional memory.
1045///
1046/// # Worst-case complexity per iteration
1047/// Constant time and additional memory.
1048///
1049/// # Examples
1050/// ```
1051/// use malachite_base::iterators::prefix_to_string;
1052/// use malachite_base::num::exhaustive::primitive_floats_increasing;
1053/// use malachite_base::num::float::NiceFloat;
1054///
1055/// assert_eq!(
1056///     prefix_to_string(primitive_floats_increasing::<f32>().map(NiceFloat), 20),
1057///     "[-Infinity, -3.4028235e38, -3.4028233e38, -3.402823e38, -3.4028229e38, -3.4028227e38, \
1058///     -3.4028225e38, -3.4028222e38, -3.402822e38, -3.4028218e38, -3.4028216e38, -3.4028214e38, \
1059///     -3.4028212e38, -3.402821e38, -3.4028208e38, -3.4028206e38, -3.4028204e38, -3.4028202e38, \
1060///     -3.40282e38, -3.4028198e38, ...]"
1061/// );
1062/// assert_eq!(
1063///     prefix_to_string(
1064///         primitive_floats_increasing::<f32>().rev().map(NiceFloat),
1065///         20
1066///     ),
1067///     "[Infinity, 3.4028235e38, 3.4028233e38, 3.402823e38, 3.4028229e38, 3.4028227e38, \
1068///     3.4028225e38, 3.4028222e38, 3.402822e38, 3.4028218e38, 3.4028216e38, 3.4028214e38, \
1069///     3.4028212e38, 3.402821e38, 3.4028208e38, 3.4028206e38, 3.4028204e38, 3.4028202e38, \
1070///     3.40282e38, 3.4028198e38, ...]"
1071/// );
1072/// ```
1073#[inline]
1074pub fn primitive_floats_increasing<T: PrimitiveFloat>() -> PrimitiveFloatIncreasingRange<T> {
1075    primitive_float_increasing_inclusive_range(T::NEGATIVE_INFINITY, T::INFINITY)
1076}
1077
1078/// Generates all finite positive primitive floats with a specified `sci_exponent` and precision.
1079///
1080/// This `struct` is created by [`exhaustive_primitive_floats_with_sci_exponent_and_precision`]; see
1081/// its documentation for more.
1082#[derive(Clone, Debug, Default)]
1083pub struct ConstantPrecisionPrimitiveFloats<T: PrimitiveFloat> {
1084    phantom: PhantomData<*const T>,
1085    n: u64,
1086    increment: u64,
1087    i: u64,
1088    count: u64,
1089}
1090
1091impl<T: PrimitiveFloat> Iterator for ConstantPrecisionPrimitiveFloats<T> {
1092    type Item = T;
1093
1094    fn next(&mut self) -> Option<T> {
1095        if self.i == self.count {
1096            None
1097        } else {
1098            let out = T::from_bits(self.n);
1099            self.i += 1;
1100            if self.i < self.count {
1101                self.n += self.increment;
1102            }
1103            Some(out)
1104        }
1105    }
1106}
1107
1108/// Generates all finite positive primitive floats with a specified `sci_exponent` and precision.
1109///
1110/// Positive and negative zero are both excluded.
1111///
1112/// A finite positive primitive float may be uniquely expressed as $x = m_s2^e_s$, where $1 \leq m_s
1113/// < 2$ and $e_s$ is an integer; then $e_s$ is the sci-exponent. An integer $e_s$ occurs as the
1114/// sci-exponent of a float iff $2-2^{E-1}-M \leq e_s < 2^{E-1}$.
1115///
1116/// In the above equation, $m$ is a dyadic rational. Let $p$ be the smallest integer such that
1117/// $m2^{p-1}$ is an integer. Then $p$ is the float's precision. It is also the number of
1118/// significant bits.
1119///
1120/// For example, consider the float $100.0$. It may be written as $\frac{25}{16}2^6$, so
1121/// $m=\frac{25}{16}$ and $e=6$. We can write $m$ in binary as $1.1001_2$. Thus, the sci-exponent is
1122/// 6 and the precision is 5.
1123///
1124/// If $p$ is 1, the output length is 1; otherwise, it is $2^{p-2}$.
1125///
1126/// # Complexity per iteration
1127/// Constant time and additional memory.
1128///
1129/// # Worst-case complexity per iteration
1130/// Constant time and additional memory.
1131///
1132/// # Worst-case complexity per iteration
1133/// Constant time and additional memory.
1134///
1135/// # Worst-case complexity per iteration
1136/// Constant time and additional memory.
1137///
1138/// # Panics
1139/// Panics if the sci-exponent is less than
1140/// [`MIN_EXPONENT`](super::basic::floats::PrimitiveFloat::MIN_EXPONENT) or greater than
1141/// [`MAX_EXPONENT`](super::basic::floats::PrimitiveFloat::MAX_EXPONENT), or if the precision is
1142/// zero or too large for the given sci-exponent (this can be checked using
1143/// [`max_precision_for_sci_exponent`](super::basic::floats::PrimitiveFloat::max_precision_for_sci_exponent)).
1144///
1145/// # Examples
1146/// ```
1147/// use itertools::Itertools;
1148/// use malachite_base::num::exhaustive::*;
1149/// use malachite_base::num::float::NiceFloat;
1150///
1151/// assert_eq!(
1152///     exhaustive_primitive_floats_with_sci_exponent_and_precision::<f32>(0, 3)
1153///         .map(NiceFloat)
1154///         .collect_vec(),
1155///     [1.25, 1.75].iter().copied().map(NiceFloat).collect_vec()
1156/// );
1157/// assert_eq!(
1158///     exhaustive_primitive_floats_with_sci_exponent_and_precision::<f32>(0, 5)
1159///         .map(NiceFloat)
1160///         .collect_vec(),
1161///     [1.0625, 1.1875, 1.3125, 1.4375, 1.5625, 1.6875, 1.8125, 1.9375]
1162///         .iter()
1163///         .copied()
1164///         .map(NiceFloat)
1165///         .collect_vec()
1166/// );
1167/// assert_eq!(
1168///     exhaustive_primitive_floats_with_sci_exponent_and_precision::<f32>(6, 5)
1169///         .map(NiceFloat)
1170///         .collect_vec(),
1171///     [68.0, 76.0, 84.0, 92.0, 100.0, 108.0, 116.0, 124.0]
1172///         .iter()
1173///         .copied()
1174///         .map(NiceFloat)
1175///         .collect_vec()
1176/// );
1177/// ```
1178#[cfg_attr(dylint_lib = "malachite_lints", expect(long_lines))]
1179pub fn exhaustive_primitive_floats_with_sci_exponent_and_precision<T: PrimitiveFloat>(
1180    sci_exponent: i64,
1181    precision: u64,
1182) -> ConstantPrecisionPrimitiveFloats<T> {
1183    assert!(sci_exponent >= T::MIN_EXPONENT);
1184    assert!(sci_exponent <= T::MAX_EXPONENT);
1185    assert_ne!(precision, 0);
1186    let max_precision = T::max_precision_for_sci_exponent(sci_exponent);
1187    assert!(precision <= max_precision);
1188    let increment = u64::power_of_2(max_precision - precision + 1);
1189    let first_mantissa = if precision == 1 {
1190        1
1191    } else {
1192        u64::power_of_2(precision - 1) | 1
1193    };
1194    let first = T::from_integer_mantissa_and_exponent(
1195        first_mantissa,
1196        sci_exponent - i64::exact_from(precision) + 1,
1197    )
1198    .unwrap()
1199    .to_bits();
1200    let count = if precision == 1 {
1201        1
1202    } else {
1203        u64::power_of_2(precision - 2)
1204    };
1205    ConstantPrecisionPrimitiveFloats {
1206        phantom: PhantomData,
1207        n: first,
1208        increment,
1209        i: 0,
1210        count,
1211    }
1212}
1213
1214#[derive(Clone, Debug)]
1215struct PrimitiveFloatsWithExponentGenerator<T: PrimitiveFloat> {
1216    phantom: PhantomData<*const T>,
1217    sci_exponent: i64,
1218}
1219
1220impl<T: PrimitiveFloat>
1221    ExhaustiveDependentPairsYsGenerator<u64, T, ConstantPrecisionPrimitiveFloats<T>>
1222    for PrimitiveFloatsWithExponentGenerator<T>
1223{
1224    #[inline]
1225    fn get_ys(&self, &precision: &u64) -> ConstantPrecisionPrimitiveFloats<T> {
1226        exhaustive_primitive_floats_with_sci_exponent_and_precision(self.sci_exponent, precision)
1227    }
1228}
1229
1230#[inline]
1231fn exhaustive_primitive_floats_with_sci_exponent_helper<T: PrimitiveFloat>(
1232    sci_exponent: i64,
1233) -> LexDependentPairs<
1234    u64,
1235    T,
1236    PrimitiveFloatsWithExponentGenerator<T>,
1237    PrimitiveIntIncreasingRange<u64>,
1238    ConstantPrecisionPrimitiveFloats<T>,
1239> {
1240    lex_dependent_pairs(
1241        primitive_int_increasing_inclusive_range(
1242            1,
1243            T::max_precision_for_sci_exponent(sci_exponent),
1244        ),
1245        PrimitiveFloatsWithExponentGenerator {
1246            phantom: PhantomData,
1247            sci_exponent,
1248        },
1249    )
1250}
1251
1252/// Generates all positive finite primitive floats with a specified `sci_exponent`.
1253///
1254/// This `struct` is created by [`exhaustive_primitive_floats_with_sci_exponent`]; see its
1255/// documentation for more.
1256#[derive(Clone, Debug)]
1257pub struct ExhaustivePrimitiveFloatsWithExponent<T: PrimitiveFloat>(
1258    LexDependentPairs<
1259        u64,
1260        T,
1261        PrimitiveFloatsWithExponentGenerator<T>,
1262        PrimitiveIntIncreasingRange<u64>,
1263        ConstantPrecisionPrimitiveFloats<T>,
1264    >,
1265);
1266
1267impl<T: PrimitiveFloat> Iterator for ExhaustivePrimitiveFloatsWithExponent<T> {
1268    type Item = T;
1269
1270    #[inline]
1271    fn next(&mut self) -> Option<T> {
1272        self.0.next().map(|p| p.1)
1273    }
1274}
1275
1276/// Generates all positive finite primitive floats with a specified sci-exponent.
1277///
1278/// Positive and negative zero are both excluded.
1279///
1280/// A finite positive primitive float may be uniquely expressed as $x = m_s2^e_s$, where $1 \leq m_s
1281/// < 2$ and $e_s$ is an integer; then $e$ is the sci-exponent. An integer $e_s$ occurs as the
1282/// sci-exponent of a float iff $2-2^{E-1}-M \leq e_s < 2^{E-1}$.
1283///
1284/// If $e_s \geq 2-2^{E-1}$ (the float is normal), the output length is $2^M$.
1285/// - For [`f32`], this is $2^{23}$, or 8388608.
1286/// - For [`f64`], this is $2^{52}$, or 4503599627370496.
1287///
1288/// If $e_s < 2-2^{E-1}$ (the float is subnormal), the output length is $2^{e_s+2^{E-1}+M-2}$.
1289/// - For [`f32`], this is $2^{e_s+149}$.
1290/// - For [`f64`], this is $2^{e_s+1074}$.
1291///
1292/// # Complexity per iteration
1293/// Constant time and additional memory.
1294///
1295/// # Panics
1296/// Panics if the sci-exponent is less than
1297/// [`MIN_EXPONENT`](super::basic::floats::PrimitiveFloat::MIN_EXPONENT) or greater than
1298/// [`MAX_EXPONENT`](super::basic::floats::PrimitiveFloat::MAX_EXPONENT).
1299///
1300/// # Examples
1301/// ```
1302/// use itertools::Itertools;
1303/// use malachite_base::iterators::prefix_to_string;
1304/// use malachite_base::num::exhaustive::exhaustive_primitive_floats_with_sci_exponent;
1305/// use malachite_base::num::float::NiceFloat;
1306///
1307/// assert_eq!(
1308///     prefix_to_string(
1309///         exhaustive_primitive_floats_with_sci_exponent::<f32>(0).map(NiceFloat),
1310///         20
1311///     ),
1312///     "[1.0, 1.5, 1.25, 1.75, 1.125, 1.375, 1.625, 1.875, 1.0625, 1.1875, 1.3125, 1.4375, \
1313///     1.5625, 1.6875, 1.8125, 1.9375, 1.03125, 1.09375, 1.15625, 1.21875, ...]",
1314/// );
1315/// assert_eq!(
1316///     prefix_to_string(
1317///         exhaustive_primitive_floats_with_sci_exponent::<f32>(4).map(NiceFloat),
1318///         20
1319///     ),
1320///     "[16.0, 24.0, 20.0, 28.0, 18.0, 22.0, 26.0, 30.0, 17.0, 19.0, 21.0, 23.0, 25.0, 27.0, \
1321///     29.0, 31.0, 16.5, 17.5, 18.5, 19.5, ...]"
1322/// );
1323/// assert_eq!(
1324///     exhaustive_primitive_floats_with_sci_exponent::<f32>(-147)
1325///         .map(NiceFloat)
1326///         .collect_vec(),
1327///     [6.0e-45, 8.0e-45, 7.0e-45, 1.0e-44]
1328///         .iter()
1329///         .copied()
1330///         .map(NiceFloat)
1331///         .collect_vec()
1332/// );
1333/// ```
1334#[inline]
1335pub fn exhaustive_primitive_floats_with_sci_exponent<T: PrimitiveFloat>(
1336    sci_exponent: i64,
1337) -> ExhaustivePrimitiveFloatsWithExponent<T> {
1338    ExhaustivePrimitiveFloatsWithExponent(exhaustive_primitive_floats_with_sci_exponent_helper(
1339        sci_exponent,
1340    ))
1341}
1342
1343#[derive(Clone, Debug)]
1344struct ExhaustivePositiveFinitePrimitiveFloatsGenerator<T: PrimitiveFloat> {
1345    phantom: PhantomData<*const T>,
1346}
1347
1348impl<T: PrimitiveFloat>
1349    ExhaustiveDependentPairsYsGenerator<i64, T, ExhaustivePrimitiveFloatsWithExponent<T>>
1350    for ExhaustivePositiveFinitePrimitiveFloatsGenerator<T>
1351{
1352    #[inline]
1353    fn get_ys(&self, &sci_exponent: &i64) -> ExhaustivePrimitiveFloatsWithExponent<T> {
1354        exhaustive_primitive_floats_with_sci_exponent(sci_exponent)
1355    }
1356}
1357
1358#[inline]
1359fn exhaustive_positive_finite_primitive_floats_helper<T: PrimitiveFloat>()
1360-> ExhaustiveDependentPairs<
1361    i64,
1362    T,
1363    RulerSequence<usize>,
1364    ExhaustivePositiveFinitePrimitiveFloatsGenerator<T>,
1365    ExhaustiveSignedRange<i64>,
1366    ExhaustivePrimitiveFloatsWithExponent<T>,
1367> {
1368    exhaustive_dependent_pairs(
1369        ruler_sequence(),
1370        exhaustive_signed_inclusive_range(T::MIN_EXPONENT, T::MAX_EXPONENT),
1371        ExhaustivePositiveFinitePrimitiveFloatsGenerator {
1372            phantom: PhantomData,
1373        },
1374    )
1375}
1376
1377/// Generates all positive finite primitive floats.
1378///
1379/// This `struct` is created by [`exhaustive_positive_finite_primitive_floats`]; see its
1380/// documentation for more.
1381#[derive(Clone, Debug)]
1382pub struct ExhaustivePositiveFinitePrimitiveFloats<T: PrimitiveFloat>(
1383    ExhaustiveDependentPairs<
1384        i64,
1385        T,
1386        RulerSequence<usize>,
1387        ExhaustivePositiveFinitePrimitiveFloatsGenerator<T>,
1388        ExhaustiveSignedRange<i64>,
1389        ExhaustivePrimitiveFloatsWithExponent<T>,
1390    >,
1391);
1392
1393impl<T: PrimitiveFloat> Iterator for ExhaustivePositiveFinitePrimitiveFloats<T> {
1394    type Item = T;
1395
1396    #[inline]
1397    fn next(&mut self) -> Option<T> {
1398        self.0.next().map(|p| p.1)
1399    }
1400}
1401
1402/// Generates all positive finite primitive floats.
1403///
1404/// Positive and negative zero are both excluded.
1405///
1406/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats in
1407/// ascending order instead, use [`positive_finite_primitive_floats_increasing`].
1408///
1409/// The output length is $2^M(2^E-1)-1$.
1410/// - For [`f32`], this is $2^{31}-2^{23}-1$, or 2139095039.
1411/// - For [`f64`], this is $2^{63}-2^{52}-1$, or 9218868437227405311.
1412///
1413/// # Complexity per iteration
1414/// Constant time and additional memory.
1415///
1416/// # Worst-case complexity per iteration
1417/// Constant time and additional memory.
1418///
1419/// # Examples
1420/// ```
1421/// use malachite_base::iterators::prefix_to_string;
1422/// use malachite_base::num::exhaustive::exhaustive_positive_finite_primitive_floats;
1423/// use malachite_base::num::float::NiceFloat;
1424///
1425/// assert_eq!(
1426///     prefix_to_string(
1427///         exhaustive_positive_finite_primitive_floats::<f32>().map(NiceFloat),
1428///         50
1429///     ),
1430///     "[1.0, 2.0, 1.5, 0.5, 1.25, 3.0, 1.75, 4.0, 1.125, 2.5, 1.375, 0.75, 1.625, 3.5, 1.875, \
1431///     0.25, 1.0625, 2.25, 1.1875, 0.625, 1.3125, 2.75, 1.4375, 6.0, 1.5625, 3.25, 1.6875, 0.875, \
1432///     1.8125, 3.75, 1.9375, 8.0, 1.03125, 2.125, 1.09375, 0.5625, 1.15625, 2.375, 1.21875, 5.0, \
1433///     1.28125, 2.625, 1.34375, 0.6875, 1.40625, 2.875, 1.46875, 0.375, 1.53125, 3.125, ...]"
1434/// );
1435/// ```
1436#[inline]
1437pub fn exhaustive_positive_finite_primitive_floats<T: PrimitiveFloat>()
1438-> ExhaustivePositiveFinitePrimitiveFloats<T> {
1439    ExhaustivePositiveFinitePrimitiveFloats(exhaustive_positive_finite_primitive_floats_helper())
1440}
1441
1442/// Generates all negative finite primitive floats.
1443///
1444/// This `struct` is created by [`exhaustive_negative_finite_primitive_floats`]; see its
1445/// documentation for more.
1446#[derive(Clone, Debug)]
1447pub struct ExhaustiveNegativeFinitePrimitiveFloats<T: PrimitiveFloat>(
1448    ExhaustivePositiveFinitePrimitiveFloats<T>,
1449);
1450
1451impl<T: PrimitiveFloat> Iterator for ExhaustiveNegativeFinitePrimitiveFloats<T> {
1452    type Item = T;
1453
1454    #[inline]
1455    fn next(&mut self) -> Option<T> {
1456        self.0.next().map(|f| -f)
1457    }
1458}
1459
1460/// Generates all negative finite primitive floats.
1461///
1462/// Positive and negative zero are both excluded.
1463///
1464/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats in
1465/// ascending order instead, use [`negative_finite_primitive_floats_increasing`].
1466///
1467/// The output length is $2^M(2^E-1)-1$.
1468/// - For [`f32`], this is $2^{31}-2^{23}-1$, or 2139095039.
1469/// - For [`f64`], this is $2^{63}-2^{52}-1$, or 9218868437227405311.
1470///
1471/// # Complexity per iteration
1472/// Constant time and additional memory.
1473///
1474/// # Worst-case complexity per iteration
1475/// Constant time and additional memory.
1476///
1477/// # Examples
1478/// ```
1479/// use malachite_base::iterators::prefix_to_string;
1480/// use malachite_base::num::exhaustive::exhaustive_negative_finite_primitive_floats;
1481/// use malachite_base::num::float::NiceFloat;
1482///
1483/// assert_eq!(
1484///     prefix_to_string(
1485///         exhaustive_negative_finite_primitive_floats::<f32>().map(NiceFloat),
1486///         50
1487///     ),
1488///     "[-1.0, -2.0, -1.5, -0.5, -1.25, -3.0, -1.75, -4.0, -1.125, -2.5, -1.375, -0.75, -1.625, \
1489///     -3.5, -1.875, -0.25, -1.0625, -2.25, -1.1875, -0.625, -1.3125, -2.75, -1.4375, -6.0, \
1490///     -1.5625, -3.25, -1.6875, -0.875, -1.8125, -3.75, -1.9375, -8.0, -1.03125, -2.125, \
1491///     -1.09375, -0.5625, -1.15625, -2.375, -1.21875, -5.0, -1.28125, -2.625, -1.34375, -0.6875, \
1492///     -1.40625, -2.875, -1.46875, -0.375, -1.53125, -3.125, ...]"
1493/// );
1494/// ```
1495#[inline]
1496pub fn exhaustive_negative_finite_primitive_floats<T: PrimitiveFloat>()
1497-> ExhaustiveNegativeFinitePrimitiveFloats<T> {
1498    ExhaustiveNegativeFinitePrimitiveFloats(exhaustive_positive_finite_primitive_floats())
1499}
1500
1501/// Generates all nonzero finite primitive floats.
1502///
1503/// This `struct` is created by [`exhaustive_nonzero_finite_primitive_floats`]; see its
1504/// documentation for more.
1505#[derive(Clone, Debug)]
1506pub struct ExhaustiveNonzeroFinitePrimitiveFloats<T: PrimitiveFloat> {
1507    toggle: bool,
1508    xs: ExhaustivePositiveFinitePrimitiveFloats<T>,
1509    x: T,
1510}
1511
1512impl<T: PrimitiveFloat> Iterator for ExhaustiveNonzeroFinitePrimitiveFloats<T> {
1513    type Item = T;
1514
1515    #[inline]
1516    fn next(&mut self) -> Option<T> {
1517        self.toggle.not_assign();
1518        Some(if self.toggle {
1519            self.x = self.xs.next().unwrap();
1520            self.x
1521        } else {
1522            -self.x
1523        })
1524    }
1525}
1526
1527/// Generates all nonzero finite primitive floats.
1528///
1529/// Positive and negative zero are both excluded.
1530///
1531/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats in
1532/// ascending order instead, use [`nonzero_finite_primitive_floats_increasing`].
1533///
1534/// The output length is $2^{M+1}(2^E-1)-2$.
1535/// - For [`f32`], this is $2^{32}-2^{24}-2$, or 4278190078.
1536/// - For [`f64`], this is $2^{64}-2^{53}-2$, or 18437736874454810622.
1537///
1538/// # Complexity per iteration
1539/// Constant time and additional memory.
1540///
1541/// # Worst-case complexity per iteration
1542/// Constant time and additional memory.
1543///
1544/// # Examples
1545/// ```
1546/// use malachite_base::iterators::prefix_to_string;
1547/// use malachite_base::num::exhaustive::exhaustive_nonzero_finite_primitive_floats;
1548/// use malachite_base::num::float::NiceFloat;
1549///
1550/// assert_eq!(
1551///     prefix_to_string(
1552///         exhaustive_nonzero_finite_primitive_floats::<f32>().map(NiceFloat),
1553///         50
1554///     ),
1555///     "[1.0, -1.0, 2.0, -2.0, 1.5, -1.5, 0.5, -0.5, 1.25, -1.25, 3.0, -3.0, 1.75, -1.75, 4.0, \
1556///     -4.0, 1.125, -1.125, 2.5, -2.5, 1.375, -1.375, 0.75, -0.75, 1.625, -1.625, 3.5, -3.5, \
1557///     1.875, -1.875, 0.25, -0.25, 1.0625, -1.0625, 2.25, -2.25, 1.1875, -1.1875, 0.625, -0.625, \
1558///     1.3125, -1.3125, 2.75, -2.75, 1.4375, -1.4375, 6.0, -6.0, 1.5625, -1.5625, ...]"
1559/// );
1560/// ```
1561#[inline]
1562pub fn exhaustive_nonzero_finite_primitive_floats<T: PrimitiveFloat>()
1563-> ExhaustiveNonzeroFinitePrimitiveFloats<T> {
1564    ExhaustiveNonzeroFinitePrimitiveFloats {
1565        toggle: false,
1566        xs: exhaustive_positive_finite_primitive_floats(),
1567        x: T::ZERO,
1568    }
1569}
1570
1571pub type ExhaustiveFinitePrimitiveFloats<T> =
1572    Chain<IntoIter<T>, ExhaustiveNonzeroFinitePrimitiveFloats<T>>;
1573
1574/// Generates all finite primitive floats.
1575///
1576/// Positive and negative zero are both included.
1577///
1578/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats in
1579/// ascending order instead, use [`finite_primitive_floats_increasing`].
1580///
1581/// The output length is $2^{M+1}(2^E-1)$.
1582/// - For [`f32`], this is $2^{32}-2^{24}$, or 4278190080.
1583/// - For [`f64`], this is $2^{64}-2^{53}$, or 18437736874454810624.
1584///
1585/// # Complexity per iteration
1586/// Constant time and additional memory.
1587///
1588/// # Worst-case complexity per iteration
1589/// Constant time and additional memory.
1590///
1591/// # Examples
1592/// ```
1593/// use malachite_base::iterators::prefix_to_string;
1594/// use malachite_base::num::exhaustive::exhaustive_finite_primitive_floats;
1595/// use malachite_base::num::float::NiceFloat;
1596///
1597/// assert_eq!(
1598///     prefix_to_string(
1599///         exhaustive_finite_primitive_floats::<f32>().map(NiceFloat),
1600///         50
1601///     ),
1602///     "[0.0, -0.0, 1.0, -1.0, 2.0, -2.0, 1.5, -1.5, 0.5, -0.5, 1.25, -1.25, 3.0, -3.0, 1.75, \
1603///     -1.75, 4.0, -4.0, 1.125, -1.125, 2.5, -2.5, 1.375, -1.375, 0.75, -0.75, 1.625, -1.625, \
1604///     3.5, -3.5, 1.875, -1.875, 0.25, -0.25, 1.0625, -1.0625, 2.25, -2.25, 1.1875, -1.1875, \
1605///     0.625, -0.625, 1.3125, -1.3125, 2.75, -2.75, 1.4375, -1.4375, 6.0, -6.0, ...]"
1606/// );
1607/// ```
1608#[inline]
1609pub fn exhaustive_finite_primitive_floats<T: PrimitiveFloat>()
1610-> Chain<IntoIter<T>, ExhaustiveNonzeroFinitePrimitiveFloats<T>> {
1611    ::alloc::vec![T::ZERO, T::NEGATIVE_ZERO]
1612        .into_iter()
1613        .chain(exhaustive_nonzero_finite_primitive_floats())
1614}
1615
1616/// Generates all positive primitive floats.
1617///
1618/// Positive and negative zero are both excluded.
1619///
1620/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats in
1621/// ascending order instead, use [`positive_primitive_floats_increasing`].
1622///
1623/// The output length is $2^M(2^E-1)$.
1624/// - For [`f32`], this is $2^{31}-2^{23}$, or 2139095040.
1625/// - For [`f64`], this is $2^{63}-2^{52}$, or 9218868437227405312.
1626///
1627/// # Complexity per iteration
1628/// Constant time and additional memory.
1629///
1630/// # Worst-case complexity per iteration
1631/// Constant time and additional memory.
1632///
1633/// # Examples
1634/// ```
1635/// use malachite_base::iterators::prefix_to_string;
1636/// use malachite_base::num::exhaustive::exhaustive_positive_primitive_floats;
1637/// use malachite_base::num::float::NiceFloat;
1638///
1639/// assert_eq!(
1640///     prefix_to_string(
1641///         exhaustive_positive_primitive_floats::<f32>().map(NiceFloat),
1642///         50
1643///     ),
1644///     "[Infinity, 1.0, 2.0, 1.5, 0.5, 1.25, 3.0, 1.75, 4.0, 1.125, 2.5, 1.375, 0.75, 1.625, \
1645///     3.5, 1.875, 0.25, 1.0625, 2.25, 1.1875, 0.625, 1.3125, 2.75, 1.4375, 6.0, 1.5625, 3.25, \
1646///     1.6875, 0.875, 1.8125, 3.75, 1.9375, 8.0, 1.03125, 2.125, 1.09375, 0.5625, 1.15625, \
1647///     2.375, 1.21875, 5.0, 1.28125, 2.625, 1.34375, 0.6875, 1.40625, 2.875, 1.46875, 0.375, \
1648///     1.53125, ...]"
1649/// );
1650/// ```
1651#[inline]
1652pub fn exhaustive_positive_primitive_floats<T: PrimitiveFloat>()
1653-> Chain<Once<T>, ExhaustivePositiveFinitePrimitiveFloats<T>> {
1654    once(T::INFINITY).chain(exhaustive_positive_finite_primitive_floats())
1655}
1656
1657/// Generates all negative primitive floats.
1658///
1659/// Positive and negative zero are both excluded.
1660///
1661/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats in
1662/// ascending order instead, use [`negative_primitive_floats_increasing`].
1663///
1664/// The output length is $2^M(2^E-1)$.
1665/// - For [`f32`], this is $2^{31}-2^{23}$, or 2139095040.
1666/// - For [`f64`], this is $2^{63}-2^{52}$, or 9218868437227405312.
1667///
1668/// # Complexity per iteration
1669/// Constant time and additional memory.
1670///
1671/// # Worst-case complexity per iteration
1672/// Constant time and additional memory.
1673///
1674/// # Examples
1675/// ```
1676/// use malachite_base::iterators::prefix_to_string;
1677/// use malachite_base::num::exhaustive::exhaustive_negative_primitive_floats;
1678/// use malachite_base::num::float::NiceFloat;
1679///
1680/// assert_eq!(
1681///     prefix_to_string(
1682///         exhaustive_negative_primitive_floats::<f32>().map(NiceFloat),
1683///         50
1684///     ),
1685///     "[-Infinity, -1.0, -2.0, -1.5, -0.5, -1.25, -3.0, -1.75, -4.0, -1.125, -2.5, -1.375, \
1686///     -0.75, -1.625, -3.5, -1.875, -0.25, -1.0625, -2.25, -1.1875, -0.625, -1.3125, -2.75, \
1687///     -1.4375, -6.0, -1.5625, -3.25, -1.6875, -0.875, -1.8125, -3.75, -1.9375, -8.0, -1.03125, \
1688///     -2.125, -1.09375, -0.5625, -1.15625, -2.375, -1.21875, -5.0, -1.28125, -2.625, -1.34375, \
1689///     -0.6875, -1.40625, -2.875, -1.46875, -0.375, -1.53125, ...]"
1690/// );
1691/// ```
1692#[inline]
1693pub fn exhaustive_negative_primitive_floats<T: PrimitiveFloat>()
1694-> Chain<Once<T>, ExhaustiveNegativeFinitePrimitiveFloats<T>> {
1695    once(T::NEGATIVE_INFINITY).chain(exhaustive_negative_finite_primitive_floats())
1696}
1697
1698/// Generates all nonzero primitive floats.
1699///
1700/// Positive and negative zero are both excluded. NaN is excluded as well.
1701///
1702/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats in
1703/// ascending order instead, use [`nonzero_primitive_floats_increasing`].
1704///
1705/// The output length is $2^{M+1}(2^E-1)$.
1706/// - For [`f32`], this is $2^{32}-2^{24}$, or 4278190080.
1707/// - For [`f64`], this is $2^{64}-2^{53}$, or 18437736874454810624.
1708///
1709/// # Complexity per iteration
1710/// Constant time and additional memory.
1711///
1712/// # Worst-case complexity per iteration
1713/// Constant time and additional memory.
1714///
1715/// # Examples
1716/// ```
1717/// use malachite_base::iterators::prefix_to_string;
1718/// use malachite_base::num::exhaustive::exhaustive_nonzero_primitive_floats;
1719/// use malachite_base::num::float::NiceFloat;
1720///
1721/// assert_eq!(
1722///     prefix_to_string(
1723///         exhaustive_nonzero_primitive_floats::<f32>().map(NiceFloat),
1724///         50
1725///     ),
1726///     "[Infinity, -Infinity, 1.0, -1.0, 2.0, -2.0, 1.5, -1.5, 0.5, -0.5, 1.25, -1.25, 3.0, \
1727///     -3.0, 1.75, -1.75, 4.0, -4.0, 1.125, -1.125, 2.5, -2.5, 1.375, -1.375, 0.75, -0.75, \
1728///     1.625, -1.625, 3.5, -3.5, 1.875, -1.875, 0.25, -0.25, 1.0625, -1.0625, 2.25, -2.25, \
1729///     1.1875, -1.1875, 0.625, -0.625, 1.3125, -1.3125, 2.75, -2.75, 1.4375, -1.4375, 6.0, -6.0, \
1730///     ...]"
1731/// );
1732/// ```
1733#[inline]
1734pub fn exhaustive_nonzero_primitive_floats<T: PrimitiveFloat>()
1735-> Chain<IntoIter<T>, ExhaustiveNonzeroFinitePrimitiveFloats<T>> {
1736    ::alloc::vec![T::INFINITY, T::NEGATIVE_INFINITY]
1737        .into_iter()
1738        .chain(exhaustive_nonzero_finite_primitive_floats())
1739}
1740
1741/// Generates all primitive floats.
1742///
1743/// Positive and negative zero are both included.
1744///
1745/// Roughly speaking, the simplest floats are generated first. If you want to generate the floats
1746/// (except `NaN`) in ascending order instead, use [`primitive_floats_increasing`].
1747///
1748/// The output length is $2^{M+1}(2^E-1)+2$.
1749/// - For [`f32`], this is $2^{32}-2^{24}+2$, or 4278190082.
1750/// - For [`f64`], this is $2^{64}-2^{53}+2$, or 18437736874454810626.
1751///
1752/// # Complexity per iteration
1753/// Constant time and additional memory.
1754///
1755/// # Worst-case complexity per iteration
1756/// Constant time and additional memory.
1757///
1758/// # Examples
1759/// ```
1760/// use malachite_base::iterators::prefix_to_string;
1761/// use malachite_base::num::exhaustive::exhaustive_primitive_floats;
1762/// use malachite_base::num::float::NiceFloat;
1763///
1764/// assert_eq!(
1765///     prefix_to_string(exhaustive_primitive_floats::<f32>().map(NiceFloat), 50),
1766///     "[NaN, Infinity, -Infinity, 0.0, -0.0, 1.0, -1.0, 2.0, -2.0, 1.5, -1.5, 0.5, -0.5, 1.25, \
1767///     -1.25, 3.0, -3.0, 1.75, -1.75, 4.0, -4.0, 1.125, -1.125, 2.5, -2.5, 1.375, -1.375, 0.75, \
1768///     -0.75, 1.625, -1.625, 3.5, -3.5, 1.875, -1.875, 0.25, -0.25, 1.0625, -1.0625, 2.25, \
1769///     -2.25, 1.1875, -1.1875, 0.625, -0.625, 1.3125, -1.3125, 2.75, -2.75, 1.4375, ...]"
1770/// );
1771/// ```
1772#[inline]
1773pub fn exhaustive_primitive_floats<T: PrimitiveFloat>()
1774-> Chain<IntoIter<T>, ExhaustiveNonzeroFinitePrimitiveFloats<T>> {
1775    ::alloc::vec![T::NAN, T::INFINITY, T::NEGATIVE_INFINITY, T::ZERO, T::NEGATIVE_ZERO]
1776        .into_iter()
1777        .chain(exhaustive_nonzero_finite_primitive_floats())
1778}
1779
1780private_test_fn! {exhaustive_primitive_floats_with_sci_exponent_and_precision_in_range<
1781    T: PrimitiveFloat,
1782>(
1783    a: T,
1784    b: T,
1785    sci_exponent: i64,
1786    precision: u64
1787) -> ConstantPrecisionPrimitiveFloats<T> {
1788    assert!(a.is_finite());
1789    assert!(b.is_finite());
1790    assert!(a > T::ZERO);
1791    assert!(b > T::ZERO);
1792    assert!(sci_exponent >= T::MIN_EXPONENT);
1793    assert!(sci_exponent <= T::MAX_EXPONENT);
1794    let (am, ae) = a.raw_mantissa_and_exponent();
1795    let (bm, be) = b.raw_mantissa_and_exponent();
1796    let ae_actual_sci_exponent = if ae == 0 {
1797        i64::wrapping_from(am.significant_bits()) + T::MIN_EXPONENT - 1
1798    } else {
1799        i64::wrapping_from(ae) - T::MAX_EXPONENT
1800    };
1801    let be_actual_sci_exponent = if be == 0 {
1802        i64::wrapping_from(bm.significant_bits()) + T::MIN_EXPONENT - 1
1803    } else {
1804        i64::wrapping_from(be) - T::MAX_EXPONENT
1805    };
1806    assert_eq!(ae_actual_sci_exponent, sci_exponent);
1807    assert_eq!(be_actual_sci_exponent, sci_exponent);
1808    assert!(am <= bm);
1809    assert_ne!(precision, 0);
1810    let max_precision = T::max_precision_for_sci_exponent(sci_exponent);
1811    assert!(precision <= max_precision);
1812    if precision == 1 && am == 0 {
1813        return ConstantPrecisionPrimitiveFloats {
1814            phantom: PhantomData,
1815            n: a.to_bits(),
1816            increment: 0,
1817            i: 0,
1818            count: 1,
1819        };
1820    }
1821    let trailing_zeros = max_precision - precision;
1822    let increment = u64::power_of_2(trailing_zeros + 1);
1823    let mut start_mantissa = am.round_to_multiple_of_power_of_2(trailing_zeros, Up).0;
1824    if !start_mantissa.get_bit(trailing_zeros) {
1825        start_mantissa.set_bit(trailing_zeros);
1826    }
1827    if start_mantissa > bm {
1828        return ConstantPrecisionPrimitiveFloats::default();
1829    }
1830    let mut end_mantissa = bm.round_to_multiple_of_power_of_2(trailing_zeros, Down).0;
1831    if !end_mantissa.get_bit(trailing_zeros) {
1832        let adjust = u64::power_of_2(trailing_zeros);
1833        if adjust > end_mantissa {
1834            return ConstantPrecisionPrimitiveFloats::default();
1835        }
1836        end_mantissa -= adjust;
1837    }
1838    assert!(start_mantissa <= end_mantissa);
1839    let count = ((end_mantissa - start_mantissa) >> (trailing_zeros + 1)) + 1;
1840    let first = T::from_raw_mantissa_and_exponent(start_mantissa, ae).to_bits();
1841    ConstantPrecisionPrimitiveFloats {
1842        phantom: PhantomData,
1843        n: first,
1844        increment,
1845        i: 0,
1846        count,
1847    }
1848}}
1849
1850#[derive(Clone, Debug)]
1851struct PrimitiveFloatsWithExponentInRangeGenerator<T: PrimitiveFloat> {
1852    a: T,
1853    b: T,
1854    sci_exponent: i64,
1855    phantom: PhantomData<*const T>,
1856}
1857
1858impl<T: PrimitiveFloat>
1859    ExhaustiveDependentPairsYsGenerator<u64, T, ConstantPrecisionPrimitiveFloats<T>>
1860    for PrimitiveFloatsWithExponentInRangeGenerator<T>
1861{
1862    #[inline]
1863    fn get_ys(&self, &precision: &u64) -> ConstantPrecisionPrimitiveFloats<T> {
1864        exhaustive_primitive_floats_with_sci_exponent_and_precision_in_range(
1865            self.a,
1866            self.b,
1867            self.sci_exponent,
1868            precision,
1869        )
1870    }
1871}
1872
1873#[inline]
1874fn exhaustive_primitive_floats_with_sci_exponent_in_range_helper<T: PrimitiveFloat>(
1875    a: T,
1876    b: T,
1877    sci_exponent: i64,
1878) -> LexDependentPairs<
1879    u64,
1880    T,
1881    PrimitiveFloatsWithExponentInRangeGenerator<T>,
1882    PrimitiveIntIncreasingRange<u64>,
1883    ConstantPrecisionPrimitiveFloats<T>,
1884> {
1885    lex_dependent_pairs(
1886        primitive_int_increasing_inclusive_range(
1887            1,
1888            T::max_precision_for_sci_exponent(sci_exponent),
1889        ),
1890        PrimitiveFloatsWithExponentInRangeGenerator {
1891            a,
1892            b,
1893            sci_exponent,
1894            phantom: PhantomData,
1895        },
1896    )
1897}
1898
1899#[doc(hidden)]
1900#[derive(Clone, Debug)]
1901pub struct ExhaustivePrimitiveFloatsWithExponentInRange<T: PrimitiveFloat>(
1902    LexDependentPairs<
1903        u64,
1904        T,
1905        PrimitiveFloatsWithExponentInRangeGenerator<T>,
1906        PrimitiveIntIncreasingRange<u64>,
1907        ConstantPrecisionPrimitiveFloats<T>,
1908    >,
1909);
1910
1911impl<T: PrimitiveFloat> Iterator for ExhaustivePrimitiveFloatsWithExponentInRange<T> {
1912    type Item = T;
1913
1914    #[inline]
1915    fn next(&mut self) -> Option<T> {
1916        self.0.next().map(|p| p.1)
1917    }
1918}
1919
1920#[doc(hidden)]
1921#[inline]
1922pub fn exhaustive_primitive_floats_with_sci_exponent_in_range<T: PrimitiveFloat>(
1923    a: T,
1924    b: T,
1925    sci_exponent: i64,
1926) -> ExhaustivePrimitiveFloatsWithExponentInRange<T> {
1927    ExhaustivePrimitiveFloatsWithExponentInRange(
1928        exhaustive_primitive_floats_with_sci_exponent_in_range_helper(a, b, sci_exponent),
1929    )
1930}
1931
1932#[derive(Clone, Debug)]
1933struct ExhaustivePositiveFinitePrimitiveFloatsInRangeGenerator<T: PrimitiveFloat> {
1934    a: T,
1935    b: T,
1936    a_sci_exponent: i64,
1937    b_sci_exponent: i64,
1938    phantom: PhantomData<*const T>,
1939}
1940
1941impl<T: PrimitiveFloat>
1942    ExhaustiveDependentPairsYsGenerator<i64, T, ExhaustivePrimitiveFloatsWithExponentInRange<T>>
1943    for ExhaustivePositiveFinitePrimitiveFloatsInRangeGenerator<T>
1944{
1945    #[inline]
1946    fn get_ys(&self, &sci_exponent: &i64) -> ExhaustivePrimitiveFloatsWithExponentInRange<T> {
1947        let a = if sci_exponent == self.a_sci_exponent {
1948            self.a
1949        } else {
1950            T::from_integer_mantissa_and_exponent(1, sci_exponent).unwrap()
1951        };
1952        let b = if sci_exponent == self.b_sci_exponent {
1953            self.b
1954        } else {
1955            T::from_integer_mantissa_and_exponent(1, sci_exponent + 1)
1956                .unwrap()
1957                .next_lower()
1958        };
1959        exhaustive_primitive_floats_with_sci_exponent_in_range(a, b, sci_exponent)
1960    }
1961}
1962
1963#[inline]
1964fn exhaustive_positive_finite_primitive_floats_in_range_helper<T: PrimitiveFloat>(
1965    a: T,
1966    b: T,
1967) -> ExhaustiveDependentPairs<
1968    i64,
1969    T,
1970    RulerSequence<usize>,
1971    ExhaustivePositiveFinitePrimitiveFloatsInRangeGenerator<T>,
1972    ExhaustiveSignedRange<i64>,
1973    ExhaustivePrimitiveFloatsWithExponentInRange<T>,
1974> {
1975    assert!(a.is_finite());
1976    assert!(b.is_finite());
1977    assert!(a > T::ZERO);
1978    assert!(a <= b);
1979    let (am, ae) = a.raw_mantissa_and_exponent();
1980    let (bm, be) = b.raw_mantissa_and_exponent();
1981    let a_sci_exponent = if ae == 0 {
1982        i64::wrapping_from(am.significant_bits()) + T::MIN_EXPONENT - 1
1983    } else {
1984        i64::wrapping_from(ae) - T::MAX_EXPONENT
1985    };
1986    let b_sci_exponent = if be == 0 {
1987        i64::wrapping_from(bm.significant_bits()) + T::MIN_EXPONENT - 1
1988    } else {
1989        i64::wrapping_from(be) - T::MAX_EXPONENT
1990    };
1991    exhaustive_dependent_pairs(
1992        ruler_sequence(),
1993        exhaustive_signed_inclusive_range(a_sci_exponent, b_sci_exponent),
1994        ExhaustivePositiveFinitePrimitiveFloatsInRangeGenerator {
1995            a,
1996            b,
1997            a_sci_exponent,
1998            b_sci_exponent,
1999            phantom: PhantomData,
2000        },
2001    )
2002}
2003
2004#[doc(hidden)]
2005#[derive(Clone, Debug)]
2006pub struct ExhaustivePositiveFinitePrimitiveFloatsInRange<T: PrimitiveFloat>(
2007    ExhaustiveDependentPairs<
2008        i64,
2009        T,
2010        RulerSequence<usize>,
2011        ExhaustivePositiveFinitePrimitiveFloatsInRangeGenerator<T>,
2012        ExhaustiveSignedRange<i64>,
2013        ExhaustivePrimitiveFloatsWithExponentInRange<T>,
2014    >,
2015);
2016
2017impl<T: PrimitiveFloat> Iterator for ExhaustivePositiveFinitePrimitiveFloatsInRange<T> {
2018    type Item = T;
2019
2020    #[inline]
2021    fn next(&mut self) -> Option<T> {
2022        self.0.next().map(|p| p.1)
2023    }
2024}
2025
2026#[doc(hidden)]
2027#[inline]
2028pub fn exhaustive_positive_finite_primitive_floats_in_range<T: PrimitiveFloat>(
2029    a: T,
2030    b: T,
2031) -> ExhaustivePositiveFinitePrimitiveFloatsInRange<T> {
2032    ExhaustivePositiveFinitePrimitiveFloatsInRange(
2033        exhaustive_positive_finite_primitive_floats_in_range_helper(a, b),
2034    )
2035}
2036
2037#[doc(hidden)]
2038#[derive(Clone, Debug)]
2039pub enum ExhaustiveNonzeroFinitePrimitiveFloatsInRange<T: PrimitiveFloat> {
2040    AllPositive(ExhaustivePositiveFinitePrimitiveFloatsInRange<T>),
2041    AllNegative(ExhaustivePositiveFinitePrimitiveFloatsInRange<T>),
2042    PositiveAndNegative(
2043        bool,
2044        ExhaustivePositiveFinitePrimitiveFloatsInRange<T>,
2045        ExhaustivePositiveFinitePrimitiveFloatsInRange<T>,
2046    ),
2047}
2048
2049impl<T: PrimitiveFloat> Iterator for ExhaustiveNonzeroFinitePrimitiveFloatsInRange<T> {
2050    type Item = T;
2051
2052    fn next(&mut self) -> Option<T> {
2053        match self {
2054            Self::AllPositive(xs) => xs.next(),
2055            Self::AllNegative(xs) => xs.next().map(T::neg),
2056            Self::PositiveAndNegative(toggle, pos_xs, neg_xs) => {
2057                toggle.not_assign();
2058                if *toggle {
2059                    pos_xs.next().or_else(|| neg_xs.next().map(T::neg))
2060                } else {
2061                    neg_xs.next().map(T::neg).or_else(|| pos_xs.next())
2062                }
2063            }
2064        }
2065    }
2066}
2067
2068#[doc(hidden)]
2069#[inline]
2070pub fn exhaustive_nonzero_finite_primitive_floats_in_range<T: PrimitiveFloat>(
2071    a: T,
2072    b: T,
2073) -> ExhaustiveNonzeroFinitePrimitiveFloatsInRange<T> {
2074    assert!(a.is_finite());
2075    assert!(b.is_finite());
2076    assert!(a != T::ZERO);
2077    assert!(b != T::ZERO);
2078    assert!(a <= b);
2079    if a > T::ZERO {
2080        ExhaustiveNonzeroFinitePrimitiveFloatsInRange::AllPositive(
2081            exhaustive_positive_finite_primitive_floats_in_range(a, b),
2082        )
2083    } else if b < T::ZERO {
2084        ExhaustiveNonzeroFinitePrimitiveFloatsInRange::AllNegative(
2085            exhaustive_positive_finite_primitive_floats_in_range(-b, -a),
2086        )
2087    } else {
2088        ExhaustiveNonzeroFinitePrimitiveFloatsInRange::PositiveAndNegative(
2089            false,
2090            exhaustive_positive_finite_primitive_floats_in_range(T::MIN_POSITIVE_SUBNORMAL, b),
2091            exhaustive_positive_finite_primitive_floats_in_range(T::MIN_POSITIVE_SUBNORMAL, -a),
2092        )
2093    }
2094}
2095
2096/// Generates all primitive floats in an interval.
2097///
2098/// This `enum` is created by [`exhaustive_primitive_float_range`] and
2099/// [`exhaustive_primitive_float_inclusive_range`]; see their documentation for more.
2100#[allow(clippy::large_enum_variant)]
2101#[derive(Clone, Debug)]
2102pub enum ExhaustivePrimitiveFloatInclusiveRange<T: PrimitiveFloat> {
2103    JustSpecials(IntoIter<T>),
2104    NotJustSpecials(Chain<IntoIter<T>, ExhaustiveNonzeroFinitePrimitiveFloatsInRange<T>>),
2105}
2106
2107impl<T: PrimitiveFloat> Iterator for ExhaustivePrimitiveFloatInclusiveRange<T> {
2108    type Item = T;
2109
2110    fn next(&mut self) -> Option<T> {
2111        match self {
2112            Self::JustSpecials(xs) => xs.next(),
2113            Self::NotJustSpecials(xs) => xs.next(),
2114        }
2115    }
2116}
2117
2118/// Generates all primitive floats in the half-open interval $[a, b)$.
2119///
2120/// Positive and negative zero are treated as two distinct values, with negative zero being smaller
2121/// than zero.
2122///
2123/// The floats are generated in a way such that simpler floats (with lower precision) are generated
2124/// first. To generate floats in ascending order instead, use [`primitive_float_increasing_range`]
2125/// instead.
2126///
2127/// `NiceFloat(a)` must be less than or equal to `NiceFloat(b)`. If `NiceFloat(a)` and
2128/// `NiceFloat(b)` are equal, the range is empty.
2129///
2130/// Let $\varphi$ be
2131/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
2132///
2133/// The output length is $\varphi(b) - \varphi(a)$.
2134///
2135/// # Complexity per iteration
2136/// Constant time and additional memory.
2137///
2138/// # Worst-case complexity per iteration
2139/// Constant time and additional memory.
2140///
2141/// # Panics
2142/// Panics if `NiceFloat(a) > NiceFloat(b)`.
2143///
2144/// # Examples
2145/// ```
2146/// use malachite_base::iterators::prefix_to_string;
2147/// use malachite_base::num::exhaustive::exhaustive_primitive_float_range;
2148/// use malachite_base::num::float::NiceFloat;
2149///
2150/// assert_eq!(
2151///     prefix_to_string(
2152///         exhaustive_primitive_float_range::<f32>(core::f32::consts::E, core::f32::consts::PI)
2153///             .map(NiceFloat),
2154///         50
2155///     ),
2156///     "[3.0, 2.75, 2.875, 3.125, 2.8125, 2.9375, 3.0625, 2.71875, 2.78125, 2.84375, 2.90625, \
2157///     2.96875, 3.03125, 3.09375, 2.734375, 2.765625, 2.796875, 2.828125, 2.859375, 2.890625, \
2158///     2.921875, 2.953125, 2.984375, 3.015625, 3.046875, 3.078125, 3.109375, 3.140625, \
2159///     2.7265625, 2.7421875, 2.7578125, 2.7734375, 2.7890625, 2.8046875, 2.8203125, 2.8359375, \
2160///     2.8515625, 2.8671875, 2.8828125, 2.8984375, 2.9140625, 2.9296875, 2.9453125, 2.9609375, \
2161///     2.9765625, 2.9921875, 3.0078125, 3.0234375, 3.0390625, 3.0546875, ...]"
2162/// );
2163/// ```
2164#[inline]
2165pub fn exhaustive_primitive_float_range<T: PrimitiveFloat>(
2166    a: T,
2167    b: T,
2168) -> ExhaustivePrimitiveFloatInclusiveRange<T> {
2169    assert!(!a.is_nan());
2170    assert!(!b.is_nan());
2171    assert!(NiceFloat(a) <= NiceFloat(b));
2172    if NiceFloat(a) == NiceFloat(b) {
2173        ExhaustivePrimitiveFloatInclusiveRange::JustSpecials(Vec::new().into_iter())
2174    } else {
2175        exhaustive_primitive_float_inclusive_range(a, b.next_lower())
2176    }
2177}
2178
2179/// Generates all primitive floats in the closed interval $[a, b]$.
2180///
2181/// Positive and negative zero are treated as two distinct values, with negative zero being smaller
2182/// than zero.
2183///
2184/// The floats are generated in a way such that simpler floats (with lower precision) are generated
2185/// first. To generate floats in ascending order instead, use
2186/// `primitive_float_increasing_inclusive_range` instead.
2187///
2188/// `NiceFloat(a)` must be less than or equal to `NiceFloat(b)`. If `NiceFloat(a)` and
2189/// `NiceFloat(b)` are equal, the range contains a single element.
2190///
2191/// Let $\varphi$ be
2192/// [`to_ordered_representation`](super::basic::floats::PrimitiveFloat::to_ordered_representation):
2193///
2194/// The output length is $\varphi(b) - \varphi(a) + 1$.
2195///
2196/// # Complexity per iteration
2197/// Constant time and additional memory.
2198///
2199/// # Worst-case complexity per iteration
2200/// Constant time and additional memory.
2201///
2202/// # Panics
2203/// Panics if `NiceFloat(a) > NiceFloat(b)`.
2204///
2205/// # Examples
2206/// ```
2207/// use malachite_base::iterators::prefix_to_string;
2208/// use malachite_base::num::exhaustive::exhaustive_primitive_float_inclusive_range;
2209/// use malachite_base::num::float::NiceFloat;
2210///
2211/// assert_eq!(
2212///     prefix_to_string(
2213///         exhaustive_primitive_float_inclusive_range::<f32>(
2214///             core::f32::consts::E,
2215///             core::f32::consts::PI
2216///         )
2217///         .map(NiceFloat),
2218///         50
2219///     ),
2220///     "[3.0, 2.75, 2.875, 3.125, 2.8125, 2.9375, 3.0625, 2.71875, 2.78125, 2.84375, 2.90625, \
2221///     2.96875, 3.03125, 3.09375, 2.734375, 2.765625, 2.796875, 2.828125, 2.859375, 2.890625, \
2222///     2.921875, 2.953125, 2.984375, 3.015625, 3.046875, 3.078125, 3.109375, 3.140625, \
2223///     2.7265625, 2.7421875, 2.7578125, 2.7734375, 2.7890625, 2.8046875, 2.8203125, 2.8359375, \
2224///     2.8515625, 2.8671875, 2.8828125, 2.8984375, 2.9140625, 2.9296875, 2.9453125, 2.9609375, \
2225///     2.9765625, 2.9921875, 3.0078125, 3.0234375, 3.0390625, 3.0546875, ...]"
2226/// );
2227/// ```
2228#[inline]
2229pub fn exhaustive_primitive_float_inclusive_range<T: PrimitiveFloat>(
2230    mut a: T,
2231    mut b: T,
2232) -> ExhaustivePrimitiveFloatInclusiveRange<T> {
2233    assert!(!a.is_nan());
2234    assert!(!b.is_nan());
2235    assert!(NiceFloat(a) <= NiceFloat(b));
2236    let mut specials = Vec::new();
2237    if b == T::INFINITY {
2238        specials.push(T::INFINITY);
2239        if a == T::INFINITY {
2240            return ExhaustivePrimitiveFloatInclusiveRange::JustSpecials(specials.into_iter());
2241        }
2242        b = T::MAX_FINITE;
2243    }
2244    if a == T::NEGATIVE_INFINITY {
2245        specials.push(T::NEGATIVE_INFINITY);
2246        if b == T::NEGATIVE_INFINITY {
2247            return ExhaustivePrimitiveFloatInclusiveRange::JustSpecials(specials.into_iter());
2248        }
2249        a = -T::MAX_FINITE;
2250    }
2251    if NiceFloat(a) <= NiceFloat(T::ZERO) && NiceFloat(b) >= NiceFloat(T::ZERO) {
2252        specials.push(T::ZERO);
2253    }
2254    if NiceFloat(a) <= NiceFloat(T::NEGATIVE_ZERO) && NiceFloat(b) >= NiceFloat(T::NEGATIVE_ZERO) {
2255        specials.push(T::NEGATIVE_ZERO);
2256    }
2257    if a == T::ZERO {
2258        if b == T::ZERO {
2259            return ExhaustivePrimitiveFloatInclusiveRange::JustSpecials(specials.into_iter());
2260        }
2261        a = T::MIN_POSITIVE_SUBNORMAL;
2262    }
2263    if b == T::ZERO {
2264        b = -T::MIN_POSITIVE_SUBNORMAL;
2265    }
2266    ExhaustivePrimitiveFloatInclusiveRange::NotJustSpecials(
2267        specials
2268            .into_iter()
2269            .chain(exhaustive_nonzero_finite_primitive_floats_in_range(a, b)),
2270    )
2271}