Skip to main content

malachite_float/constants/
phi.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::{self, *};
11use malachite_base::num::basic::traits::{One, Two};
12use malachite_base::rounding_modes::RoundingMode::{self, *};
13
14impl Float {
15    /// Returns an approximation of the golden ratio, with the given precision and rounded using the
16    /// given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded
17    /// value is less than or greater than the exact value of the constant. (Since the constant is
18    /// irrational, the rounded value is never equal to the exact value.)
19    ///
20    /// $$
21    /// \varphi = \frac{1+\sqrt{2}}{2}+\varepsilon.
22    /// $$
23    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p}$.
24    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p-1}$.
25    ///
26    /// The constant is irrational and algebraic.
27    ///
28    /// The output has precision `prec`.
29    ///
30    /// # Worst-case complexity
31    /// $T(n) = O(n \log n \log\log n)$
32    ///
33    /// $M(n) = O(n \log n)$
34    ///
35    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
36    ///
37    /// # Panics
38    /// Panics if `prec` is zero or if `rm` is `Exact`.
39    ///
40    /// # Examples
41    /// ```
42    /// use malachite_base::rounding_modes::RoundingMode::*;
43    /// use malachite_float::Float;
44    /// use std::cmp::Ordering::*;
45    ///
46    /// let (phi, o) = Float::phi_prec_round(100, Floor);
47    /// assert_eq!(phi.to_string(), "1.618033988749894848204586834364");
48    /// assert_eq!(o, Less);
49    ///
50    /// let (phi, o) = Float::phi_prec_round(100, Ceiling);
51    /// assert_eq!(phi.to_string(), "1.618033988749894848204586834366");
52    /// assert_eq!(o, Greater);
53    /// ```
54    #[inline]
55    pub fn phi_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
56        if prec == 1 {
57            match rm {
58                Floor | Down => (Self::ONE, Less),
59                Ceiling | Up | Nearest => (Self::TWO, Greater),
60                Exact => panic!("Inexact float square root"),
61            }
62        } else {
63            let (sqrt_5, o) =
64                Self::sqrt_prec_round(const { Self::const_from_unsigned(5) }, prec, rm);
65            ((sqrt_5 + Self::ONE) >> 1u32, o)
66        }
67    }
68
69    /// Returns an approximation of the golden ratio, with the given precision and rounded to the
70    /// nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating whether
71    /// the rounded value is less than or greater than the exact value of the constant. (Since the
72    /// constant is irrational, the rounded value is never equal to the exact value.)
73    ///
74    /// $$
75    /// \varphi = \frac{1+\sqrt{2}}{2}+\varepsilon.
76    /// $$
77    /// - $|\varepsilon| < 2^{-p}$.
78    ///
79    /// The constant is irrational and algebraic.
80    ///
81    /// The output has precision `prec`.
82    ///
83    /// # Worst-case complexity
84    /// $T(n) = O(n \log n \log\log n)$
85    ///
86    /// $M(n) = O(n \log n)$
87    ///
88    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
89    ///
90    /// # Panics
91    /// Panics if `prec` is zero.
92    ///
93    /// # Examples
94    /// ```
95    /// use malachite_float::Float;
96    /// use std::cmp::Ordering::*;
97    ///
98    /// let (phi, o) = Float::phi_prec(1);
99    /// assert_eq!(phi.to_string(), "2.0");
100    /// assert_eq!(o, Greater);
101    ///
102    /// let (phi, o) = Float::phi_prec(10);
103    /// assert_eq!(phi.to_string(), "1.617");
104    /// assert_eq!(o, Less);
105    ///
106    /// let (phi, o) = Float::phi_prec(100);
107    /// assert_eq!(phi.to_string(), "1.618033988749894848204586834366");
108    /// assert_eq!(o, Greater);
109    /// ```
110    #[inline]
111    pub fn phi_prec(prec: u64) -> (Self, Ordering) {
112        Self::phi_prec_round(prec, Nearest)
113    }
114}