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