malachite_float/conversion/
from_bits.rs

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
// Copyright © 2025 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 crate::Float;
use crate::InnerFloat::Finite;
use alloc::vec;
use core::cmp::Ordering::{self, *};
use malachite_base::num::arithmetic::traits::{
    DivisibleByPowerOf2, NegModPowerOf2, PowerOf2, ShrRound,
};
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::conversion::traits::ExactFrom;
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use malachite_nz::natural::Natural;
use malachite_nz::platform::Limb;

const HIGH_BIT: Limb = 1 << (Limb::WIDTH - 1);

impl Float {
    /// Returns an approximation of a real number, given the number's bits. To avoid troublesome
    /// edge cases, the number should not be a dyadic rational (and the iterator of bits should
    /// therefore be infinite, and not eventually 0 or 1). Given this assumption, the rounding mode
    /// `Exact` should never be passed.
    ///
    /// The approximation has precision `prec` and is rounded according to the provided rounding
    /// mode.
    ///
    /// This function reads `prec + z` bits, or `prec + z + 1` bits if `rm` is `Nearest`, where `z`
    /// is the number of leading false bits in `bits`.
    ///
    /// This function always produces a value in the interval $[1/2,1]$. In particular, it never
    /// overflows or underflows.
    ///
    /// $$
    /// f((x_k),p,m) = C+\varepsilon,
    /// $$
    /// where
    /// $$
    /// C=\sum_{k=0}^\infty x_k 2^{-(k+1)}.
    /// $$
    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 C\rfloor-p+1}$.
    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 C\rfloor-p}$.
    ///
    /// The output has precision `prec`.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
    ///
    /// # Panics
    /// Panics if `prec` is zero or `rm` is `Exact`.
    ///
    /// # Examples
    /// ```
    /// use malachite_base::rounding_modes::RoundingMode::*;
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// // Produces 10100100010000100000...
    /// struct Bits {
    ///     b: bool,
    ///     k: usize,
    ///     j: usize,
    /// }
    ///
    /// impl Iterator for Bits {
    ///     type Item = bool;
    ///
    ///     fn next(&mut self) -> Option<bool> {
    ///         Some(if self.b {
    ///             self.b = false;
    ///             self.j = self.k;
    ///             true
    ///         } else {
    ///             self.j -= 1;
    ///             if self.j == 0 {
    ///                 self.k += 1;
    ///                 self.b = true;
    ///             }
    ///             false
    ///         })
    ///     }
    /// }
    ///
    /// impl Bits {
    ///     fn new() -> Bits {
    ///         Bits {
    ///             b: true,
    ///             k: 1,
    ///             j: 1,
    ///         }
    ///     }
    /// }
    ///
    /// let (c, o) = Float::non_dyadic_from_bits_prec_round(Bits::new(), 100, Floor);
    /// assert_eq!(c.to_string(), "0.6416325606551538662938427702254");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::non_dyadic_from_bits_prec_round(Bits::new(), 100, Ceiling);
    /// assert_eq!(c.to_string(), "0.641632560655153866293842770226");
    /// assert_eq!(o, Greater);
    /// ```
    pub fn non_dyadic_from_bits_prec_round<I: Iterator<Item = bool>>(
        mut bits: I,
        prec: u64,
        rm: RoundingMode,
    ) -> (Float, Ordering) {
        assert_ne!(prec, 0);
        assert_ne!(rm, Exact);
        let len = usize::exact_from(prec.shr_round(Limb::LOG_WIDTH, Ceiling).0);
        let mut limbs = vec![0; len];
        let mut limbs_it = limbs.iter_mut().rev();
        let mut x = limbs_it.next().unwrap();
        let mut mask = HIGH_BIT;
        let mut seen_one = false;
        let mut exponent: i32 = 0;
        let mut remaining = prec;
        for b in &mut bits {
            if !seen_one {
                if b {
                    seen_one = true;
                } else {
                    exponent = exponent.checked_sub(1).unwrap();
                    continue;
                }
            }
            if b {
                *x |= mask;
            }
            remaining -= 1;
            if remaining == 0 {
                break;
            }
            if mask == 1 {
                x = limbs_it.next().unwrap();
                mask = HIGH_BIT;
            } else {
                mask >>= 1;
            }
        }
        let mut significand = Natural::from_owned_limbs_asc(limbs);
        let increment = rm == Up || rm == Ceiling || (rm == Nearest && bits.next() == Some(true));
        if increment {
            significand +=
                Natural::from(Limb::power_of_2(prec.neg_mod_power_of_2(Limb::LOG_WIDTH)));
            if !significand
                .significant_bits()
                .divisible_by_power_of_2(Limb::LOG_WIDTH)
            {
                significand >>= 1;
                exponent += 1;
            }
        }
        (
            Float(Finite {
                sign: true,
                exponent,
                precision: prec,
                significand,
            }),
            if increment { Greater } else { Less },
        )
    }

    /// Returns an approximation of a real number, given the number's bits. To avoid troublesome
    /// edge cases, the number should not be a dyadic rational (and the iterator of bits should
    /// therefore be infinite, and not eventually 0 or 1).
    ///
    /// The approximation has precision `prec` and is rounded according to the `Nearest` rounding
    /// mode.
    ///
    /// This function reads `prec + z + 1` bits, where `z` is the number of leading false bits in
    /// `bits`.
    ///
    /// This function always produces a value in the interval $[1/2,1]$. In particular, it never
    /// overflows or underflows.
    ///
    /// $$
    /// f((x_k),p,m) = C+\varepsilon,
    /// $$
    /// where
    /// $$
    /// C=\sum_{k=0}^\infty x_k 2^{-(k+1)}
    /// $$
    /// and $|\varepsilon| < 2^{\lfloor\log_2 C\rfloor-p}$.
    ///
    /// The output has precision `prec`.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
    ///
    /// # Panics
    /// Panics if `prec` is zero.
    ///
    /// # Examples
    /// ```
    /// use malachite_float::Float;
    /// use std::cmp::Ordering::*;
    ///
    /// // Produces 10100100010000100000...
    /// struct Bits {
    ///     b: bool,
    ///     k: usize,
    ///     j: usize,
    /// }
    ///
    /// impl Iterator for Bits {
    ///     type Item = bool;
    ///
    ///     fn next(&mut self) -> Option<bool> {
    ///         Some(if self.b {
    ///             self.b = false;
    ///             self.j = self.k;
    ///             true
    ///         } else {
    ///             self.j -= 1;
    ///             if self.j == 0 {
    ///                 self.k += 1;
    ///                 self.b = true;
    ///             }
    ///             false
    ///         })
    ///     }
    /// }
    ///
    /// impl Bits {
    ///     fn new() -> Bits {
    ///         Bits {
    ///             b: true,
    ///             k: 1,
    ///             j: 1,
    ///         }
    ///     }
    /// }
    ///
    /// let (c, o) = Float::non_dyadic_from_bits_prec(Bits::new(), 1);
    /// assert_eq!(c.to_string(), "0.5");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::non_dyadic_from_bits_prec(Bits::new(), 10);
    /// assert_eq!(c.to_string(), "0.642");
    /// assert_eq!(o, Less);
    ///
    /// let (c, o) = Float::non_dyadic_from_bits_prec(Bits::new(), 100);
    /// assert_eq!(c.to_string(), "0.6416325606551538662938427702254");
    /// assert_eq!(o, Less);
    /// ```
    #[inline]
    pub fn non_dyadic_from_bits_prec<I: Iterator<Item = bool>>(
        bits: I,
        prec: u64,
    ) -> (Float, Ordering) {
        Float::non_dyadic_from_bits_prec_round(bits, prec, Nearest)
    }
}