malachite_float/constants/pi_over_2.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 $\pi/2$, with the given precision and rounded using the given
21 /// [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded value is
22 /// 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 = \pi/2+\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 (pi_over_2, o) = Float::pi_over_2_prec_round(100, Floor);
52 /// assert_eq!(pi_over_2.to_string(), "1.57079632679489661923132169164");
53 /// assert_eq!(o, Less);
54 ///
55 /// let (pi_over_2, o) = Float::pi_over_2_prec_round(100, Ceiling);
56 /// assert_eq!(pi_over_2.to_string(), "1.570796326794896619231321691641");
57 /// assert_eq!(o, Greater);
58 /// ```
59 #[inline]
60 pub fn pi_over_2_prec_round(prec: u64, rm: RoundingMode) -> (Self, Ordering) {
61 let (pi, o) = Self::pi_prec_round(prec, rm);
62 (pi >> 1u32, o)
63 }
64
65 /// Returns an approximation of $\pi/2$, with the given precision and rounded to the nearest
66 /// [`Float`] of that precision. An [`Ordering`] is also returned, indicating whether the
67 /// rounded value is less than or greater than the exact value of the constant. (Since the
68 /// constant is irrational, the rounded value is never equal to the exact value.)
69 ///
70 /// $$
71 /// x = \pi/2+\varepsilon.
72 /// $$
73 /// - $|\varepsilon| < 2^{-p}$.
74 ///
75 /// The constant is irrational and transcendental.
76 ///
77 /// The output has precision `prec`.
78 ///
79 /// # Worst-case complexity
80 /// $T(n) = O(n (\log n)^2 \log\log n)$
81 ///
82 /// $M(n) = O(n (\log n)^2)$
83 ///
84 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
85 ///
86 /// # Panics
87 /// Panics if `prec` is zero.
88 ///
89 /// # Examples
90 /// ```
91 /// use malachite_float::Float;
92 /// use std::cmp::Ordering::*;
93 ///
94 /// let (pi_over_2, o) = Float::pi_over_2_prec(1);
95 /// assert_eq!(pi_over_2.to_string(), "2.0");
96 /// assert_eq!(o, Greater);
97 ///
98 /// let (pi_over_2, o) = Float::pi_over_2_prec(10);
99 /// assert_eq!(pi_over_2.to_string(), "1.57");
100 /// assert_eq!(o, Less);
101 ///
102 /// let (pi_over_2, o) = Float::pi_over_2_prec(100);
103 /// assert_eq!(pi_over_2.to_string(), "1.57079632679489661923132169164");
104 /// assert_eq!(o, Less);
105 /// ```
106 #[inline]
107 pub fn pi_over_2_prec(prec: u64) -> (Self, Ordering) {
108 Self::pi_over_2_prec_round(prec, Nearest)
109 }
110}