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