Skip to main content

malachite_float/constants/
ln_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 natural logarithm of 10, with the given precision and
15    /// rounded using the given [`RoundingMode`]. An [`Ordering`] is also returned, indicating
16    /// whether the rounded value is less than or greater than the exact value of the constant.
17    /// (Since the constant is irrational, the rounded value is never equal to the exact value.)
18    ///
19    /// $$
20    /// x = \ln 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 (l10, o) = Float::ln_10_prec_round(100, Floor);
46    /// assert_eq!(l10.to_string(), "2.302585092994045684017991454684");
47    /// assert_eq!(o, Less);
48    ///
49    /// let (l10, o) = Float::ln_10_prec_round(100, Ceiling);
50    /// assert_eq!(l10.to_string(), "2.302585092994045684017991454687");
51    /// assert_eq!(o, Greater);
52    /// ```
53    #[inline]
54    pub fn ln_10_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
55        Self::ln_prec_round(const { Self::const_from_unsigned(10) }, prec, rm)
56    }
57
58    /// Returns an approximation of the natural logarithm of 10, with the given precision and
59    /// rounded to the nearest [`Float`] of that precision. An [`Ordering`] is also returned,
60    /// indicating whether the rounded value is less than or greater than the exact value of the
61    /// constant. (Since the constant is irrational, the rounded value is never equal to the exact
62    /// value.)
63    ///
64    /// $$
65    /// x = \ln 10+\varepsilon.
66    /// $$
67    /// - $|\varepsilon| < 2^{-p+1}$.
68    ///
69    /// The constant is irrational and transcendental.
70    ///
71    /// The output has precision `prec`.
72    ///
73    /// # Worst-case complexity
74    /// $T(n) = O(n (\log n)^2 \log\log n)$
75    ///
76    /// $M(n) = O(n (\log n)^2)$
77    ///
78    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
79    ///
80    /// # Panics
81    /// Panics if `prec` is zero.
82    ///
83    /// # Examples
84    /// ```
85    /// use malachite_float::Float;
86    /// use std::cmp::Ordering::*;
87    ///
88    /// let (l10, o) = Float::ln_10_prec(1);
89    /// assert_eq!(l10.to_string(), "2.0");
90    /// assert_eq!(o, Less);
91    ///
92    /// let (l10, o) = Float::ln_10_prec(10);
93    /// assert_eq!(l10.to_string(), "2.301");
94    /// assert_eq!(o, Less);
95    ///
96    /// let (l10, o) = Float::ln_10_prec(100);
97    /// assert_eq!(l10.to_string(), "2.302585092994045684017991454684");
98    /// assert_eq!(o, Less);
99    /// ```
100    #[inline]
101    pub fn ln_10_prec(prec: u64) -> (Self, Ordering) {
102        Self::ln_10_prec_round(prec, Nearest)
103    }
104}