Skip to main content

malachite_float/constants/
one_over_sqrt_tau.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{\tau}=1/\sqrt{2\pi}$, with the given precision and
25    /// rounded using the given [`RoundingMode`]. An [`Ordering`] is also returned, indicating
26    /// whether the rounded value is less than or greater than the exact value of the constant.
27    /// (Since the constant is irrational, the rounded value is never equal to the exact value.)
28    ///
29    /// $$
30    /// x = 1/\sqrt{\tau}+\varepsilon=1/\sqrt{2\pi}+\varepsilon.
31    /// $$
32    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p-1}$.
33    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p-2}$.
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_tau, o) = Float::one_over_sqrt_tau_prec_round(100, Floor);
56    /// assert_eq!(
57    ///     one_over_sqrt_tau.to_string(),
58    ///     "0.3989422804014326779399460599341"
59    /// );
60    /// assert_eq!(o, Less);
61    ///
62    /// let (one_over_sqrt_tau, o) = Float::one_over_sqrt_tau_prec_round(100, Ceiling);
63    /// assert_eq!(
64    ///     one_over_sqrt_tau.to_string(),
65    ///     "0.3989422804014326779399460599344"
66    /// );
67    /// assert_eq!(o, Greater);
68    /// ```
69    pub fn one_over_sqrt_tau_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_tau = Self::tau_prec_round(working_prec, Floor)
74                .0
75                .reciprocal_sqrt();
76            if float_can_round(
77                one_over_sqrt_tau.significand_ref().unwrap(),
78                working_prec - 2,
79                prec,
80                rm,
81            ) {
82                return Self::from_float_prec_round(one_over_sqrt_tau, prec, rm);
83            }
84            working_prec += increment;
85            increment = working_prec >> 1;
86        }
87    }
88
89    /// Returns an approximation of $1/\sqrt{\tau}=1/\sqrt{2\pi}$, with the given precision and
90    /// rounded to the nearest [`Float`] of that precision. An [`Ordering`] is also returned,
91    /// indicating whether the rounded value is less than or greater than the exact value of the
92    /// constant. (Since the constant is irrational, the rounded value is never equal to the exact
93    /// value.)
94    ///
95    /// $$
96    /// x = 1/\sqrt{\tau}+\varepsilon=1/\sqrt{2\pi}+\varepsilon.
97    /// $$
98    /// - $|\varepsilon| < 2^{-p-2}$.
99    ///
100    /// The constant is irrational and transcendental.
101    ///
102    /// The output has precision `prec`.
103    ///
104    /// # Worst-case complexity
105    /// $T(n) = O(n (\log n)^2 \log\log n)$
106    ///
107    /// $M(n) = O(n (\log n)^2)$
108    ///
109    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
110    ///
111    /// # Panics
112    /// Panics if `prec` is zero.
113    ///
114    /// # Examples
115    /// ```
116    /// use malachite_float::Float;
117    /// use std::cmp::Ordering::*;
118    ///
119    /// let (one_over_sqrt_tau, o) = Float::one_over_sqrt_tau_prec(1);
120    /// assert_eq!(one_over_sqrt_tau.to_string(), "0.5");
121    /// assert_eq!(o, Greater);
122    ///
123    /// let (one_over_sqrt_tau, o) = Float::one_over_sqrt_tau_prec(10);
124    /// assert_eq!(one_over_sqrt_tau.to_string(), "0.3989");
125    /// assert_eq!(o, Less);
126    ///
127    /// let (one_over_sqrt_tau, o) = Float::one_over_sqrt_tau_prec(100);
128    /// assert_eq!(
129    ///     one_over_sqrt_tau.to_string(),
130    ///     "0.3989422804014326779399460599344"
131    /// );
132    /// assert_eq!(o, Greater);
133    /// ```
134    #[inline]
135    pub fn one_over_sqrt_tau_prec(prec: u64) -> (Self, Ordering) {
136        Self::one_over_sqrt_tau_prec_round(prec, Nearest)
137    }
138}