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