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