Skip to main content

malachite_float/constants/
sqrt_3_over_3.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 third of the square root of 3, 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{3}/3+\varepsilon=\sqrt{1/3}+\varepsilon=1/\sqrt{3}+\varepsilon.
21    /// $$
22    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p}$.
23    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p-1}$.
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_3_over_3, o) = Float::sqrt_3_over_3_prec_round(100, Floor);
46    /// assert_eq!(
47    ///     sqrt_3_over_3.to_string(),
48    ///     "0.577350269189625764509148780501"
49    /// );
50    /// assert_eq!(o, Less);
51    ///
52    /// let (sqrt_3_over_3, o) = Float::sqrt_3_over_3_prec_round(100, Ceiling);
53    /// assert_eq!(
54    ///     sqrt_3_over_3.to_string(),
55    ///     "0.577350269189625764509148780502"
56    /// );
57    /// assert_eq!(o, Greater);
58    /// ```
59    #[inline]
60    pub fn sqrt_3_over_3_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
61        Self::reciprocal_sqrt_prec_round(const { Self::const_from_unsigned(3) }, prec, rm)
62    }
63
64    /// Returns an approximation of one third of the square root of 3, 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{3}/3+\varepsilon=\sqrt{1/3}+\varepsilon=1/\sqrt{3}+\varepsilon.
72    /// $$
73    /// - $|\varepsilon| < 2^{-p-1}$.
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_3_over_3, o) = Float::sqrt_3_over_3_prec(1);
95    /// assert_eq!(sqrt_3_over_3.to_string(), "0.5");
96    /// assert_eq!(o, Less);
97    ///
98    /// let (sqrt_3_over_3, o) = Float::sqrt_3_over_3_prec(10);
99    /// assert_eq!(sqrt_3_over_3.to_string(), "0.577");
100    /// assert_eq!(o, Less);
101    ///
102    /// let (sqrt_3_over_3, o) = Float::sqrt_3_over_3_prec(100);
103    /// assert_eq!(
104    ///     sqrt_3_over_3.to_string(),
105    ///     "0.577350269189625764509148780502"
106    /// );
107    /// assert_eq!(o, Greater);
108    /// ```
109    #[inline]
110    pub fn sqrt_3_over_3_prec(prec: u64) -> (Self, Ordering) {
111        Self::sqrt_3_over_3_prec_round(prec, Nearest)
112    }
113}