malachite_float/constants/sqrt_5_over_5.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 one fifth of the square root of 5, 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 = \sqrt{5}/5+\varepsilon=\sqrt{1/5}+\varepsilon=1/\sqrt{5}+\varepsilon.
21 /// $$
22 /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p-1}$.
23 /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p-2}$.
24 ///
25 /// The constant is irrational and algebraic.
26 ///
27 /// The output has precision `prec`.
28 ///
29 /// # Worst-case complexity
30 /// $T(n) = O(n \log n \log\log n)$
31 ///
32 /// $M(n) = O(n \log n)$
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 (sqrt_5_over_5, o) = Float::sqrt_5_over_5_prec_round(100, Floor);
46 /// assert_eq!(
47 /// sqrt_5_over_5.to_string(),
48 /// "0.447213595499957939281834733746"
49 /// );
50 /// assert_eq!(o, Less);
51 ///
52 /// let (sqrt_5_over_5, o) = Float::sqrt_5_over_5_prec_round(100, Ceiling);
53 /// assert_eq!(
54 /// sqrt_5_over_5.to_string(),
55 /// "0.4472135954999579392818347337464"
56 /// );
57 /// assert_eq!(o, Greater);
58 /// ```
59 #[inline]
60 pub fn sqrt_5_over_5_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
61 Self::reciprocal_sqrt_prec_round(const { Self::const_from_unsigned(5) }, prec, rm)
62 }
63
64 /// Returns an approximation of one fifth of the square root of 5, with the given precision and
65 /// rounded to the nearest [`Float`] of that precision. An [`Ordering`] is also returned,
66 /// indicating whether the rounded value is less than or greater than the exact value of the
67 /// constant. (Since the constant is irrational, the rounded value is never equal to the exact
68 /// value.)
69 ///
70 /// $$
71 /// x = \sqrt{5}/5+\varepsilon=\sqrt{1/5}+\varepsilon=1/\sqrt{5}+\varepsilon.
72 /// $$
73 /// - $|\varepsilon| < 2^{-p-2}$.
74 ///
75 /// The constant is irrational and algebraic.
76 ///
77 /// The output has precision `prec`.
78 ///
79 /// # Worst-case complexity
80 /// $T(n) = O(n \log n \log\log n)$
81 ///
82 /// $M(n) = O(n \log n)$
83 ///
84 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
85 ///
86 /// # Panics
87 /// Panics if `prec` is zero.
88 ///
89 /// # Examples
90 /// ```
91 /// use malachite_float::Float;
92 /// use std::cmp::Ordering::*;
93 ///
94 /// let (sqrt_5_over_5, o) = Float::sqrt_5_over_5_prec(1);
95 /// assert_eq!(sqrt_5_over_5.to_string(), "0.5");
96 /// assert_eq!(o, Greater);
97 ///
98 /// let (sqrt_5_over_5, o) = Float::sqrt_5_over_5_prec(10);
99 /// assert_eq!(sqrt_5_over_5.to_string(), "0.4473");
100 /// assert_eq!(o, Greater);
101 ///
102 /// let (sqrt_5_over_5, o) = Float::sqrt_5_over_5_prec(100);
103 /// assert_eq!(
104 /// sqrt_5_over_5.to_string(),
105 /// "0.4472135954999579392818347337464"
106 /// );
107 /// assert_eq!(o, Greater);
108 /// ```
109 #[inline]
110 pub fn sqrt_5_over_5_prec(prec: u64) -> (Self, Ordering) {
111 Self::sqrt_5_over_5_prec_round(prec, Nearest)
112 }
113}