Skip to main content

malachite_float/constants/
log_2_10.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::Float;
10use core::cmp::Ordering;
11use malachite_base::rounding_modes::RoundingMode::{self, *};
12
13impl Float {
14    /// Returns an approximation of the base-2 logarithm of 10, with the given precision and rounded
15    /// using the given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the
16    /// rounded value is less than or greater than the exact value of the constant. (Since the
17    /// constant is irrational, the rounded value is never equal to the exact value.)
18    ///
19    /// $$
20    /// x = \log_2 10+\varepsilon.
21    /// $$
22    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p+2}$.
23    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p+1}$.
24    ///
25    /// The constant is irrational and transcendental.
26    ///
27    /// The output has precision `prec`.
28    ///
29    /// # Worst-case complexity
30    /// $T(n) = O(n (\log n)^2 \log\log n)$
31    ///
32    /// $M(n) = O(n (\log n)^2)$
33    ///
34    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
35    ///
36    /// # Panics
37    /// Panics if `prec` is zero or if `rm` is `Exact`.
38    ///
39    /// # Examples
40    /// ```
41    /// use malachite_base::rounding_modes::RoundingMode::*;
42    /// use malachite_float::Float;
43    /// use std::cmp::Ordering::*;
44    ///
45    /// let (l, o) = Float::log_2_10_prec_round(100, Floor);
46    /// assert_eq!(l.to_string(), "3.321928094887362347870319429487");
47    /// assert_eq!(o, Less);
48    ///
49    /// let (l, o) = Float::log_2_10_prec_round(100, Ceiling);
50    /// assert_eq!(l.to_string(), "3.32192809488736234787031942949");
51    /// assert_eq!(o, Greater);
52    /// ```
53    #[inline]
54    pub fn log_2_10_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
55        Self::log_base_2_prec_round(const { Self::const_from_unsigned(10) }, prec, rm)
56    }
57
58    /// Returns an approximation of the base-2 logarithm of 10, with the given precision and rounded
59    /// to the nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating
60    /// whether the rounded value is less than or greater than the exact value of the constant.
61    /// (Since the constant is irrational, the rounded value is never equal to the exact value.)
62    ///
63    /// $$
64    /// x = \log_2 10+\varepsilon.
65    /// $$
66    /// - $|\varepsilon| < 2^{-p+1}$.
67    ///
68    /// The constant is irrational and transcendental.
69    ///
70    /// The output has precision `prec`.
71    ///
72    /// # Worst-case complexity
73    /// $T(n) = O(n (\log n)^2 \log\log n)$
74    ///
75    /// $M(n) = O(n (\log n)^2)$
76    ///
77    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
78    ///
79    /// # Panics
80    /// Panics if `prec` is zero.
81    ///
82    /// # Examples
83    /// ```
84    /// use malachite_float::Float;
85    /// use std::cmp::Ordering::*;
86    ///
87    /// let (l, o) = Float::log_2_10_prec(1);
88    /// assert_eq!(l.to_string(), "4.0");
89    /// assert_eq!(o, Greater);
90    ///
91    /// let (l, o) = Float::log_2_10_prec(10);
92    /// assert_eq!(l.to_string(), "3.32");
93    /// assert_eq!(o, Less);
94    ///
95    /// let (l, o) = Float::log_2_10_prec(100);
96    /// assert_eq!(l.to_string(), "3.32192809488736234787031942949");
97    /// assert_eq!(o, Greater);
98    /// ```
99    #[inline]
100    pub fn log_2_10_prec(prec: u64) -> (Self, Ordering) {
101        Self::log_2_10_prec_round(prec, Nearest)
102    }
103}