malachite_float/basic/
complexity.rs

1// Copyright © 2025 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::Float;
10use crate::InnerFloat::Finite;
11use core::cmp::max;
12use malachite_base::num::logic::traits::SignificantBits;
13
14impl Float {
15    /// Determines a [`Float`]'s complexity. The complexity is defined as follows:
16    ///
17    /// $$
18    /// f(\text{NaN}) = f(\pm\infty) = f(\pm 0.0) = 1,
19    /// $$
20    ///
21    /// and, if $x$ is finite and nonzero,
22    ///
23    /// $$
24    /// f(x) = \max(|\lfloor \log_2 x\rfloor|, p),
25    /// $$
26    ///
27    /// where $p$ is the precision of $x$.
28    ///
29    /// Informally, the complexity is proportional to the number of characters you would need to
30    /// write the [`Float`] out without using exponents.
31    ///
32    /// See also the [`Float`] implementation of [`SignificantBits`].
33    ///
34    /// # Worst-case complexity
35    /// Constant time and additional memory.
36    ///
37    /// # Examples
38    /// ```
39    /// use malachite_base::num::arithmetic::traits::PowerOf2;
40    /// use malachite_base::num::basic::traits::{NaN, One};
41    /// use malachite_float::Float;
42    ///
43    /// assert_eq!(Float::NAN.complexity(), 1);
44    /// assert_eq!(Float::ONE.complexity(), 1);
45    /// assert_eq!(Float::one_prec(100).complexity(), 100);
46    /// assert_eq!(Float::from(std::f64::consts::PI).complexity(), 50);
47    /// assert_eq!(Float::power_of_2(100u64).complexity(), 100);
48    /// assert_eq!(Float::power_of_2(-100i64).complexity(), 100);
49    /// ```
50    pub fn complexity(&self) -> u64 {
51        match self {
52            Float(Finite {
53                exponent,
54                precision,
55                ..
56            }) => max(u64::from((exponent - 1).unsigned_abs()), *precision),
57            _ => 1,
58        }
59    }
60}
61
62impl SignificantBits for &Float {
63    /// Returns the number of significant bits of a [`Float`]. This is defined as follows:
64    ///
65    /// $$
66    /// f(\text{NaN}) = f(\pm\infty) = f(\pm 0.0) = 1,
67    /// $$
68    ///
69    /// and, if $x$ is finite and nonzero,
70    ///
71    /// $$
72    /// f(x) = p,
73    /// $$
74    ///
75    /// where $p$ is the precision of $x$.
76    ///
77    /// See also the [`complexity`](Float::complexity) function.
78    ///
79    /// # Worst-case complexity
80    /// Constant time and additional memory.
81    ///
82    /// # Examples
83    /// ```
84    /// use malachite_base::num::arithmetic::traits::PowerOf2;
85    /// use malachite_base::num::basic::traits::{NaN, One};
86    /// use malachite_base::num::logic::traits::SignificantBits;
87    /// use malachite_float::Float;
88    ///
89    /// assert_eq!(Float::NAN.significant_bits(), 1);
90    /// assert_eq!(Float::ONE.significant_bits(), 1);
91    /// assert_eq!(Float::one_prec(100).significant_bits(), 100);
92    /// assert_eq!(Float::from(std::f64::consts::PI).significant_bits(), 50);
93    /// assert_eq!(Float::power_of_2(100u64).significant_bits(), 1);
94    /// assert_eq!(Float::power_of_2(-100i64).significant_bits(), 1);
95    /// ```
96    fn significant_bits(self) -> u64 {
97        match self {
98            Float(Finite { precision, .. }) => *precision,
99            _ => 1,
100        }
101    }
102}