Skip to main content

malachite_float/float/constants/
gelfonds_constant.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, floor_and_ceiling};
10use core::cmp::Ordering::{self, *};
11use malachite_base::num::basic::integers::PrimitiveInt;
12use malachite_base::rounding_modes::RoundingMode::{self, *};
13use malachite_nz::platform::Limb;
14
15impl Float {
16    /// Returns an approximation of Gelfond's constant, $e^\pi$, with the given precision and
17    /// rounded using the given [`RoundingMode`]. An [`Ordering`] is also returned, indicating
18    /// whether the rounded value is less than or greater than the exact value of the constant.
19    /// (Since the constant is irrational, the rounded value is never equal to the exact value.)
20    ///
21    /// $$
22    /// x = e^\pi+\varepsilon.
23    /// $$
24    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p+5}$.
25    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p+4}$.
26    ///
27    /// The constant is irrational and transcendental.
28    ///
29    /// The output has precision `prec`.
30    ///
31    /// # Worst-case complexity
32    /// $T(n) = O(n^{3/2} \log n \log\log n)$
33    ///
34    /// $M(n) = O(n \log n)$
35    ///
36    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
37    ///
38    /// # Panics
39    /// Panics if `prec` is zero or if `rm` is `Exact`.
40    ///
41    /// # Examples
42    /// ```
43    /// use malachite_base::rounding_modes::RoundingMode::*;
44    /// use malachite_float::Float;
45    /// use std::cmp::Ordering::*;
46    ///
47    /// let (gelfonds_constant, o) = Float::gelfonds_constant_prec_round(100, Floor);
48    /// assert_eq!(
49    ///     gelfonds_constant.to_string(),
50    ///     "23.140692632779269005729086367940"
51    /// );
52    /// assert_eq!(o, Less);
53    ///
54    /// let (gelfonds_constant, o) = Float::gelfonds_constant_prec_round(100, Ceiling);
55    /// assert_eq!(
56    ///     gelfonds_constant.to_string(),
57    ///     "23.140692632779269005729086367965"
58    /// );
59    /// assert_eq!(o, Greater);
60    /// ```
61    pub fn gelfonds_constant_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
62        let mut working_prec = prec + 10;
63        let mut increment = Limb::WIDTH;
64        loop {
65            let (pi_lo, pi_hi) = floor_and_ceiling(Self::pi_prec_round(working_prec, Floor));
66            // exp is increasing, so exp(pi_lo) <= exp(pi) <= exp(pi_hi).
67            let (gelfonds_constant_lo, mut o_lo) =
68                Self::from_float_prec_round(pi_lo.exp_round(Floor).0, prec, rm);
69            let (gelfonds_constant_hi, mut o_hi) =
70                Self::from_float_prec_round(pi_hi.exp_round(Ceiling).0, prec, rm);
71            if o_lo == Equal {
72                o_lo = o_hi;
73            }
74            if o_hi == Equal {
75                o_hi = o_lo;
76            }
77            if o_lo == o_hi && gelfonds_constant_lo == gelfonds_constant_hi {
78                return (gelfonds_constant_lo, o_lo);
79            }
80            working_prec += increment;
81            increment = working_prec >> 1;
82        }
83    }
84
85    /// Returns an approximation of Gelfond's constant, $e^\pi$, with the given precision and
86    /// rounded to the nearest [`Float`] of that precision. An [`Ordering`] is also returned,
87    /// indicating whether the rounded value is less than or greater than the exact value of the
88    /// constant. (Since the constant is irrational, the rounded value is never equal to the exact
89    /// value.)
90    ///
91    /// $$
92    /// x = e^\pi+\varepsilon.
93    /// $$
94    /// - $|\varepsilon| < 2^{-p+4}$.
95    ///
96    /// The constant is irrational and transcendental.
97    ///
98    /// The output has precision `prec`.
99    ///
100    /// # Worst-case complexity
101    /// $T(n) = O(n^{3/2} \log n \log\log n)$
102    ///
103    /// $M(n) = O(n \log n)$
104    ///
105    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
106    ///
107    /// # Panics
108    /// Panics if `prec` is zero.
109    ///
110    /// # Examples
111    /// ```
112    /// use malachite_float::Float;
113    /// use std::cmp::Ordering::*;
114    ///
115    /// let (gelfonds_constant, o) = Float::gelfonds_constant_prec(1);
116    /// assert_eq!(gelfonds_constant.to_string(), "16.0");
117    /// assert_eq!(o, Less);
118    ///
119    /// let (gelfonds_constant, o) = Float::gelfonds_constant_prec(10);
120    /// assert_eq!(gelfonds_constant.to_string(), "23.156");
121    /// assert_eq!(o, Greater);
122    ///
123    /// let (gelfonds_constant, o) = Float::gelfonds_constant_prec(100);
124    /// assert_eq!(
125    ///     gelfonds_constant.to_string(),
126    ///     "23.140692632779269005729086367940"
127    /// );
128    /// assert_eq!(o, Less);
129    /// ```
130    #[inline]
131    pub fn gelfonds_constant_prec(prec: u64) -> (Self, Ordering) {
132        Self::gelfonds_constant_prec_round(prec, Nearest)
133    }
134}