malachite_float/float/constants/one_over_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::Reciprocal;
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/\pi$, with the given precision and rounded using the given
25 /// [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded value is
26 /// 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/\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_pi, o) = Float::one_over_pi_prec_round(100, Floor);
56 /// assert_eq!(
57 /// one_over_pi.to_string(),
58 /// "0.31830988618379067153776752674490"
59 /// );
60 /// assert_eq!(o, Less);
61 ///
62 /// let (one_over_pi, o) = Float::one_over_pi_prec_round(100, Ceiling);
63 /// assert_eq!(
64 /// one_over_pi.to_string(),
65 /// "0.31830988618379067153776752674529"
66 /// );
67 /// assert_eq!(o, Greater);
68 /// ```
69 pub fn one_over_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_pi = Self::pi_prec_round(working_prec, Floor).0.reciprocal();
74 // See algorithms.tex. Since we rounded down when computing pi, the absolute error of
75 // the inverse is bounded by (c_w + 2c_uk_u)ulp(log_e(2)) <= 4ulp(pi).
76 if float_can_round(
77 one_over_pi.significand_ref().unwrap(),
78 working_prec - 2,
79 prec,
80 rm,
81 ) {
82 return Self::from_float_prec_round(one_over_pi, prec, rm);
83 }
84 working_prec += increment;
85 increment = working_prec >> 1;
86 }
87 }
88
89 /// Returns an approximation of $1/\pi$, with the given precision and rounded to the nearest
90 /// [`Float`] of that precision. An [`Ordering`] is also returned, indicating whether the
91 /// rounded value is less than or greater than the exact value of the constant. (Since the
92 /// constant is irrational, the rounded value is never equal to the exact value.)
93 ///
94 /// $$
95 /// x = 1/\pi+\varepsilon.
96 /// $$
97 /// - $|\varepsilon| < 2^{-p-1}$.
98 ///
99 /// The constant is irrational and transcendental.
100 ///
101 /// The output has precision `prec`.
102 ///
103 /// # Worst-case complexity
104 /// $T(n) = O(n (\log n)^2 \log\log n)$
105 ///
106 /// $M(n) = O(n (\log n)^2)$
107 ///
108 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
109 ///
110 /// # Panics
111 /// Panics if `prec` is zero.
112 ///
113 /// # Examples
114 /// ```
115 /// use malachite_float::Float;
116 /// use std::cmp::Ordering::*;
117 ///
118 /// let (one_over_pi, o) = Float::one_over_pi_prec(1);
119 /// assert_eq!(one_over_pi.to_string(), "0.25");
120 /// assert_eq!(o, Less);
121 ///
122 /// let (one_over_pi, o) = Float::one_over_pi_prec(10);
123 /// assert_eq!(one_over_pi.to_string(), "0.31836");
124 /// assert_eq!(o, Greater);
125 ///
126 /// let (one_over_pi, o) = Float::one_over_pi_prec(100);
127 /// assert_eq!(
128 /// one_over_pi.to_string(),
129 /// "0.31830988618379067153776752674490"
130 /// );
131 /// assert_eq!(o, Less);
132 /// ```
133 #[inline]
134 pub fn one_over_pi_prec(prec: u64) -> (Self, Ordering) {
135 Self::one_over_pi_prec_round(prec, Nearest)
136 }
137}