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