malachite_float/constants/one_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::num::arithmetic::traits::ReciprocalSqrt;
18use malachite_base::num::basic::integers::PrimitiveInt;
19use malachite_base::rounding_modes::RoundingMode::{self, *};
20use malachite_nz::natural::arithmetic::float_extras::float_can_round;
21use malachite_nz::platform::Limb;
22
23impl Float {
24 /// Returns an approximation of $1/\sqrt{\pi}$, with the given precision and rounded using the
25 /// given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded
26 /// value is less than or greater than the exact value of the constant. (Since the constant is
27 /// irrational, the rounded value is never equal to the exact value.)
28 ///
29 /// $$
30 /// x = 1/\sqrt{\pi}+\varepsilon.
31 /// $$
32 /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p}$.
33 /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p-1}$.
34 ///
35 /// The constant is irrational and transcendental.
36 ///
37 /// The output has precision `prec`.
38 ///
39 /// # Worst-case complexity
40 /// $T(n) = O(n (\log n)^2 \log\log n)$
41 ///
42 /// $M(n) = O(n (\log n)^2)$
43 ///
44 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
45 ///
46 /// # Panics
47 /// Panics if `prec` is zero or if `rm` is `Exact`.
48 ///
49 /// # Examples
50 /// ```
51 /// use malachite_base::rounding_modes::RoundingMode::*;
52 /// use malachite_float::Float;
53 /// use std::cmp::Ordering::*;
54 ///
55 /// let (one_over_sqrt_pi, o) = Float::one_over_sqrt_pi_prec_round(100, Floor);
56 /// assert_eq!(
57 /// one_over_sqrt_pi.to_string(),
58 /// "0.56418958354775628694807945156"
59 /// );
60 /// assert_eq!(o, Less);
61 ///
62 /// let (one_over_sqrt_pi, o) = Float::one_over_sqrt_pi_prec_round(100, Ceiling);
63 /// assert_eq!(
64 /// one_over_sqrt_pi.to_string(),
65 /// "0.564189583547756286948079451561"
66 /// );
67 /// assert_eq!(o, Greater);
68 /// ```
69 pub fn one_over_sqrt_pi_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
70 let mut working_prec = prec + 10;
71 let mut increment = Limb::WIDTH;
72 loop {
73 let one_over_sqrt_pi = Self::pi_prec_round(working_prec, Floor).0.reciprocal_sqrt();
74 if float_can_round(
75 one_over_sqrt_pi.significand_ref().unwrap(),
76 working_prec - 2,
77 prec,
78 rm,
79 ) {
80 return Self::from_float_prec_round(one_over_sqrt_pi, prec, rm);
81 }
82 working_prec += increment;
83 increment = working_prec >> 1;
84 }
85 }
86
87 /// Returns an approximation of $1/\sqrt{\pi}$, with the given precision and rounded to the
88 /// nearest [`Float`] of that precision. An [`Ordering`] is also returned, indicating whether
89 /// the rounded value is less than or greater than the exact value of the constant. (Since the
90 /// constant is irrational, the rounded value is never equal to the exact value.)
91 ///
92 /// $$
93 /// x = 1/\sqrt{\pi}+\varepsilon.
94 /// $$
95 /// - $|\varepsilon| < 2^{-p-1}$.
96 ///
97 /// The constant is irrational and transcendental.
98 ///
99 /// The output has precision `prec`.
100 ///
101 /// # Worst-case complexity
102 /// $T(n) = O(n (\log n)^2 \log\log n)$
103 ///
104 /// $M(n) = O(n (\log n)^2)$
105 ///
106 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
107 ///
108 /// # Panics
109 /// Panics if `prec` is zero.
110 ///
111 /// # Examples
112 /// ```
113 /// use malachite_float::Float;
114 /// use std::cmp::Ordering::*;
115 ///
116 /// let (one_over_sqrt_pi, o) = Float::one_over_sqrt_pi_prec(1);
117 /// assert_eq!(one_over_sqrt_pi.to_string(), "0.5");
118 /// assert_eq!(o, Less);
119 ///
120 /// let (one_over_sqrt_pi, o) = Float::one_over_sqrt_pi_prec(10);
121 /// assert_eq!(one_over_sqrt_pi.to_string(), "0.564");
122 /// assert_eq!(o, Greater);
123 ///
124 /// let (one_over_sqrt_pi, o) = Float::one_over_sqrt_pi_prec(100);
125 /// assert_eq!(
126 /// one_over_sqrt_pi.to_string(),
127 /// "0.564189583547756286948079451561"
128 /// );
129 /// assert_eq!(o, Greater);
130 /// ```
131 #[inline]
132 pub fn one_over_sqrt_pi_prec(prec: u64) -> (Self, Ordering) {
133 Self::one_over_sqrt_pi_prec_round(prec, Nearest)
134 }
135}