Skip to main content

malachite_float/constants/
two_over_sqrt_pi.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5//      Copyright 1999, 2001-2024 Free Software Foundation, Inc.
6//
7//      Contributed by the AriC and Caramba projects, INRIA.
8//
9// This file is part of Malachite.
10//
11// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
12// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
13// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
14
15use crate::Float;
16use core::cmp::Ordering;
17use malachite_base::rounding_modes::RoundingMode::{self, *};
18
19impl Float {
20    /// Returns an approximation of $2/\sqrt{pi}$, with the given precision and rounded using the
21    /// given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded
22    /// value is less than or greater than the exact value of the constant. (Since the constant is
23    /// irrational, the rounded value is never equal to the exact value.)
24    ///
25    /// $$
26    /// x = 2/\sqrt{pi}+\varepsilon.
27    /// $$
28    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p+1}$.
29    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p}$.
30    ///
31    /// The constant is irrational and transcendental.
32    ///
33    /// The output has precision `prec`.
34    ///
35    /// # Worst-case complexity
36    /// $T(n) = O(n (\log n)^2 \log\log n)$
37    ///
38    /// $M(n) = O(n (\log n)^2)$
39    ///
40    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
41    ///
42    /// # Panics
43    /// Panics if `prec` is zero or if `rm` is `Exact`.
44    ///
45    /// # Examples
46    /// ```
47    /// use malachite_base::rounding_modes::RoundingMode::*;
48    /// use malachite_float::Float;
49    /// use std::cmp::Ordering::*;
50    ///
51    /// let (two_over_sqrt_pi, o) = Float::two_over_sqrt_pi_prec_round(100, Floor);
52    /// assert_eq!(
53    ///     two_over_sqrt_pi.to_string(),
54    ///     "1.12837916709551257389615890312"
55    /// );
56    /// assert_eq!(o, Less);
57    ///
58    /// let (two_over_sqrt_pi, o) = Float::two_over_sqrt_pi_prec_round(100, Ceiling);
59    /// assert_eq!(
60    ///     two_over_sqrt_pi.to_string(),
61    ///     "1.128379167095512573896158903122"
62    /// );
63    /// assert_eq!(o, Greater);
64    /// ```
65    #[inline]
66    pub fn two_over_sqrt_pi_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
67        let (pi, o) = Self::one_over_sqrt_pi_prec_round(prec, rm);
68        (pi << 1u32, o)
69    }
70
71    /// Returns an approximation of $2/\sqrt{pi}$, with the given precision and rounded to the
72    /// nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating whether
73    /// the rounded value is less than or greater than the exact value of the constant. (Since the
74    /// constant is irrational, the rounded value is never equal to the exact value.)
75    ///
76    /// $$
77    /// x = 2/\sqrt{pi}+\varepsilon.
78    /// $$
79    /// - $|\varepsilon| < 2^{-p}$.
80    ///
81    /// The constant is irrational and transcendental.
82    ///
83    /// The output has precision `prec`.
84    ///
85    /// # Worst-case complexity
86    /// $T(n) = O(n (\log n)^2 \log\log n)$
87    ///
88    /// $M(n) = O(n (\log n)^2)$
89    ///
90    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
91    ///
92    /// # Panics
93    /// Panics if `prec` is zero.
94    ///
95    /// # Examples
96    /// ```
97    /// use malachite_float::Float;
98    /// use std::cmp::Ordering::*;
99    ///
100    /// let (two_over_sqrt_pi, o) = Float::two_over_sqrt_pi_prec(1);
101    /// assert_eq!(two_over_sqrt_pi.to_string(), "1.0");
102    /// assert_eq!(o, Less);
103    ///
104    /// let (two_over_sqrt_pi, o) = Float::two_over_sqrt_pi_prec(10);
105    /// assert_eq!(two_over_sqrt_pi.to_string(), "1.129");
106    /// assert_eq!(o, Greater);
107    ///
108    /// let (two_over_sqrt_pi, o) = Float::two_over_sqrt_pi_prec(100);
109    /// assert_eq!(
110    ///     two_over_sqrt_pi.to_string(),
111    ///     "1.128379167095512573896158903122"
112    /// );
113    /// assert_eq!(o, Greater);
114    /// ```
115    #[inline]
116    pub fn two_over_sqrt_pi_prec(prec: u64) -> (Self, Ordering) {
117        Self::two_over_sqrt_pi_prec_round(prec, Nearest)
118    }
119}