Skip to main content

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