Skip to main content

malachite_float/constants/
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::rounding_modes::RoundingMode::{self, *};
18
19impl Float {
20    /// Returns an approximation of $\tau=2\pi$, with the given precision and rounded using the
21    /// given [`RoundingMode`]. An [`Ordering`] is also returned, indicating whether the rounded
22    /// value is 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 = \tau=2\pi+\varepsilon.
27    /// $$
28    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{-p+3}$.
29    /// - If $m$ is `Nearest`, then $|\varepsilon| < 2^{-p+2}$.
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 (tau, o) = Float::tau_prec_round(100, Floor);
52    /// assert_eq!(tau.to_string(), "6.283185307179586476925286766559");
53    /// assert_eq!(o, Less);
54    ///
55    /// let (tau, o) = Float::tau_prec_round(100, Ceiling);
56    /// assert_eq!(tau.to_string(), "6.283185307179586476925286766565");
57    /// assert_eq!(o, Greater);
58    /// ```
59    #[inline]
60    pub fn tau_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 $\tau=2\pi$, 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 = \tau=2\pi+\varepsilon.
72    /// $$
73    /// - $|\varepsilon| < 2^{-p+2}$.
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 (tau, o) = Float::tau_prec(1);
95    /// assert_eq!(tau.to_string(), "8.0");
96    /// assert_eq!(o, Greater);
97    ///
98    /// let (tau, o) = Float::tau_prec(10);
99    /// assert_eq!(tau.to_string(), "6.28");
100    /// assert_eq!(o, Less);
101    ///
102    /// let (tau, o) = Float::tau_prec(100);
103    /// assert_eq!(tau.to_string(), "6.283185307179586476925286766559");
104    /// assert_eq!(o, Less);
105    /// ```
106    #[inline]
107    pub fn tau_prec(prec: u64) -> (Self, Ordering) {
108        Self::tau_prec_round(prec, Nearest)
109    }
110}