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