malachite_float/constants/sqrt_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 the square root of 3, with the given precision and rounded using
15 /// the given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded
16 /// value is less than or greater than the exact value of the constant. (Since the constant is
17 /// irrational, the rounded value is never equal to the exact value.)
18 ///
19 /// $$
20 /// x = \sqrt{3}+\varepsilon.
21 /// $$
22 /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p+1}$.
23 /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p}$.
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, o) = Float::sqrt_3_prec_round(100, Floor);
46 /// assert_eq!(sqrt_3.to_string(), "1.732050807568877293527446341505");
47 /// assert_eq!(o, Less);
48 ///
49 /// let (sqrt_3, o) = Float::sqrt_3_prec_round(100, Ceiling);
50 /// assert_eq!(sqrt_3.to_string(), "1.732050807568877293527446341506");
51 /// assert_eq!(o, Greater);
52 /// ```
53 #[inline]
54 pub fn sqrt_3_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
55 Self::sqrt_prec_round(const { Self::const_from_unsigned(3) }, prec, rm)
56 }
57
58 /// Returns an approximation of the square root of 3, with the given precision and rounded to
59 /// the nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating
60 /// whether the rounded value is less than or greater than the exact value of the constant.
61 /// (Since the constant is irrational, the rounded value is never equal to the exact value.)
62 ///
63 /// $$
64 /// x = \sqrt{3}+\varepsilon.
65 /// $$
66 /// - $|\varepsilon| < 2^{-p}$.
67 ///
68 /// The constant is irrational and algebraic.
69 ///
70 /// The output has precision `prec`.
71 ///
72 /// # Worst-case complexity
73 /// $T(n) = O(n \log n \log\log n)$
74 ///
75 /// $M(n) = O(n \log n)$
76 ///
77 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
78 ///
79 /// # Panics
80 /// Panics if `prec` is zero.
81 ///
82 /// # Examples
83 /// ```
84 /// use malachite_float::Float;
85 /// use std::cmp::Ordering::*;
86 ///
87 /// let (sqrt_3, o) = Float::sqrt_3_prec(1);
88 /// assert_eq!(sqrt_3.to_string(), "2.0");
89 /// assert_eq!(o, Greater);
90 ///
91 /// let (sqrt_3, o) = Float::sqrt_3_prec(10);
92 /// assert_eq!(sqrt_3.to_string(), "1.732");
93 /// assert_eq!(o, Greater);
94 ///
95 /// let (sqrt_3, o) = Float::sqrt_3_prec(100);
96 /// assert_eq!(sqrt_3.to_string(), "1.732050807568877293527446341506");
97 /// assert_eq!(o, Greater);
98 /// ```
99 #[inline]
100 pub fn sqrt_3_prec(prec: u64) -> (Self, Ordering) {
101 Self::sqrt_3_prec_round(prec, Nearest)
102 }
103}