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