Skip to main content

malachite_float/float/arithmetic/
cot.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5//      Copyright © 2005-2025 Free Software Foundation, Inc.
6//
7//      Contributed by the Pascaline 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
15// Port of MPFR's cotangent. `mpfr_cot` (`cot.c`) instantiates the generic reciprocal template
16// (`gen_inverse.h`) with the tangent, and MPFR's tangent is in turn a quotient of a sine and a
17// cosine; taking cot x as cos x / sin x directly saves the middle rounding and gives the two ends
18// of the exponent range the same treatment, so that is what the Ziv loop below does, with the same
19// working precision and the same two bits of slack. Unlike the secant and the cosecant, the
20// cotangent is not bounded away from zero, so it both overflows, within 2^(-2^30) of a multiple of
21// pi, and underflows, within 2^(-2^30) of an odd multiple of pi/2; each end is decided from an
22// exact bracket. MPFR's shortcut for a tiny input, where cot x is 1/x - x/3 + O(x^3), is kept: the
23// quotient there is exactly representable, so the Ziv loop could never certify it.
24
25use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
26use crate::float::arithmetic::cos::{
27    reduce_huge, signed_constant, sin_bound, trig_near_zero_bracket,
28    trig_rational_near_zero_bracket, trig_turns_near_zero_bracket,
29};
30use crate::float::arithmetic::round_near_x::{
31    round_near_reciprocal, round_rational_reciprocal_leading_term,
32};
33use crate::float::arithmetic::sin_cos::{
34    sin_cos_rational_helper, sin_cos_turns_helper, sin_cos_with_period_prec_round_normal_ref,
35};
36use crate::float::arithmetic::tan::{
37    MAX_CANCEL, MAX_SETTLED_EXPONENT, MIN_SETTLED_EXPONENT, nearest_bracket, round_bracket_signed,
38    round_bracket_signed_by,
39};
40use crate::float::conversion::string::set_str::overflow;
41use crate::{Float, emulate_float_to_float_fn, emulate_rational_to_float_fn};
42use core::cmp::Ordering::{self, Equal};
43use core::cmp::max;
44use malachite_base::num::arithmetic::traits::{
45    Abs, AddMul, CeilingLogBase2, Cot, CotAssign, IsPowerOf2, Mod, Parity, Pow, Reciprocal, Square,
46};
47use malachite_base::num::basic::floats::PrimitiveFloat;
48use malachite_base::num::basic::integers::PrimitiveInt;
49use malachite_base::num::basic::traits::{
50    Infinity as InfinityTrait, NaN as NaNTrait, NegativeInfinity,
51    NegativeZero as NegativeZeroTrait, One, Zero as ZeroTrait,
52};
53use malachite_base::num::comparison::traits::PartialOrdAbs;
54use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
55use malachite_base::num::logic::traits::SignificantBits;
56use malachite_base::rounding_modes::RoundingMode::{self, *};
57use malachite_nz::integer::Integer;
58use malachite_nz::natural::arithmetic::float::round::float_can_round;
59use malachite_nz::platform::Limb;
60use malachite_q::Rational;
61
62// Decides cot(x) = c/s from the sine and cosine rounded to nearest at precision m, by a `Rational`
63// bracket, for the cases the `Float` quotient cannot settle: it overflowed, underflowed, or lies
64// within two bits of either end of the exponent range, or the sine or cosine underflowed. Returns
65// `None` if the bracket does not decide the rounding, so that the working precision must grow.
66fn cot_bracket(
67    x: &Float,
68    s: &Float,
69    c: &Float,
70    m: u64,
71    prec: u64,
72    rm: RoundingMode,
73) -> Option<(Float, Ordering)> {
74    let negative = s.is_sign_negative() != c.is_sign_negative();
75    // A sine that underflowed is at most 1.5 times the smallest positive `Float`, and the cosine is
76    // then within 2^-m of 1, so the cotangent is at least 2^(2^30)/1.5 in magnitude, beyond the
77    // largest finite `Float`.
78    if *s == 0u32
79        || (s.get_exponent() == Some(Float::MIN_EXPONENT)
80            && s.significand_ref().unwrap().is_power_of_2())
81    {
82        return Some(overflow(!negative, prec, rm));
83    }
84    let (s_lo, s_hi) = nearest_bracket(s, m);
85    let (c_lo, c_hi) = if *c == 0u32 {
86        // The cosine underflowed, so its rounding says only that it is below half the smallest
87        // positive `Float`, and the cotangent, barely larger than the cosine, cannot be placed
88        // against that same bound: take the cosine's exact bracket from the distance to the nearest
89        // odd multiple of pi/2, as the near-zero path does.
90        let (lo, hi) = trig_near_zero_bracket(x, m + 64, MAX_CANCEL, true);
91        if lo < 0u32 { (-hi, -lo) } else { (lo, hi) }
92    } else {
93        nearest_bracket(c, m)
94    };
95    round_bracket_signed_by(negative, c_lo / s_hi, c_hi / s_lo, prec, rm)
96}
97
98// This is mpfr_cot from cot.c, MPFR 4.2.2, with the tangent's own quotient of a sine and a cosine
99// folded in and the bracket path for results near the ends of the exponent range.
100fn cot_prec_round_normal_ref(x: &Float, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
101    assert_ne!(rm, Exact, "Inexact cot");
102    let exp_x = i64::from(x.get_exponent().unwrap());
103    // ACTION_TINY from cot.c: EXP(x) + 1 <= -2 max(PREC(x), PREC(y)). There cot x = 1/x - x/3 -
104    // ..., and |cot x - 1/x| <= 0.36 for |x| <= 1, with the correction opposing the sign of 1/x, so
105    // that the cotangent lies just short of 1/x.
106    let n = i64::exact_from(max(x.get_prec().unwrap(), prec));
107    if exp_x < -(n << 1) {
108        return round_near_reciprocal(x, false, prec, rm);
109    }
110    // Compute initial precision
111    let mut m = prec + prec.ceiling_log_base_2() + 13;
112    let mut increment = Limb::WIDTH;
113    loop {
114        // err <= 1/2 ulp on s and c, each correctly rounded even within 2^(-2^30) of a zero of its
115        // function, where it may underflow
116        let (s, c, _, _) = x.sin_cos_prec_ref(m);
117        // err <= 4 ulps
118        let q = if s == 0u32 || c == 0u32 {
119            None
120        } else {
121            Some(c.div_prec_ref_ref(&s, m).0)
122        };
123        // A quotient that overflowed, underflowed, or lies within two bits of either end of the
124        // exponent range, where rounding it to `prec` could still cross the end, is decided from
125        // brackets, as is a sine or cosine that underflowed.
126        match q.as_ref().and_then(Float::get_exponent).map(i64::from) {
127            Some(e) if e > MIN_SETTLED_EXPONENT && e < MAX_SETTLED_EXPONENT => {
128                let q = q.unwrap();
129                if float_can_round(q.significand_ref().unwrap(), m - 2, prec, rm) {
130                    return Float::from_float_prec_round(q, prec, rm);
131                }
132            }
133            _ => {
134                if let Some(result) = cot_bracket(x, &s, &c, m, prec, rm) {
135                    return result;
136                }
137            }
138        }
139        m += increment;
140        increment = m >> 1;
141    }
142}
143
144// cot x for a tiny nonzero `Rational` x whose two-term bracket straddles a rounding boundary: the
145// sine is bracketed by `sin_bound` at a growing working precision, and the cosine by consecutive
146// partial sums of its alternating series, until the quotient's bracket rounds unambiguously (the
147// cotangent is transcendental, so it eventually does). This is `tan_rational_tiny` with the
148// quotient the other way up.
149fn cot_rational_tiny(
150    x: &Rational,
151    ax: &Rational,
152    prec: u64,
153    rm: RoundingMode,
154) -> (Float, Ordering) {
155    let x2 = ax.square();
156    let mut w = prec + 64;
157    let mut terms = 2u64;
158    loop {
159        let s_lo = sin_bound(ax, w, false);
160        let s_hi = sin_bound(ax, w, true);
161        // cos x = 1 - x^2/2 + x^4/24 - ..., an alternating series with decreasing terms for |x| <=
162        // 1, so the partial sums with an even and an odd number of terms bracket it
163        let mut c_lo = Rational::ONE;
164        let mut term = Rational::ONE;
165        let mut c_hi = Rational::ONE;
166        for k in 1..=terms {
167            term *= &x2;
168            term /= Rational::from((k << 1) * ((k << 1) - 1));
169            if k.odd() {
170                c_lo = &c_hi - &term;
171            } else {
172                c_hi = &c_lo + &term;
173            }
174        }
175        let lo = c_lo / s_hi;
176        let hi = c_hi / s_lo;
177        if let Some(result) = round_bracket_signed(x, lo, hi, prec, rm) {
178            return result;
179        }
180        w <<= 1;
181        terms += 1;
182    }
183}
184
185// `cot_bracket` for a `Rational` input: the cosine's exact bracket, when it underflowed, comes from
186// the `Rational` near-zero machinery, on the input reduced modulo 2 pi if it is too large to be a
187// `Float`.
188fn cot_rational_bracket(
189    x: &Rational,
190    exp_x: i64,
191    s: &Float,
192    c: &Float,
193    m: u64,
194    prec: u64,
195    rm: RoundingMode,
196) -> Option<(Float, Ordering)> {
197    let negative = s.is_sign_negative() != c.is_sign_negative();
198    if *s == 0u32
199        || (s.get_exponent() == Some(Float::MIN_EXPONENT)
200            && s.significand_ref().unwrap().is_power_of_2())
201    {
202        return Some(overflow(!negative, prec, rm));
203    }
204    let (s_lo, s_hi) = nearest_bracket(s, m);
205    let (c_lo, c_hi) = if *c == 0u32 {
206        let w = m + 64;
207        let reduced;
208        let (y, extra) = if exp_x >= Float::MAX_EXPONENT_I64 {
209            reduced = reduce_huge(x, exp_x, w);
210            (&reduced, Some(2 - i64::exact_from(w)))
211        } else {
212            (x, None)
213        };
214        let exp_y = y.floor_log_base_2_abs() + 1;
215        let (lo, hi) = trig_rational_near_zero_bracket(y, exp_y, extra, w, m, true);
216        if lo < 0u32 { (-hi, -lo) } else { (lo, hi) }
217    } else {
218        nearest_bracket(c, m)
219    };
220    round_bracket_signed_by(negative, c_lo / s_hi, c_hi / s_lo, prec, rm)
221}
222
223// Computes cot(x) for a nonzero `Rational` x, rounded to precision `prec` with rounding mode `rm`.
224// (x = 0 is handled by the caller.) The result is never exactly representable, so `rm` must not be
225// `Exact`.
226//
227// This is the `Float` algorithm with the sine and cosine taken from `sin_cos_rational_helper`,
228// which rounds the input once and shares the argument reduction, and with a direct bracket for a
229// tiny input, inverting the tangent's own series bracket: that also covers inputs below the `Float`
230// exponent range, which no other path could even round, and the powers of 2, whose reciprocals are
231// exactly representable and which the general loop could therefore never certify.
232pub(crate) fn cot_rational_helper(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
233    assert_ne!(rm, Exact, "Inexact cot");
234    let exp_x = x.floor_log_base_2_abs() + 1; // the MPFR-style exponent of x
235    // cot(x) = 1/x - x/3 - ..., so 1/|x| exceeds |cot x| by less than |x|/2: for a tiny x the
236    // reciprocal's own rounding, nudged toward zero, is the answer.
237    if let Some(result) = round_rational_reciprocal_leading_term(x, exp_x, false, prec, rm) {
238        return result;
239    }
240    // For |x| <= 1/2, |x| + |x|^3/3 <= |tan x| <= |x| + |x|^3/3 + |x|^5, so the cotangent lies
241    // between the reciprocals of those, a bracket of relative width below x^4, which decides the
242    // rounding once x^4 is below 2^-(prec + 3), unless the cotangent lies within that of a rounding
243    // boundary.
244    if exp_x < 0 && -(exp_x << 2) > i64::exact_from(prec) + 3 {
245        let ax = x.abs();
246        let ax3 = (&ax).pow(3u64);
247        let t_lo = &ax + &ax3 / const { Rational::const_from_unsigned(3) };
248        let t_hi = (&t_lo).add_mul(&ax3, &(&ax).square());
249        if let Some(result) =
250            round_bracket_signed(x, t_hi.reciprocal(), t_lo.reciprocal(), prec, rm)
251        {
252            return result;
253        }
254        // The bracket straddles a rounding boundary; tighten it from the series of the sine and
255        // cosine, which narrows without bound.
256        return cot_rational_tiny(x, &ax, prec, rm);
257    }
258    let mut m = prec + prec.ceiling_log_base_2() + 13;
259    let mut increment = Limb::WIDTH;
260    loop {
261        // the sine and cosine correctly rounded at m, even within 2^(-2^30) of a zero of either,
262        // where they may underflow
263        let (s, c, _, _) = sin_cos_rational_helper(x, m, Nearest);
264        // err <= 4 ulps
265        let q = if s == 0u32 || c == 0u32 {
266            None
267        } else {
268            Some(c.div_prec_ref_ref(&s, m).0)
269        };
270        match q.as_ref().and_then(Float::get_exponent).map(i64::from) {
271            Some(e) if e > MIN_SETTLED_EXPONENT && e < MAX_SETTLED_EXPONENT => {
272                let q = q.unwrap();
273                if float_can_round(q.significand_ref().unwrap(), m - 2, prec, rm) {
274                    return Float::from_float_prec_round(q, prec, rm);
275                }
276            }
277            _ => {
278                if let Some(result) = cot_rational_bracket(x, exp_x, &s, &c, m, prec, rm) {
279                    return result;
280                }
281            }
282        }
283        m += increment;
284        increment = m >> 1;
285    }
286}
287
288// The closed-form cases of cot(2 pi x / u), keyed by the denominator d of x/u in lowest terms (with
289// 0 < |x/u| < 1, so d > 1 and the numerator n is the angle in units of 1/d of a turn). These are
290// the tangent's, reciprocated: an odd multiple of 1/4 of a turn, where the tangent has a pole, is a
291// zero of the cotangent, and a multiple of 1/2 is the other way about; an odd multiple of 1/8 gives
292// 1 or -1, as it does for the tangent; and d = 3 or 6 gives sqrt(3)/3 while d = 12 gives sqrt(3),
293// up to sign. Those two constants are never exact, so they return `None` for `Exact`.
294fn cot_turns_special_case(q: &Rational, prec: u64, rm: RoundingMode) -> Option<(Float, Ordering)> {
295    let d = q.denominator_ref();
296    if *d > 12u32 {
297        return None;
298    }
299    let d = u64::exact_from(d);
300    let negative = *q < 0u32;
301    // the angle in units of 1/d of a turn (the numerator of a `Rational` is unsigned, so the sign
302    // is restored before reducing modulo d)
303    let n = u64::exact_from(
304        &Integer::from_sign_and_abs_ref(!negative, q.numerator_ref()).mod_op(Integer::from(d)),
305    );
306    match d {
307        // eighths of a turn; n cannot be 0, since 0 < |q| < 1
308        2 | 4 | 8 => Some(match n * (8 / d) {
309            // the pole at 180°, where the sine is a zero with the sign of q and the cosine is -1
310            4 => (
311                if negative {
312                    Float::INFINITY
313                } else {
314                    Float::NEGATIVE_INFINITY
315                },
316                Equal,
317            ),
318            // cot(90°) = +0, cot(270°) = -0
319            2 => (Float::ZERO, Equal),
320            6 => (Float::NEGATIVE_ZERO, Equal),
321            // cot(45°) = cot(225°) = 1, cot(135°) = cot(315°) = -1
322            1 | 5 => (Float::one_prec(prec), Equal),
323            _ => (-Float::one_prec(prec), Equal),
324        }),
325        _ if rm == Exact => None,
326        // twelfths of a turn
327        3 | 6 | 12 => Some(match n * (12 / d) {
328            // cot(30°) = cot(210°) = sqrt(3), cot(150°) = cot(330°) = -sqrt(3)
329            1 | 7 => signed_constant(Float::sqrt_3_prec_round, false, prec, rm),
330            5 | 11 => signed_constant(Float::sqrt_3_prec_round, true, prec, rm),
331            // cot(60°) = cot(240°) = sqrt(3)/3, cot(120°) = cot(300°) = -sqrt(3)/3
332            2 | 8 => signed_constant(Float::sqrt_3_over_3_prec_round, false, prec, rm),
333            _ => signed_constant(Float::sqrt_3_over_3_prec_round, true, prec, rm),
334        }),
335        _ => None,
336    }
337}
338
339// `cot_bracket` for an argument in u ths of a turn: the cosine's exact bracket, when it
340// underflowed, comes from the distance of the fraction of a turn to the nearest odd multiple of
341// 1/4, as the near-zero path uses it. `q` produces that fraction, which the `Float` caller forms
342// only here.
343fn cot_turns_bracket<F: Fn() -> Rational>(
344    q: F,
345    s: &Float,
346    c: &Float,
347    m: u64,
348    prec: u64,
349    rm: RoundingMode,
350) -> Option<(Float, Ordering)> {
351    let negative = s.is_sign_negative() != c.is_sign_negative();
352    if *s == 0u32
353        || (s.get_exponent() == Some(Float::MIN_EXPONENT)
354            && s.significand_ref().unwrap().is_power_of_2())
355    {
356        return Some(overflow(!negative, prec, rm));
357    }
358    let (s_lo, s_hi) = nearest_bracket(s, m);
359    let (c_lo, c_hi) = if *c == 0u32 {
360        let (lo, hi) = trig_turns_near_zero_bracket(&q(), m, true)?;
361        if lo < 0u32 { (-hi, -lo) } else { (lo, hi) }
362    } else {
363        nearest_bracket(c, m)
364    };
365    round_bracket_signed_by(negative, c_lo / s_hi, c_hi / s_lo, prec, rm)
366}
367
368// Computes cot(2 pi q) for a nonzero `Rational` fraction of a turn q in (-1, 1), rounded to
369// precision `prec` with rounding mode `rm`. `rm` may be `Exact` only in the exact cases (see
370// `cot_turns_special_case`). This is the `Float` algorithm with the fraction of a turn taken
371// directly: since q is exact, only pi and the sine and cosine are rounded, and no argument
372// reduction is needed beyond the exact one the caller has already done. The tangent's shortcut for
373// a tiny q is not needed here either: `sin_cos_turns_helper` rounds the sine through its own
374// near-zero path, and a sine that underflows there, with the cosine at 1, is the overflow the
375// bracket reads it as.
376fn cot_turns_helper(q: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
377    let exp_q = q.floor_log_base_2_abs() + 1;
378    // The special cases need |q| >= 1/12
379    if exp_q >= -4
380        && let Some(result) = cot_turns_special_case(q, prec, rm)
381    {
382        return result;
383    }
384    // Only the exact cases can be rounded exactly
385    assert_ne!(rm, Exact, "Inexact cot_with_period");
386    let mut m = prec + prec.ceiling_log_base_2() + 13;
387    let mut increment = Limb::WIDTH;
388    loop {
389        // err <= 1/2 ulp on s and c, each correctly rounded even within 2^(-2^30) of a zero of its
390        // function, where it may underflow
391        let (s, c, _, _) = sin_cos_turns_helper(q, m, Nearest);
392        // err <= 4 ulps
393        let t = if s == 0u32 || c == 0u32 {
394            None
395        } else {
396            Some(c.div_prec_ref_ref(&s, m).0)
397        };
398        // as in the `Float` version, a quotient at either end of the exponent range, or a sine or
399        // cosine that underflowed, is decided from brackets
400        match t.as_ref().and_then(Float::get_exponent).map(i64::from) {
401            Some(e) if e > MIN_SETTLED_EXPONENT && e < MAX_SETTLED_EXPONENT => {
402                let t = t.unwrap();
403                if float_can_round(t.significand_ref().unwrap(), m - 2, prec, rm) {
404                    return Float::from_float_prec_round(t, prec, rm);
405                }
406            }
407            _ => {
408                if let Some(result) = cot_turns_bracket(|| q.clone(), &s, &c, m, prec, rm) {
409                    return result;
410                }
411            }
412        }
413        m += increment;
414        increment = m >> 1;
415    }
416}
417
418// Computes cot(2 pi x/u) for a finite nonzero `Float` x and a nonzero u, rounded to precision
419// `prec` with rounding mode `rm`. `rm` may be `Exact` only in the exact cases (see
420// `cot_turns_special_case`).
421//
422// This has no MPFR counterpart; it is `cot` with the sine and cosine taken in u ths of a turn,
423// which reduces the argument exactly rather than modulo an approximation of 2 pi, and so reaches
424// the exact and closed-form cases that the radian version cannot see. The tangent's shortcut for a
425// tiny x/u is not needed: there the sine underflows while the cosine is 1, and the bracket reads
426// that as the overflow it is.
427fn cot_with_period_prec_round_normal_ref(
428    x: &Float,
429    u: u64,
430    prec: u64,
431    rm: RoundingMode,
432) -> (Float, Ordering) {
433    // Range reduction, as in `tan_with_period`: the argument is already reduced if |x| < u.
434    let xr;
435    let xp = if x.lt_abs(&u) {
436        x
437    } else {
438        // xr = x mod u, with the sign of x, exactly
439        let p = i64::exact_from(x.get_prec().unwrap()) - i64::from(x.get_exponent().unwrap());
440        let (r, o) =
441            x.rem_unsigned_prec_round_ref(u, u64::WIDTH + u64::exact_from(max(p, 0)), Exact);
442        assert_eq!(o, Equal);
443        if r == 0u32 {
444            // x is a multiple of u, so the sine is a zero with the sign of x, the cosine is 1, and
445            // the cotangent is an infinity with that sign
446            return (
447                if *x < 0u32 {
448                    Float::NEGATIVE_INFINITY
449                } else {
450                    Float::INFINITY
451                },
452                Equal,
453            );
454        }
455        xr = r;
456        &xr
457    };
458    // now |xp/u| < 1
459    let exp_x = i64::from(xp.get_exponent().unwrap());
460    // The special cases need |x/u| >= 1/12, so the exponent test skips the `Rational` construction
461    // for the small x that would make it expensive (a tiny x has a huge power-of-2 denominator).
462    if exp_x >= i64::exact_from(u.significant_bits()) - 4
463        && let Some(result) =
464            cot_turns_special_case(&(Rational::exact_from(xp) / Rational::from(u)), prec, rm)
465    {
466        return result;
467    }
468    // Only the exact cases can be rounded exactly
469    assert_ne!(rm, Exact, "Inexact cot_with_period");
470    let mut m = prec + prec.ceiling_log_base_2() + 13;
471    let mut increment = Limb::WIDTH;
472    loop {
473        // err <= 1/2 ulp on s and c, each correctly rounded even within 2^(-2^30) of a zero of its
474        // function, where it may underflow
475        let (s, c, _, _) = sin_cos_with_period_prec_round_normal_ref(xp, u, m, Nearest);
476        // err <= 4 ulps
477        let q = if s == 0u32 || c == 0u32 {
478            None
479        } else {
480            Some(c.div_prec_ref_ref(&s, m).0)
481        };
482        // A quotient that overflowed, underflowed, or lies within two bits of either end of the
483        // exponent range, where rounding it to `prec` could still cross the end, is decided from
484        // brackets, as is a sine or cosine that underflowed.
485        match q.as_ref().and_then(Float::get_exponent).map(i64::from) {
486            Some(e) if e > MIN_SETTLED_EXPONENT && e < MAX_SETTLED_EXPONENT => {
487                let q = q.unwrap();
488                if float_can_round(q.significand_ref().unwrap(), m - 2, prec, rm) {
489                    return Float::from_float_prec_round(q, prec, rm);
490                }
491            }
492            _ => {
493                if let Some(result) = cot_turns_bracket(
494                    || Rational::exact_from(xp) / Rational::from(u),
495                    &s,
496                    &c,
497                    m,
498                    prec,
499                    rm,
500                ) {
501                    return result;
502                }
503            }
504        }
505        m += increment;
506        increment = m >> 1;
507    }
508}
509
510impl Float {
511    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result to the specified
512    /// precision and with the specified rounding mode. The [`Float`] is taken by value. An
513    /// [`Ordering`] is also returned, indicating whether the rounded cotangent is less than, equal
514    /// to, or greater than the exact cotangent. Although `NaN`s are not comparable to any
515    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
516    ///
517    /// See [`RoundingMode`] for a description of the possible rounding modes.
518    ///
519    /// $$
520    /// f(x,p,m) = \cot x+\varepsilon.
521    /// $$
522    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
523    /// - If $x$ is finite and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot
524    ///   x|\rfloor-p+1}$.
525    /// - If $x$ is finite and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot
526    ///   x|\rfloor-p}$.
527    ///
528    /// If the output has a precision, it is `prec`.
529    ///
530    /// Special cases:
531    /// - $f(\text{NaN},p,m)=\text{NaN}$
532    /// - $f(\pm\infty,p,m)=\text{NaN}$
533    /// - $f(\pm0.0,p,m)=\pm\infty$
534    ///
535    /// Overflow:
536    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
537    ///   returned instead.
538    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
539    ///   returned instead.
540    /// - If $f(x,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
541    ///   returned instead.
542    /// - If $f(x,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
543    ///   is returned instead.
544    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
545    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
546    ///
547    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
548    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
549    /// more than $2^{30}$ bits of precision; overflow also occurs for an input of magnitude about
550    /// $2^{-2^{30}}$, whose reciprocal alone is beyond the largest finite [`Float`].
551    ///
552    /// If you know you'll be using `Nearest`, consider using [`Float::cot_prec`] instead. If you
553    /// know that your target precision is the precision of the input, consider using
554    /// [`Float::cot_round`] instead. If both of these things are true, consider using
555    /// [`Float::cot`] instead.
556    ///
557    /// # Worst-case complexity
558    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
559    ///
560    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
561    ///
562    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is
563    /// `self.significant_bits()`, and $e$ is the exponent of `self` (0 if `self` has no exponent or
564    /// a negative one): the sine and cosine at working precision $n$ (for large $n$ by binary
565    /// splitting of the Taylor series, otherwise the cosine, from which the sine is derived), and
566    /// their quotient, cost the first term, and for $|x| \geq 4$ the argument is reduced modulo
567    /// $2\pi$, which requires $\pi$ to about $n + e$ bits and a remainder of the $m$-bit input.
568    /// Unlike most functions, `cot` therefore gets slower as the magnitude of its input grows, not
569    /// just as the precision does.
570    ///
571    /// # Panics
572    /// Panics if `rm` is `Exact`, since the cotangent of a finite nonzero [`Float`] is never
573    /// exactly representable, or if `prec` is zero.
574    ///
575    /// # Examples
576    /// ```
577    /// use malachite_base::rounding_modes::RoundingMode::*;
578    /// use malachite_float::Float;
579    /// use std::cmp::Ordering::*;
580    ///
581    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
582    ///     .0
583    ///     .cot_prec_round(5, Floor);
584    /// assert_eq!(c.to_string(), "0.625");
585    /// assert_eq!(o, Less);
586    ///
587    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
588    ///     .0
589    ///     .cot_prec_round(5, Ceiling);
590    /// assert_eq!(c.to_string(), "0.656");
591    /// assert_eq!(o, Greater);
592    ///
593    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
594    ///     .0
595    ///     .cot_prec_round(5, Nearest);
596    /// assert_eq!(c.to_string(), "0.656");
597    /// assert_eq!(o, Greater);
598    ///
599    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
600    ///     .0
601    ///     .cot_prec_round(20, Floor);
602    /// assert_eq!(c.to_string(), "0.64209175");
603    /// assert_eq!(o, Less);
604    ///
605    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
606    ///     .0
607    ///     .cot_prec_round(20, Ceiling);
608    /// assert_eq!(c.to_string(), "0.64209270");
609    /// assert_eq!(o, Greater);
610    ///
611    /// let (c, o) = Float::from_unsigned_prec(1u32, 100)
612    ///     .0
613    ///     .cot_prec_round(20, Nearest);
614    /// assert_eq!(c.to_string(), "0.64209270");
615    /// assert_eq!(o, Greater);
616    /// ```
617    #[inline]
618    pub fn cot_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
619        self.cot_prec_round_ref(prec, rm)
620    }
621
622    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result to the specified
623    /// precision and with the specified rounding mode. The [`Float`] is taken by reference. An
624    /// [`Ordering`] is also returned, indicating whether the rounded cotangent is less than, equal
625    /// to, or greater than the exact cotangent. Although `NaN`s are not comparable to any
626    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
627    ///
628    /// See [`RoundingMode`] for a description of the possible rounding modes.
629    ///
630    /// $$
631    /// f(x,p,m) = \cot x+\varepsilon.
632    /// $$
633    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
634    /// - If $x$ is finite and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot
635    ///   x|\rfloor-p+1}$.
636    /// - If $x$ is finite and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot
637    ///   x|\rfloor-p}$.
638    ///
639    /// If the output has a precision, it is `prec`.
640    ///
641    /// Special cases:
642    /// - $f(\text{NaN},p,m)=\text{NaN}$
643    /// - $f(\pm\infty,p,m)=\text{NaN}$
644    /// - $f(\pm0.0,p,m)=\pm\infty$
645    ///
646    /// Overflow:
647    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
648    ///   returned instead.
649    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
650    ///   returned instead.
651    /// - If $f(x,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
652    ///   returned instead.
653    /// - If $f(x,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
654    ///   is returned instead.
655    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
656    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
657    ///
658    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
659    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
660    /// more than $2^{30}$ bits of precision; overflow also occurs for an input of magnitude about
661    /// $2^{-2^{30}}$, whose reciprocal alone is beyond the largest finite [`Float`].
662    ///
663    /// If you know you'll be using `Nearest`, consider using [`Float::cot_prec_ref`] instead. If
664    /// you know that your target precision is the precision of the input, consider using
665    /// [`Float::cot_round_ref`] instead. If both of these things are true, consider using
666    /// `(&Float).cot()` instead.
667    ///
668    /// # Worst-case complexity
669    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
670    ///
671    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
672    ///
673    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is
674    /// `self.significant_bits()`, and $e$ is the exponent of `self` (0 if `self` has no exponent or
675    /// a negative one): the sine and cosine at working precision $n$ (for large $n$ by binary
676    /// splitting of the Taylor series, otherwise the cosine, from which the sine is derived), and
677    /// their quotient, cost the first term, and for $|x| \geq 4$ the argument is reduced modulo
678    /// $2\pi$, which requires $\pi$ to about $n + e$ bits and a remainder of the $m$-bit input.
679    /// Unlike most functions, `cot` therefore gets slower as the magnitude of its input grows, not
680    /// just as the precision does.
681    ///
682    /// # Panics
683    /// Panics if `rm` is `Exact`, since the cotangent of a finite nonzero [`Float`] is never
684    /// exactly representable, or if `prec` is zero.
685    ///
686    /// # Examples
687    /// ```
688    /// use malachite_base::rounding_modes::RoundingMode::*;
689    /// use malachite_float::Float;
690    /// use std::cmp::Ordering::*;
691    ///
692    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_round_ref(5, Floor);
693    /// assert_eq!(c.to_string(), "0.625");
694    /// assert_eq!(o, Less);
695    ///
696    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_round_ref(5, Ceiling);
697    /// assert_eq!(c.to_string(), "0.656");
698    /// assert_eq!(o, Greater);
699    ///
700    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_round_ref(5, Nearest);
701    /// assert_eq!(c.to_string(), "0.656");
702    /// assert_eq!(o, Greater);
703    ///
704    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_round_ref(20, Floor);
705    /// assert_eq!(c.to_string(), "0.64209175");
706    /// assert_eq!(o, Less);
707    ///
708    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_round_ref(20, Ceiling);
709    /// assert_eq!(c.to_string(), "0.64209270");
710    /// assert_eq!(o, Greater);
711    ///
712    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_round_ref(20, Nearest);
713    /// assert_eq!(c.to_string(), "0.64209270");
714    /// assert_eq!(o, Greater);
715    /// ```
716    pub fn cot_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
717        assert_ne!(prec, 0);
718        match &self.0 {
719            NaN | Infinity { .. } => (Self::NAN, Equal),
720            // cot(+0) = +infinity, cot(-0) = -infinity
721            Zero { .. } => (
722                if self.is_sign_negative() {
723                    Self::NEGATIVE_INFINITY
724                } else {
725                    Self::INFINITY
726                },
727                Equal,
728            ),
729            Finite { .. } => cot_prec_round_normal_ref(self, prec, rm),
730        }
731    }
732
733    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result to the nearest value of
734    /// the specified precision. The [`Float`] is taken by value. An [`Ordering`] is also returned,
735    /// indicating whether the rounded cotangent is less than, equal to, or greater than the exact
736    /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function
737    /// returns a `NaN` it also returns `Equal`.
738    ///
739    /// If the cotangent is equidistant from two [`Float`]s with the specified precision, the
740    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
741    /// description of the `Nearest` rounding mode.
742    ///
743    /// $$
744    /// f(x,p) = \cot x+\varepsilon.
745    /// $$
746    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
747    /// - If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$.
748    ///
749    /// If the output has a precision, it is `prec`.
750    ///
751    /// Special cases:
752    /// - $f(\text{NaN},p)=\text{NaN}$
753    /// - $f(\pm\infty,p)=\text{NaN}$
754    /// - $f(\pm0.0,p)=\pm\infty$
755    ///
756    /// Overflow:
757    /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
758    /// - If $f(x,p)\leq -2^{2^{30}-1}$, $-\infty$ is returned instead.
759    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
760    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
761    ///
762    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
763    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
764    /// more than $2^{30}$ bits of precision; overflow also occurs for an input of magnitude about
765    /// $2^{-2^{30}}$, whose reciprocal alone is beyond the largest finite [`Float`].
766    ///
767    /// If you want to use a rounding mode other than `Nearest`, consider using
768    /// [`Float::cot_prec_round`] instead. If you know that your target precision is the precision
769    /// of the input, consider using [`Float::cot`] instead.
770    ///
771    /// # Worst-case complexity
772    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
773    ///
774    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
775    ///
776    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is
777    /// `self.significant_bits()`, and $e$ is the exponent of `self` (0 if `self` has no exponent or
778    /// a negative one): the sine and cosine at working precision $n$ (for large $n$ by binary
779    /// splitting of the Taylor series, otherwise the cosine, from which the sine is derived), and
780    /// their quotient, cost the first term, and for $|x| \geq 4$ the argument is reduced modulo
781    /// $2\pi$, which requires $\pi$ to about $n + e$ bits and a remainder of the $m$-bit input.
782    /// Unlike most functions, `cot` therefore gets slower as the magnitude of its input grows, not
783    /// just as the precision does.
784    ///
785    /// # Panics
786    /// Panics if `prec` is zero.
787    ///
788    /// # Examples
789    /// ```
790    /// use malachite_float::Float;
791    /// use std::cmp::Ordering::*;
792    ///
793    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.cot_prec(5);
794    /// assert_eq!(c.to_string(), "0.656");
795    /// assert_eq!(o, Greater);
796    ///
797    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.cot_prec(20);
798    /// assert_eq!(c.to_string(), "0.64209270");
799    /// assert_eq!(o, Greater);
800    /// ```
801    #[inline]
802    pub fn cot_prec(self, prec: u64) -> (Self, Ordering) {
803        self.cot_prec_round(prec, Nearest)
804    }
805
806    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result to the nearest value of
807    /// the specified precision. The [`Float`] is taken by reference. An [`Ordering`] is also
808    /// returned, indicating whether the rounded cotangent is less than, equal to, or greater than
809    /// the exact cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this
810    /// function returns a `NaN` it also returns `Equal`.
811    ///
812    /// If the cotangent is equidistant from two [`Float`]s with the specified precision, the
813    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
814    /// description of the `Nearest` rounding mode.
815    ///
816    /// $$
817    /// f(x,p) = \cot x+\varepsilon.
818    /// $$
819    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
820    /// - If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$.
821    ///
822    /// If the output has a precision, it is `prec`.
823    ///
824    /// Special cases:
825    /// - $f(\text{NaN},p)=\text{NaN}$
826    /// - $f(\pm\infty,p)=\text{NaN}$
827    /// - $f(\pm0.0,p)=\pm\infty$
828    ///
829    /// Overflow:
830    /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
831    /// - If $f(x,p)\leq -2^{2^{30}-1}$, $-\infty$ is returned instead.
832    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
833    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
834    ///
835    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
836    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
837    /// more than $2^{30}$ bits of precision; overflow also occurs for an input of magnitude about
838    /// $2^{-2^{30}}$, whose reciprocal alone is beyond the largest finite [`Float`].
839    ///
840    /// If you want to use a rounding mode other than `Nearest`, consider using
841    /// [`Float::cot_prec_round_ref`] instead. If you know that your target precision is the
842    /// precision of the input, consider using `(&Float).cot()` instead.
843    ///
844    /// # Worst-case complexity
845    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
846    ///
847    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
848    ///
849    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is
850    /// `self.significant_bits()`, and $e$ is the exponent of `self` (0 if `self` has no exponent or
851    /// a negative one): the sine and cosine at working precision $n$ (for large $n$ by binary
852    /// splitting of the Taylor series, otherwise the cosine, from which the sine is derived), and
853    /// their quotient, cost the first term, and for $|x| \geq 4$ the argument is reduced modulo
854    /// $2\pi$, which requires $\pi$ to about $n + e$ bits and a remainder of the $m$-bit input.
855    /// Unlike most functions, `cot` therefore gets slower as the magnitude of its input grows, not
856    /// just as the precision does.
857    ///
858    /// # Panics
859    /// Panics if `prec` is zero.
860    ///
861    /// # Examples
862    /// ```
863    /// use malachite_float::Float;
864    /// use std::cmp::Ordering::*;
865    ///
866    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_ref(5);
867    /// assert_eq!(c.to_string(), "0.656");
868    /// assert_eq!(o, Greater);
869    ///
870    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_prec_ref(20);
871    /// assert_eq!(c.to_string(), "0.64209270");
872    /// assert_eq!(o, Greater);
873    /// ```
874    #[inline]
875    pub fn cot_prec_ref(&self, prec: u64) -> (Self, Ordering) {
876        self.cot_prec_round_ref(prec, Nearest)
877    }
878
879    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result with the specified
880    /// rounding mode. The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating
881    /// whether the rounded cotangent is less than, equal to, or greater than the exact cotangent.
882    /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
883    /// it also returns `Equal`.
884    ///
885    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
886    /// description of the possible rounding modes.
887    ///
888    /// $$
889    /// f(x,m) = \cot x+\varepsilon.
890    /// $$
891    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
892    /// - If $x$ is finite and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot
893    ///   x|\rfloor-p+1}$, where $p$ is the precision of the input.
894    /// - If $x$ is finite and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot
895    ///   x|\rfloor-p}$, where $p$ is the precision of the input.
896    ///
897    /// If the output has a precision, it is the precision of the input.
898    ///
899    /// Special cases:
900    /// - $f(\text{NaN},m)=\text{NaN}$
901    /// - $f(\pm\infty,m)=\text{NaN}$
902    /// - $f(\pm0.0,m)=\pm\infty$
903    ///
904    /// Overflow:
905    /// - If $f(x,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
906    ///   returned instead.
907    /// - If $f(x,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
908    ///   returned instead.
909    /// - If $f(x,m)\leq -2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
910    ///   returned instead.
911    /// - If $f(x,m)\leq -2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
912    ///   is returned instead.
913    /// - If $0<f(x,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
914    /// - If $-2^{-2^{30}-1}\leq f(x,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
915    ///
916    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
917    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
918    /// more than $2^{30}$ bits of precision; overflow also occurs for an input of magnitude about
919    /// $2^{-2^{30}}$, whose reciprocal alone is beyond the largest finite [`Float`].
920    ///
921    /// If you want to specify an output precision, consider using [`Float::cot_prec_round`]
922    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
923    /// [`Float::cot`] instead.
924    ///
925    /// # Worst-case complexity
926    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
927    ///
928    /// $M(n, e) = O((n+e) \log (n+e))$
929    ///
930    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
931    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
932    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
933    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
934    /// e$ bits. Unlike most functions, `cot` therefore gets slower as the magnitude of its input
935    /// grows, not just as the precision does.
936    ///
937    /// # Panics
938    /// Panics if `rm` is `Exact`, since the cotangent of a finite nonzero [`Float`] is never
939    /// exactly representable.
940    ///
941    /// # Examples
942    /// ```
943    /// use malachite_base::rounding_modes::RoundingMode::*;
944    /// use malachite_float::Float;
945    /// use std::cmp::Ordering::*;
946    ///
947    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.cot_round(Floor);
948    /// assert_eq!(c.to_string(), "0.64209261593433070300641998659417");
949    /// assert_eq!(o, Less);
950    ///
951    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.cot_round(Ceiling);
952    /// assert_eq!(c.to_string(), "0.64209261593433070300641998659496");
953    /// assert_eq!(o, Greater);
954    ///
955    /// let (c, o) = Float::from_unsigned_prec(1u32, 100).0.cot_round(Nearest);
956    /// assert_eq!(c.to_string(), "0.64209261593433070300641998659417");
957    /// assert_eq!(o, Less);
958    /// ```
959    #[inline]
960    pub fn cot_round(self, rm: RoundingMode) -> (Self, Ordering) {
961        let prec = self.significant_bits();
962        self.cot_prec_round(prec, rm)
963    }
964
965    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result with the specified
966    /// rounding mode. The [`Float`] is taken by reference. An [`Ordering`] is also returned,
967    /// indicating whether the rounded cotangent is less than, equal to, or greater than the exact
968    /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function
969    /// returns a `NaN` it also returns `Equal`.
970    ///
971    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
972    /// description of the possible rounding modes.
973    ///
974    /// $$
975    /// f(x,m) = \cot x+\varepsilon.
976    /// $$
977    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
978    /// - If $x$ is finite and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot
979    ///   x|\rfloor-p+1}$, where $p$ is the precision of the input.
980    /// - If $x$ is finite and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot
981    ///   x|\rfloor-p}$, where $p$ is the precision of the input.
982    ///
983    /// If the output has a precision, it is the precision of the input.
984    ///
985    /// Special cases:
986    /// - $f(\text{NaN},m)=\text{NaN}$
987    /// - $f(\pm\infty,m)=\text{NaN}$
988    /// - $f(\pm0.0,m)=\pm\infty$
989    ///
990    /// Overflow:
991    /// - If $f(x,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
992    ///   returned instead.
993    /// - If $f(x,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
994    ///   returned instead.
995    /// - If $f(x,m)\leq -2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
996    ///   returned instead.
997    /// - If $f(x,m)\leq -2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
998    ///   is returned instead.
999    /// - If $0<f(x,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1000    /// - If $-2^{-2^{30}-1}\leq f(x,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1001    ///
1002    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
1003    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
1004    /// more than $2^{30}$ bits of precision; overflow also occurs for an input of magnitude about
1005    /// $2^{-2^{30}}$, whose reciprocal alone is beyond the largest finite [`Float`].
1006    ///
1007    /// If you want to specify an output precision, consider using [`Float::cot_prec_round_ref`]
1008    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
1009    /// `(&Float).cot()` instead.
1010    ///
1011    /// # Worst-case complexity
1012    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
1013    ///
1014    /// $M(n, e) = O((n+e) \log (n+e))$
1015    ///
1016    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
1017    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
1018    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
1019    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
1020    /// e$ bits. Unlike most functions, `cot` therefore gets slower as the magnitude of its input
1021    /// grows, not just as the precision does.
1022    ///
1023    /// # Panics
1024    /// Panics if `rm` is `Exact`, since the cotangent of a finite nonzero [`Float`] is never
1025    /// exactly representable.
1026    ///
1027    /// # Examples
1028    /// ```
1029    /// use malachite_base::rounding_modes::RoundingMode::*;
1030    /// use malachite_float::Float;
1031    /// use std::cmp::Ordering::*;
1032    ///
1033    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_round_ref(Floor);
1034    /// assert_eq!(c.to_string(), "0.64209261593433070300641998659417");
1035    /// assert_eq!(o, Less);
1036    ///
1037    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_round_ref(Ceiling);
1038    /// assert_eq!(c.to_string(), "0.64209261593433070300641998659496");
1039    /// assert_eq!(o, Greater);
1040    ///
1041    /// let (c, o) = (&Float::from_unsigned_prec(1u32, 100).0).cot_round_ref(Nearest);
1042    /// assert_eq!(c.to_string(), "0.64209261593433070300641998659417");
1043    /// assert_eq!(o, Less);
1044    /// ```
1045    #[inline]
1046    pub fn cot_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
1047        self.cot_prec_round_ref(self.significant_bits(), rm)
1048    }
1049
1050    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result to the specified
1051    /// precision and with the specified rounding mode. The [`Float`] is replaced by the result, and
1052    /// an [`Ordering`] is returned, indicating whether the rounded cotangent is less than, equal
1053    /// to, or greater than the exact cotangent. Although `NaN`s are not comparable to any
1054    /// [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
1055    ///
1056    /// See [`RoundingMode`] for a description of the possible rounding modes.
1057    ///
1058    /// $$
1059    /// x \gets \cot x+\varepsilon.
1060    /// $$
1061    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
1062    /// - If $x$ is finite and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot
1063    ///   x|\rfloor-p+1}$.
1064    /// - If $x$ is finite and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot
1065    ///   x|\rfloor-p}$.
1066    ///
1067    /// If the output has a precision, it is `prec`.
1068    ///
1069    /// See the [`Float::cot_prec_round`] documentation for information on special cases and
1070    /// overflow.
1071    ///
1072    /// If you know you'll be using `Nearest`, consider using [`Float::cot_prec_assign`] instead. If
1073    /// you know that your target precision is the precision of the input, consider using
1074    /// [`Float::cot_round_assign`] instead. If both of these things are true, consider using
1075    /// [`Float::cot_assign`] instead.
1076    ///
1077    /// # Worst-case complexity
1078    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
1079    ///
1080    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
1081    ///
1082    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is
1083    /// `self.significant_bits()`, and $e$ is the exponent of `self` (0 if `self` has no exponent or
1084    /// a negative one): the sine and cosine at working precision $n$ (for large $n$ by binary
1085    /// splitting of the Taylor series, otherwise the cosine, from which the sine is derived), and
1086    /// their quotient, cost the first term, and for $|x| \geq 4$ the argument is reduced modulo
1087    /// $2\pi$, which requires $\pi$ to about $n + e$ bits and a remainder of the $m$-bit input.
1088    /// Unlike most functions, `cot` therefore gets slower as the magnitude of its input grows, not
1089    /// just as the precision does.
1090    ///
1091    /// # Panics
1092    /// Panics if `rm` is `Exact`, since the cotangent of a finite nonzero [`Float`] is never
1093    /// exactly representable, or if `prec` is zero.
1094    ///
1095    /// # Examples
1096    /// ```
1097    /// use malachite_base::rounding_modes::RoundingMode::*;
1098    /// use malachite_float::Float;
1099    /// use std::cmp::Ordering::*;
1100    ///
1101    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1102    /// assert_eq!(x.cot_prec_round_assign(5, Floor), Less);
1103    /// assert_eq!(x.to_string(), "0.625");
1104    ///
1105    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1106    /// assert_eq!(x.cot_prec_round_assign(5, Ceiling), Greater);
1107    /// assert_eq!(x.to_string(), "0.656");
1108    ///
1109    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1110    /// assert_eq!(x.cot_prec_round_assign(5, Nearest), Greater);
1111    /// assert_eq!(x.to_string(), "0.656");
1112    ///
1113    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1114    /// assert_eq!(x.cot_prec_round_assign(20, Floor), Less);
1115    /// assert_eq!(x.to_string(), "0.64209175");
1116    ///
1117    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1118    /// assert_eq!(x.cot_prec_round_assign(20, Ceiling), Greater);
1119    /// assert_eq!(x.to_string(), "0.64209270");
1120    ///
1121    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1122    /// assert_eq!(x.cot_prec_round_assign(20, Nearest), Greater);
1123    /// assert_eq!(x.to_string(), "0.64209270");
1124    /// ```
1125    #[inline]
1126    pub fn cot_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
1127        let o;
1128        (*self, o) = self.cot_prec_round_ref(prec, rm);
1129        o
1130    }
1131
1132    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result to the nearest value of
1133    /// the specified precision. The [`Float`] is replaced by the result, and an [`Ordering`] is
1134    /// returned, indicating whether the rounded cotangent is less than, equal to, or greater than
1135    /// the exact cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this
1136    /// function sets a `NaN` it also returns `Equal`.
1137    ///
1138    /// If the cotangent is equidistant from two [`Float`]s with the specified precision, the
1139    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1140    /// description of the `Nearest` rounding mode.
1141    ///
1142    /// $$
1143    /// x \gets \cot x+\varepsilon.
1144    /// $$
1145    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
1146    /// - If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$.
1147    ///
1148    /// If the output has a precision, it is `prec`.
1149    ///
1150    /// See the [`Float::cot_prec`] documentation for information on special cases and overflow.
1151    ///
1152    /// If you want to use a rounding mode other than `Nearest`, consider using
1153    /// [`Float::cot_prec_round_assign`] instead. If you know that your target precision is the
1154    /// precision of the input, consider using [`Float::cot_assign`] instead.
1155    ///
1156    /// # Worst-case complexity
1157    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
1158    ///
1159    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
1160    ///
1161    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is
1162    /// `self.significant_bits()`, and $e$ is the exponent of `self` (0 if `self` has no exponent or
1163    /// a negative one): the sine and cosine at working precision $n$ (for large $n$ by binary
1164    /// splitting of the Taylor series, otherwise the cosine, from which the sine is derived), and
1165    /// their quotient, cost the first term, and for $|x| \geq 4$ the argument is reduced modulo
1166    /// $2\pi$, which requires $\pi$ to about $n + e$ bits and a remainder of the $m$-bit input.
1167    /// Unlike most functions, `cot` therefore gets slower as the magnitude of its input grows, not
1168    /// just as the precision does.
1169    ///
1170    /// # Panics
1171    /// Panics if `prec` is zero.
1172    ///
1173    /// # Examples
1174    /// ```
1175    /// use malachite_float::Float;
1176    /// use std::cmp::Ordering::*;
1177    ///
1178    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1179    /// assert_eq!(x.cot_prec_assign(5), Greater);
1180    /// assert_eq!(x.to_string(), "0.656");
1181    ///
1182    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1183    /// assert_eq!(x.cot_prec_assign(20), Greater);
1184    /// assert_eq!(x.to_string(), "0.64209270");
1185    /// ```
1186    #[inline]
1187    pub fn cot_prec_assign(&mut self, prec: u64) -> Ordering {
1188        self.cot_prec_round_assign(prec, Nearest)
1189    }
1190
1191    /// Computes $\cot x$, the cotangent of a [`Float`], rounding the result with the specified
1192    /// rounding mode. The [`Float`] is replaced by the result, and an [`Ordering`] is returned,
1193    /// indicating whether the rounded cotangent is less than, equal to, or greater than the exact
1194    /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
1195    /// a `NaN` it also returns `Equal`.
1196    ///
1197    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
1198    /// description of the possible rounding modes.
1199    ///
1200    /// $$
1201    /// x \gets \cot x+\varepsilon.
1202    /// $$
1203    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
1204    /// - If $x$ is finite and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot
1205    ///   x|\rfloor-p+1}$, where $p$ is the precision of the input.
1206    /// - If $x$ is finite and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot
1207    ///   x|\rfloor-p}$, where $p$ is the precision of the input.
1208    ///
1209    /// If the output has a precision, it is the precision of the input.
1210    ///
1211    /// See the [`Float::cot_round`] documentation for information on special cases and overflow.
1212    ///
1213    /// If you want to specify an output precision, consider using [`Float::cot_prec_round_assign`]
1214    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
1215    /// [`Float::cot_assign`] instead.
1216    ///
1217    /// # Worst-case complexity
1218    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
1219    ///
1220    /// $M(n, e) = O((n+e) \log (n+e))$
1221    ///
1222    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
1223    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
1224    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
1225    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
1226    /// e$ bits. Unlike most functions, `cot` therefore gets slower as the magnitude of its input
1227    /// grows, not just as the precision does.
1228    ///
1229    /// # Panics
1230    /// Panics if `rm` is `Exact`, since the cotangent of a finite nonzero [`Float`] is never
1231    /// exactly representable.
1232    ///
1233    /// # Examples
1234    /// ```
1235    /// use malachite_base::rounding_modes::RoundingMode::*;
1236    /// use malachite_float::Float;
1237    /// use std::cmp::Ordering::*;
1238    ///
1239    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1240    /// assert_eq!(x.cot_round_assign(Floor), Less);
1241    /// assert_eq!(x.to_string(), "0.64209261593433070300641998659417");
1242    ///
1243    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1244    /// assert_eq!(x.cot_round_assign(Ceiling), Greater);
1245    /// assert_eq!(x.to_string(), "0.64209261593433070300641998659496");
1246    ///
1247    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
1248    /// assert_eq!(x.cot_round_assign(Nearest), Less);
1249    /// assert_eq!(x.to_string(), "0.64209261593433070300641998659417");
1250    /// ```
1251    #[inline]
1252    pub fn cot_round_assign(&mut self, rm: RoundingMode) -> Ordering {
1253        let prec = self.significant_bits();
1254        self.cot_prec_round_assign(prec, rm)
1255    }
1256
1257    /// Computes $\cot x$, the cotangent of a [`Rational`], rounding the result to the specified
1258    /// precision and with the specified rounding mode and returning the result as a [`Float`]. The
1259    /// [`Rational`] is taken by value. An [`Ordering`] is also returned, indicating whether the
1260    /// rounded cotangent is less than, equal to, or greater than the exact cotangent.
1261    ///
1262    /// See [`RoundingMode`] for a description of the possible rounding modes.
1263    ///
1264    /// $$
1265    /// f(x,p,m) = \cot x+\varepsilon.
1266    /// $$
1267    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p+1}$.
1268    /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot x|\rfloor-p}$.
1269    ///
1270    /// These bounds do not apply when the result overflows; see below.
1271    ///
1272    /// The output has precision `prec`.
1273    ///
1274    /// Special cases:
1275    /// - $f(0,p,m)=\infty$.
1276    ///
1277    /// Overflow:
1278    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1279    ///   returned instead.
1280    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
1281    ///   returned instead.
1282    /// - If $f(x,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1283    ///   returned instead.
1284    /// - If $f(x,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`, $-(1-(1/2)^p)2^{2^{30}-1}$
1285    ///   is returned instead.
1286    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1287    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1288    ///
1289    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
1290    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
1291    /// a denominator of more than $2^{30}$ bits; overflow also occurs for an input of magnitude
1292    /// about $2^{-2^{30}}$ or below, whose reciprocal alone is beyond the largest finite [`Float`].
1293    ///
1294    /// If you know you'll be using `Nearest`, consider using [`Float::cot_rational_prec`] instead.
1295    ///
1296    /// # Worst-case complexity
1297    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
1298    ///
1299    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
1300    ///
1301    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is `x.significant_bits()`,
1302    /// and $e$ is `x.floor_log_base_2_abs()` (taken as 0 when it is negative or $x = 0$): the input
1303    /// is rounded to a working precision and its [`Float`] sine and cosine taken there, then
1304    /// divided, which for $|x| \geq 2$ reduces the argument modulo $2\pi$ and so needs $\pi$ to
1305    /// about $n + e$ bits.
1306    ///
1307    /// # Panics
1308    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1309    /// with the given precision (which is the case for every nonzero input).
1310    ///
1311    /// # Examples
1312    /// ```
1313    /// use malachite_base::rounding_modes::RoundingMode::*;
1314    /// use malachite_float::Float;
1315    /// use malachite_q::Rational;
1316    /// use std::cmp::Ordering::*;
1317    ///
1318    /// let (c, o) = Float::cot_rational_prec_round(Rational::from_unsigneds(3u8, 5), 5, Floor);
1319    /// assert_eq!(c.to_string(), "1.44");
1320    /// assert_eq!(o, Less);
1321    ///
1322    /// let (c, o) = Float::cot_rational_prec_round(Rational::from_unsigneds(3u8, 5), 5, Ceiling);
1323    /// assert_eq!(c.to_string(), "1.50");
1324    /// assert_eq!(o, Greater);
1325    ///
1326    /// let (c, o) = Float::cot_rational_prec_round(Rational::from_unsigneds(3u8, 5), 20, Floor);
1327    /// assert_eq!(c.to_string(), "1.4616947");
1328    /// assert_eq!(o, Less);
1329    ///
1330    /// let (c, o) = Float::cot_rational_prec_round(Rational::from_unsigneds(3u8, 5), 20, Ceiling);
1331    /// assert_eq!(c.to_string(), "1.4616966");
1332    /// assert_eq!(o, Greater);
1333    /// ```
1334    #[inline]
1335    #[allow(clippy::needless_pass_by_value)]
1336    pub fn cot_rational_prec_round(x: Rational, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1337        Self::cot_rational_prec_round_ref(&x, prec, rm)
1338    }
1339
1340    /// Computes $\cot x$, the cotangent of a [`Rational`], rounding the result to the specified
1341    /// precision and with the specified rounding mode and returning the result as a [`Float`]. The
1342    /// [`Rational`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
1343    /// rounded cotangent is less than, equal to, or greater than the exact cotangent.
1344    ///
1345    /// See [`RoundingMode`] for a description of the possible rounding modes.
1346    ///
1347    /// $$
1348    /// f(x,p,m) = \cot x+\varepsilon.
1349    /// $$
1350    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p+1}$.
1351    /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot x|\rfloor-p}$.
1352    ///
1353    /// These bounds do not apply when the result overflows.
1354    ///
1355    /// The output has precision `prec`.
1356    ///
1357    /// Special cases:
1358    /// - $f(0,p,m)=\infty$.
1359    ///
1360    /// See the [`Float::cot_rational_prec_round`] documentation for information on overflow.
1361    ///
1362    /// If you know you'll be using `Nearest`, consider using [`Float::cot_rational_prec_ref`]
1363    /// instead.
1364    ///
1365    /// # Worst-case complexity
1366    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
1367    ///
1368    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
1369    ///
1370    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is `x.significant_bits()`,
1371    /// and $e$ is `x.floor_log_base_2_abs()` (taken as 0 when it is negative or $x = 0$): the input
1372    /// is rounded to a working precision and its [`Float`] sine and cosine taken there, then
1373    /// divided, which for $|x| \geq 2$ reduces the argument modulo $2\pi$ and so needs $\pi$ to
1374    /// about $n + e$ bits.
1375    ///
1376    /// # Panics
1377    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1378    /// with the given precision (which is the case for every nonzero input).
1379    ///
1380    /// # Examples
1381    /// ```
1382    /// use malachite_base::rounding_modes::RoundingMode::*;
1383    /// use malachite_float::Float;
1384    /// use malachite_q::Rational;
1385    /// use std::cmp::Ordering::*;
1386    ///
1387    /// let (c, o) =
1388    ///     Float::cot_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 5, Floor);
1389    /// assert_eq!(c.to_string(), "1.44");
1390    /// assert_eq!(o, Less);
1391    ///
1392    /// let (c, o) =
1393    ///     Float::cot_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 5, Ceiling);
1394    /// assert_eq!(c.to_string(), "1.50");
1395    /// assert_eq!(o, Greater);
1396    ///
1397    /// let (c, o) =
1398    ///     Float::cot_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 20, Floor);
1399    /// assert_eq!(c.to_string(), "1.4616947");
1400    /// assert_eq!(o, Less);
1401    ///
1402    /// let (c, o) =
1403    ///     Float::cot_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 20, Ceiling);
1404    /// assert_eq!(c.to_string(), "1.4616966");
1405    /// assert_eq!(o, Greater);
1406    /// ```
1407    pub fn cot_rational_prec_round_ref(
1408        x: &Rational,
1409        prec: u64,
1410        rm: RoundingMode,
1411    ) -> (Self, Ordering) {
1412        assert_ne!(prec, 0);
1413        if *x == 0u32 {
1414            // cot(0) = infinity; a `Rational` zero has no sign, so the result is positive
1415            return (Self::INFINITY, Equal);
1416        }
1417        cot_rational_helper(x, prec, rm)
1418    }
1419
1420    /// Computes $\cot x$, the cotangent of a [`Rational`], rounding the result to the nearest value
1421    /// of the specified precision and returning the result as a [`Float`]. The [`Rational`] is
1422    /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded cotangent
1423    /// is less than, equal to, or greater than the exact cotangent.
1424    ///
1425    /// If the cotangent is equidistant from two [`Float`]s with the specified precision, the
1426    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1427    /// description of the `Nearest` rounding mode.
1428    ///
1429    /// $$
1430    /// f(x,p) = \cot x+\varepsilon,
1431    /// $$
1432    /// where $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot x|\rfloor-p}$ (unless the result overflows;
1433    /// see below).
1434    ///
1435    /// The output has precision `prec`.
1436    ///
1437    /// Special cases:
1438    /// - $f(0,p)=\infty$.
1439    ///
1440    /// Overflow:
1441    /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1442    /// - If $f(x,p)\leq -2^{2^{30}-1}$, $-\infty$ is returned instead.
1443    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1444    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
1445    ///
1446    /// Overflow requires an input within $2^{-2^{30}}$ of a nonzero multiple of $\pi$, and
1447    /// underflow an input within $2^{-2^{30}}$ of an odd multiple of $\pi/2$, either of which takes
1448    /// a denominator of more than $2^{30}$ bits; overflow also occurs for an input of magnitude
1449    /// about $2^{-2^{30}}$ or below, whose reciprocal alone is beyond the largest finite [`Float`].
1450    ///
1451    /// If you want to use a rounding mode other than `Nearest`, consider using
1452    /// [`Float::cot_rational_prec_round`] instead.
1453    ///
1454    /// # Worst-case complexity
1455    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
1456    ///
1457    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
1458    ///
1459    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is `x.significant_bits()`,
1460    /// and $e$ is `x.floor_log_base_2_abs()` (taken as 0 when it is negative or $x = 0$): the input
1461    /// is rounded to a working precision and its [`Float`] sine and cosine taken there, then
1462    /// divided, which for $|x| \geq 2$ reduces the argument modulo $2\pi$ and so needs $\pi$ to
1463    /// about $n + e$ bits.
1464    ///
1465    /// # Panics
1466    /// Panics if `prec` is zero.
1467    ///
1468    /// # Examples
1469    /// ```
1470    /// use malachite_float::Float;
1471    /// use malachite_q::Rational;
1472    /// use std::cmp::Ordering::*;
1473    ///
1474    /// let (c, o) = Float::cot_rational_prec(Rational::from_unsigneds(3u8, 5), 5);
1475    /// assert_eq!(c.to_string(), "1.44");
1476    /// assert_eq!(o, Less);
1477    ///
1478    /// let (c, o) = Float::cot_rational_prec(Rational::from_unsigneds(3u8, 5), 20);
1479    /// assert_eq!(c.to_string(), "1.4616966");
1480    /// assert_eq!(o, Greater);
1481    /// ```
1482    #[inline]
1483    #[allow(clippy::needless_pass_by_value)]
1484    pub fn cot_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
1485        Self::cot_rational_prec_round_ref(&x, prec, Nearest)
1486    }
1487
1488    /// Computes $\cot x$, the cotangent of a [`Rational`], rounding the result to the nearest value
1489    /// of the specified precision and returning the result as a [`Float`]. The [`Rational`] is
1490    /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
1491    /// cotangent is less than, equal to, or greater than the exact cotangent.
1492    ///
1493    /// If the cotangent is equidistant from two [`Float`]s with the specified precision, the
1494    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1495    /// description of the `Nearest` rounding mode.
1496    ///
1497    /// $$
1498    /// f(x,p) = \cot x+\varepsilon,
1499    /// $$
1500    /// where $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot x|\rfloor-p}$ (unless the result
1501    /// overflows).
1502    ///
1503    /// The output has precision `prec`.
1504    ///
1505    /// Special cases:
1506    /// - $f(0,p)=\infty$.
1507    ///
1508    /// See the [`Float::cot_rational_prec`] documentation for information on overflow.
1509    ///
1510    /// If you want to use a rounding mode other than `Nearest`, consider using
1511    /// [`Float::cot_rational_prec_round_ref`] instead.
1512    ///
1513    /// # Worst-case complexity
1514    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
1515    ///
1516    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
1517    ///
1518    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is `x.significant_bits()`,
1519    /// and $e$ is `x.floor_log_base_2_abs()` (taken as 0 when it is negative or $x = 0$): the input
1520    /// is rounded to a working precision and its [`Float`] sine and cosine taken there, then
1521    /// divided, which for $|x| \geq 2$ reduces the argument modulo $2\pi$ and so needs $\pi$ to
1522    /// about $n + e$ bits.
1523    ///
1524    /// # Panics
1525    /// Panics if `prec` is zero.
1526    ///
1527    /// # Examples
1528    /// ```
1529    /// use malachite_float::Float;
1530    /// use malachite_q::Rational;
1531    /// use std::cmp::Ordering::*;
1532    ///
1533    /// let (c, o) = Float::cot_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 5);
1534    /// assert_eq!(c.to_string(), "1.44");
1535    /// assert_eq!(o, Less);
1536    ///
1537    /// let (c, o) = Float::cot_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 20);
1538    /// assert_eq!(c.to_string(), "1.4616966");
1539    /// assert_eq!(o, Greater);
1540    /// ```
1541    #[inline]
1542    pub fn cot_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
1543        Self::cot_rational_prec_round_ref(x, prec, Nearest)
1544    }
1545
1546    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn,
1547    /// rounding the result to the specified precision and with the specified rounding mode. The
1548    /// [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether the
1549    /// rounded cotangent is less than, equal to, or greater than the exact cotangent. Although
1550    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1551    /// returns `Equal`.
1552    ///
1553    /// See [`RoundingMode`] for a description of the possible rounding modes.
1554    ///
1555    /// $$
1556    /// f(x,u,p,m) = \cot(2\pi x/u)+\varepsilon.
1557    /// $$
1558    /// - If $x$ is not finite, $u=0$, or $x/u$ is a multiple of $1/8$, $\varepsilon$ may be ignored
1559    ///   or assumed to be 0.
1560    /// - If $x$ is finite, $u\neq 0$, and $m$ is not `Nearest`, then $|\varepsilon| <
1561    ///   2^{\lfloor\log_2 |\cot(2\pi x/u)|\rfloor-p+1}$.
1562    /// - If $x$ is finite, $u\neq 0$, and $m$ is `Nearest`, then $|\varepsilon| \leq
1563    ///   2^{\lfloor\log_2 |\cot(2\pi x/u)|\rfloor-p}$.
1564    ///
1565    /// If the output has a precision, it is `prec`.
1566    ///
1567    /// Special cases:
1568    /// - $f(\text{NaN},u,p,m)=\text{NaN}$
1569    /// - $f(\pm\infty,u,p,m)=\text{NaN}$
1570    /// - $f(x,0,p,m)=\text{NaN}$
1571    /// - $f(\\pm0.0,u,p,m)=\\pm\\infty$
1572    /// - If $x/u$ is a multiple of $1/2$, the cotangent has a pole there, and the result is exactly
1573    ///   $\\pm\\infty$: the sine is a zero carrying the sign of $x$ and the cosine is $\\pm1$, so
1574    ///   the sign is that of $x$ at an even multiple and the opposite at an odd one. Keeping that
1575    ///   identity is what makes the function odd.
1576    /// - If $x/u$ is an odd multiple of $1/4$, the result is exactly $\\pm0.0$, with the sign of
1577    ///   the sine there.
1578    /// - If $x/u$ is an odd multiple of $1/8$, the result is exactly $\\pm1$.
1579    ///
1580    /// When $x/u$ in lowest terms has denominator 3 or 6, the result is $\pm\sqrt3/3$, and when it
1581    /// has denominator 12, $\pm\sqrt3$; each is computed from a single correctly rounded constant
1582    /// rather than from $\pi$, a sine, and a cosine, which is far faster.
1583    ///
1584    /// Overflow:
1585    /// - If $f(x,u,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1586    ///   returned instead.
1587    /// - If $f(x,u,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1588    ///   is returned instead.
1589    /// - If $f(x,u,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Floor`, `Up`, or `Nearest`, $-\infty$ is
1590    ///   returned instead.
1591    /// - If $f(x,u,p,m)\leq -2^{2^{30}-1}$ and $m$ is `Ceiling` or `Down`,
1592    ///   $-(1-(1/2)^p)2^{2^{30}-1}$ is returned instead.
1593    /// - If $0<f(x,u,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1594    /// - If $-2^{-2^{30}-1}\leq f(x,u,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1595    ///
1596    /// Overflow requires $x/u$ within $2^{-2^{30}}$ of a multiple of $1/2$ without being one, and
1597    /// underflow $x/u$ within $2^{-2^{30}}$ of an odd multiple of $1/4$ without being one, either
1598    /// of which takes more than $2^{30}$ bits of precision; overflow also occurs for an $x/u$ so
1599    /// small that $2\pi x/u$ is below $2^{-2^{30}}$.
1600    ///
1601    /// If you know you'll be using `Nearest`, consider using [`Float::cot_with_period_prec`]
1602    /// instead. If you know that your target precision is the precision of the input, consider
1603    /// using [`Float::cot_with_period_round`] instead.
1604    ///
1605    /// # Worst-case complexity
1606    /// $T(n, m, e) = O(n (\log n)^3 \log\log n + (n+m+e) (\log (n+m+e))^2 \log\log (n+m+e))$
1607    ///
1608    /// $M(n, m, e) = O((n+m+e) \log (n+m+e))$
1609    ///
1610    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, $m$ is
1611    /// `self.significant_bits()`, and $e$ is the exponent of `self` (0 if `self` has no exponent or
1612    /// a negative one): the argument is reduced modulo $u$ exactly, and the sine and cosine of
1613    /// $2\pi x/u$ are then taken at a working precision of about $n + e$ bits, which needs $\pi$ to
1614    /// that many bits, and divided.
1615    ///
1616    /// # Panics
1617    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1618    /// with the given precision (which is the case unless $x/u$ is a multiple of $1/8$, or $x$ is
1619    /// zero or not finite, or $u$ is zero).
1620    ///
1621    /// # Examples
1622    /// ```
1623    /// use malachite_base::num::basic::traits::One;
1624    /// use malachite_base::rounding_modes::RoundingMode::*;
1625    /// use malachite_float::Float;
1626    /// use std::cmp::Ordering::*;
1627    ///
1628    /// let (t, o) = Float::ONE.cot_with_period_prec_round(7, 10, Floor);
1629    /// assert_eq!(t.to_string(), "0.79688");
1630    /// assert_eq!(o, Less);
1631    ///
1632    /// let (t, o) = Float::ONE.cot_with_period_prec_round(7, 10, Ceiling);
1633    /// assert_eq!(t.to_string(), "0.79785");
1634    /// assert_eq!(o, Greater);
1635    ///
1636    /// // a quarter turn is exactly 0
1637    /// let (t, o) = Float::from(90u32).cot_with_period_prec_round(360, 10, Exact);
1638    /// assert_eq!(t.to_string(), "0.0");
1639    /// assert_eq!(o, Equal);
1640    ///
1641    /// // a half turn is a pole
1642    /// let (t, o) = Float::from(180u32).cot_with_period_prec_round(360, 10, Exact);
1643    /// assert_eq!(t.to_string(), "-Infinity");
1644    /// assert_eq!(o, Equal);
1645    ///
1646    /// // a twelfth of a turn: sqrt(3)
1647    /// let (t, o) = Float::from(30u32).cot_with_period_prec_round(360, 10, Nearest);
1648    /// assert_eq!(t.to_string(), "1.7324");
1649    /// assert_eq!(o, Greater);
1650    /// ```
1651    #[inline]
1652    pub fn cot_with_period_prec_round(
1653        self,
1654        u: u64,
1655        prec: u64,
1656        rm: RoundingMode,
1657    ) -> (Self, Ordering) {
1658        self.cot_with_period_prec_round_ref(u, prec, rm)
1659    }
1660
1661    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn,
1662    /// rounding the result to the specified precision and with the specified rounding mode. The
1663    /// [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
1664    /// rounded cotangent is less than, equal to, or greater than the exact cotangent. Although
1665    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1666    /// returns `Equal`.
1667    ///
1668    /// See [`Float::cot_with_period_prec_round`] for the error bounds, the special and closed-form
1669    /// cases, overflow, and the complexity; this function behaves the same way.
1670    ///
1671    /// # Panics
1672    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1673    /// with the given precision.
1674    ///
1675    /// # Examples
1676    /// ```
1677    /// use malachite_base::num::basic::traits::One;
1678    /// use malachite_base::rounding_modes::RoundingMode::*;
1679    /// use malachite_float::Float;
1680    /// use std::cmp::Ordering::*;
1681    ///
1682    /// let (t, o) = Float::ONE.cot_with_period_prec_round_ref(7, 10, Floor);
1683    /// assert_eq!(t.to_string(), "0.79688");
1684    /// assert_eq!(o, Less);
1685    /// ```
1686    pub fn cot_with_period_prec_round_ref(
1687        &self,
1688        u: u64,
1689        prec: u64,
1690        rm: RoundingMode,
1691    ) -> (Self, Ordering) {
1692        assert_ne!(prec, 0);
1693        match &self.0 {
1694            // for u=0, return NaN
1695            _ if u == 0 => (Self::NAN, Equal),
1696            NaN | Infinity { .. } => (Self::NAN, Equal),
1697            // x is zero: cot(±0) = ±infinity
1698            Zero { .. } => (
1699                if self.is_sign_negative() {
1700                    Self::NEGATIVE_INFINITY
1701                } else {
1702                    Self::INFINITY
1703                },
1704                Equal,
1705            ),
1706            Finite { .. } => cot_with_period_prec_round_normal_ref(self, u, prec, rm),
1707        }
1708    }
1709
1710    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn,
1711    /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
1712    /// by value. An [`Ordering`] is also returned, indicating whether the rounded cotangent is less
1713    /// than, equal to, or greater than the exact cotangent. Although `NaN`s are not comparable to
1714    /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1715    ///
1716    /// If the cotangent is equidistant from two [`Float`]s with the specified precision, the
1717    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1718    /// description of the `Nearest` rounding mode.
1719    ///
1720    /// See [`Float::cot_with_period_prec_round`] for the error bounds, the special and closed-form
1721    /// cases, overflow, and the complexity; this function behaves the same way with `Nearest`.
1722    ///
1723    /// If you want to use a rounding mode other than `Nearest`, consider using
1724    /// [`Float::cot_with_period_prec_round`] instead.
1725    ///
1726    /// # Panics
1727    /// Panics if `prec` is zero.
1728    ///
1729    /// # Examples
1730    /// ```
1731    /// use malachite_base::num::basic::traits::One;
1732    /// use malachite_float::Float;
1733    /// use std::cmp::Ordering::*;
1734    ///
1735    /// let (t, o) = Float::ONE.cot_with_period_prec(7, 10);
1736    /// assert_eq!(t.to_string(), "0.79785");
1737    /// assert_eq!(o, Greater);
1738    ///
1739    /// // an eighth of a turn is exactly 1
1740    /// let (t, o) = Float::ONE.cot_with_period_prec(8, 10);
1741    /// assert_eq!(t.to_string(), "1.0000");
1742    /// assert_eq!(o, Equal);
1743    /// ```
1744    #[inline]
1745    pub fn cot_with_period_prec(self, u: u64, prec: u64) -> (Self, Ordering) {
1746        self.cot_with_period_prec_round(u, prec, Nearest)
1747    }
1748
1749    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn,
1750    /// rounding the result to the nearest value of the specified precision. The [`Float`] is taken
1751    /// by reference. An [`Ordering`] is also returned, indicating whether the rounded cotangent is
1752    /// less than, equal to, or greater than the exact cotangent. Although `NaN`s are not comparable
1753    /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1754    ///
1755    /// See [`Float::cot_with_period_prec`] and [`Float::cot_with_period_prec_round`]; this function
1756    /// behaves the same way.
1757    ///
1758    /// # Panics
1759    /// Panics if `prec` is zero.
1760    ///
1761    /// # Examples
1762    /// ```
1763    /// use malachite_base::num::basic::traits::One;
1764    /// use malachite_float::Float;
1765    /// use std::cmp::Ordering::*;
1766    ///
1767    /// let (t, o) = Float::ONE.cot_with_period_prec_ref(7, 10);
1768    /// assert_eq!(t.to_string(), "0.79785");
1769    /// assert_eq!(o, Greater);
1770    /// ```
1771    #[inline]
1772    pub fn cot_with_period_prec_ref(&self, u: u64, prec: u64) -> (Self, Ordering) {
1773        self.cot_with_period_prec_round_ref(u, prec, Nearest)
1774    }
1775
1776    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn,
1777    /// rounding the result to the precision of the input and with the specified rounding mode. The
1778    /// [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether the
1779    /// rounded cotangent is less than, equal to, or greater than the exact cotangent. Although
1780    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1781    /// returns `Equal`.
1782    ///
1783    /// See [`Float::cot_with_period_prec_round`] for the error bounds, the special and closed-form
1784    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
1785    /// the precision of the input.
1786    ///
1787    /// If you want to specify an output precision, consider using
1788    /// [`Float::cot_with_period_prec_round`] instead.
1789    ///
1790    /// # Panics
1791    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1792    /// the input.
1793    ///
1794    /// # Examples
1795    /// ```
1796    /// use malachite_base::rounding_modes::RoundingMode::*;
1797    /// use malachite_float::Float;
1798    /// use std::cmp::Ordering::*;
1799    ///
1800    /// let (t, o) = Float::from_unsigned_prec(1u32, 10)
1801    ///     .0
1802    ///     .cot_with_period_round(7, Floor);
1803    /// assert_eq!(t.to_string(), "0.79688");
1804    /// assert_eq!(o, Less);
1805    /// ```
1806    #[inline]
1807    pub fn cot_with_period_round(self, u: u64, rm: RoundingMode) -> (Self, Ordering) {
1808        let prec = self.significant_bits();
1809        self.cot_with_period_prec_round(u, prec, rm)
1810    }
1811
1812    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn,
1813    /// rounding the result to the precision of the input and with the specified rounding mode. The
1814    /// [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
1815    /// rounded cotangent is less than, equal to, or greater than the exact cotangent. Although
1816    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
1817    /// returns `Equal`.
1818    ///
1819    /// See [`Float::cot_with_period_round`] and [`Float::cot_with_period_prec_round`]; this
1820    /// function behaves the same way.
1821    ///
1822    /// # Panics
1823    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1824    /// the input.
1825    ///
1826    /// # Examples
1827    /// ```
1828    /// use malachite_base::rounding_modes::RoundingMode::*;
1829    /// use malachite_float::Float;
1830    /// use std::cmp::Ordering::*;
1831    ///
1832    /// let (t, o) = Float::from_unsigned_prec(1u32, 10)
1833    ///     .0
1834    ///     .cot_with_period_round_ref(7, Floor);
1835    /// assert_eq!(t.to_string(), "0.79688");
1836    /// assert_eq!(o, Less);
1837    /// ```
1838    #[inline]
1839    pub fn cot_with_period_round_ref(&self, u: u64, rm: RoundingMode) -> (Self, Ordering) {
1840        self.cot_with_period_prec_round_ref(u, self.significant_bits(), rm)
1841    }
1842
1843    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn (so
1844    /// that `u = 360` is degrees), rounding the result to the precision of the input and to the
1845    /// nearest [`Float`]. The [`Float`] is taken by value.
1846    ///
1847    /// If the cotangent is equidistant from two [`Float`]s with the precision of the input, the
1848    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1849    /// description of the `Nearest` rounding mode.
1850    ///
1851    /// See [`Float::cot_with_period_prec_round`] for the error bounds, the special and closed-form
1852    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
1853    /// the precision of the input and `rm` equal to `Nearest`.
1854    ///
1855    /// If you want to use a rounding mode other than `Nearest`, consider using
1856    /// [`Float::cot_with_period_round`] instead. If you want to specify an output precision,
1857    /// consider using [`Float::cot_with_period_prec`]. If you want both of these things, consider
1858    /// using [`Float::cot_with_period_prec_round`].
1859    ///
1860    /// # Examples
1861    /// ```
1862    /// use malachite_float::Float;
1863    ///
1864    /// let t = Float::from_unsigned_prec(1u32, 10).0.cot_with_period(7);
1865    /// assert_eq!(t.to_string(), "0.79785");
1866    ///
1867    /// // a quarter turn is exactly 0
1868    /// assert_eq!(Float::from(90u32).cot_with_period(360).to_string(), "0.0");
1869    /// ```
1870    #[inline]
1871    pub fn cot_with_period(self, u: u64) -> Self {
1872        let prec = self.significant_bits();
1873        self.cot_with_period_prec(u, prec).0
1874    }
1875
1876    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn (so
1877    /// that `u = 360` is degrees), rounding the result to the precision of the input and to the
1878    /// nearest [`Float`]. The [`Float`] is taken by reference.
1879    ///
1880    /// If the cotangent is equidistant from two [`Float`]s with the precision of the input, the
1881    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
1882    /// description of the `Nearest` rounding mode.
1883    ///
1884    /// See [`Float::cot_with_period_prec_round`] for the error bounds, the special and closed-form
1885    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
1886    /// the precision of the input and `rm` equal to `Nearest`.
1887    ///
1888    /// If you want to use a rounding mode other than `Nearest`, consider using
1889    /// [`Float::cot_with_period_round_ref`] instead. If you want to specify an output precision,
1890    /// consider using [`Float::cot_with_period_prec_ref`]. If you want both of these things,
1891    /// consider using [`Float::cot_with_period_prec_round_ref`].
1892    ///
1893    /// # Examples
1894    /// ```
1895    /// use malachite_float::Float;
1896    ///
1897    /// let t = (&Float::from_unsigned_prec(1u32, 10).0).cot_with_period_ref(7);
1898    /// assert_eq!(t.to_string(), "0.79785");
1899    /// ```
1900    #[inline]
1901    pub fn cot_with_period_ref(&self, u: u64) -> Self {
1902        self.cot_with_period_prec_ref(u, self.significant_bits()).0
1903    }
1904
1905    /// Replaces a [`Float`] measured in $u$ths of a turn with its cotangent, rounding the result to
1906    /// the specified precision and with the specified rounding mode. An [`Ordering`] is returned,
1907    /// indicating whether the rounded cotangent is less than, equal to, or greater than the exact
1908    /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function sets
1909    /// a `NaN` it also returns `Equal`.
1910    ///
1911    /// See [`Float::cot_with_period_prec_round`] for the error bounds, the special and closed-form
1912    /// cases, overflow, and the complexity; this function behaves the same way.
1913    ///
1914    /// # Panics
1915    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1916    /// with the given precision.
1917    ///
1918    /// # Examples
1919    /// ```
1920    /// use malachite_base::num::basic::traits::One;
1921    /// use malachite_base::rounding_modes::RoundingMode::*;
1922    /// use malachite_float::Float;
1923    /// use std::cmp::Ordering::*;
1924    ///
1925    /// let mut x = Float::ONE;
1926    /// assert_eq!(x.cot_with_period_prec_round_assign(7, 10, Floor), Less);
1927    /// assert_eq!(x.to_string(), "0.79688");
1928    /// ```
1929    #[inline]
1930    pub fn cot_with_period_prec_round_assign(
1931        &mut self,
1932        u: u64,
1933        prec: u64,
1934        rm: RoundingMode,
1935    ) -> Ordering {
1936        let (t, o) = self.cot_with_period_prec_round_ref(u, prec, rm);
1937        *self = t;
1938        o
1939    }
1940
1941    /// Replaces a [`Float`] measured in $u$ths of a turn with its cotangent, rounding the result to
1942    /// the nearest value of the specified precision. An [`Ordering`] is returned, indicating
1943    /// whether the rounded cotangent is less than, equal to, or greater than the exact cotangent.
1944    /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets a `NaN` it
1945    /// also returns `Equal`.
1946    ///
1947    /// See [`Float::cot_with_period_prec`] and [`Float::cot_with_period_prec_round`]; this function
1948    /// behaves the same way.
1949    ///
1950    /// # Panics
1951    /// Panics if `prec` is zero.
1952    ///
1953    /// # Examples
1954    /// ```
1955    /// use malachite_base::num::basic::traits::One;
1956    /// use malachite_float::Float;
1957    /// use std::cmp::Ordering::*;
1958    ///
1959    /// let mut x = Float::ONE;
1960    /// assert_eq!(x.cot_with_period_prec_assign(7, 10), Greater);
1961    /// assert_eq!(x.to_string(), "0.79785");
1962    /// ```
1963    #[inline]
1964    pub fn cot_with_period_prec_assign(&mut self, u: u64, prec: u64) -> Ordering {
1965        self.cot_with_period_prec_round_assign(u, prec, Nearest)
1966    }
1967
1968    /// Replaces a [`Float`] measured in $u$ths of a turn with its cotangent, rounding the result to
1969    /// the precision of the input and with the specified rounding mode. An [`Ordering`] is
1970    /// returned, indicating whether the rounded cotangent is less than, equal to, or greater than
1971    /// the exact cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this
1972    /// function sets a `NaN` it also returns `Equal`.
1973    ///
1974    /// See [`Float::cot_with_period_round`] and [`Float::cot_with_period_prec_round`]; this
1975    /// function behaves the same way.
1976    ///
1977    /// # Panics
1978    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the precision of
1979    /// the input.
1980    ///
1981    /// # Examples
1982    /// ```
1983    /// use malachite_base::rounding_modes::RoundingMode::*;
1984    /// use malachite_float::Float;
1985    /// use std::cmp::Ordering::*;
1986    ///
1987    /// let mut x = Float::from_unsigned_prec(1u32, 10).0;
1988    /// assert_eq!(x.cot_with_period_round_assign(7, Floor), Less);
1989    /// assert_eq!(x.to_string(), "0.79688");
1990    /// ```
1991    #[inline]
1992    pub fn cot_with_period_round_assign(&mut self, u: u64, rm: RoundingMode) -> Ordering {
1993        let prec = self.significant_bits();
1994        self.cot_with_period_prec_round_assign(u, prec, rm)
1995    }
1996
1997    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Float`] measured in $u$ths of a turn (so
1998    /// that `u = 360` is degrees), rounding the result to the precision of the input and to the
1999    /// nearest [`Float`]. The [`Float`] is replaced by the result.
2000    ///
2001    /// If the cotangent is equidistant from two [`Float`]s with the precision of the input, the
2002    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2003    /// description of the `Nearest` rounding mode.
2004    ///
2005    /// See [`Float::cot_with_period_prec_round`] for the error bounds, the special and closed-form
2006    /// cases, overflow, and the complexity; this function behaves the same way with `prec` equal to
2007    /// the precision of the input and `rm` equal to `Nearest`.
2008    ///
2009    /// If you want to use a rounding mode other than `Nearest`, consider using
2010    /// [`Float::cot_with_period_round_assign`] instead. If you want to specify an output precision,
2011    /// consider using [`Float::cot_with_period_prec_assign`]. If you want both of these things,
2012    /// consider using [`Float::cot_with_period_prec_round_assign`].
2013    ///
2014    /// # Examples
2015    /// ```
2016    /// use malachite_float::Float;
2017    ///
2018    /// let mut x = Float::from_unsigned_prec(1u32, 10).0;
2019    /// x.cot_with_period_assign(7);
2020    /// assert_eq!(x.to_string(), "0.79785");
2021    /// ```
2022    #[inline]
2023    pub fn cot_with_period_assign(&mut self, u: u64) {
2024        let prec = self.significant_bits();
2025        self.cot_with_period_prec_assign(u, prec);
2026    }
2027
2028    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Rational`] measured in $u$ths of a turn,
2029    /// rounding the result to the specified precision and with the specified rounding mode, and
2030    /// returning the result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is
2031    /// also returned, indicating whether the rounded cotangent is less than, equal to, or greater
2032    /// than the exact cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this
2033    /// function returns a `NaN` it also returns `Equal`.
2034    ///
2035    /// See [`RoundingMode`] for a description of the possible rounding modes.
2036    ///
2037    /// $$
2038    /// f(x,u,p,m) = \cot(2\pi x/u)+\varepsilon.
2039    /// $$
2040    /// - If $u=0$ or $x/u$ is a multiple of $1/8$, $\varepsilon$ may be ignored or assumed to be 0.
2041    /// - If $u\neq 0$ and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot(2\pi
2042    ///   x/u)|\rfloor-p+1}$.
2043    /// - If $u\neq 0$ and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |\cot(2\pi
2044    ///   x/u)|\rfloor-p}$.
2045    ///
2046    /// If the output has a precision, it is `prec`.
2047    ///
2048    /// Special cases:
2049    /// - $f(x,0,p,m)=\text{NaN}$
2050    /// - $f(0,u,p,m)=\infty$
2051    /// - If $x/u$ is a multiple of $1/2$, the cotangent has a pole there, and the result is exactly
2052    ///   $\\pm\\infty$: the sine is a zero carrying the sign of $x$ and the cosine is $\\pm1$, so
2053    ///   the sign is that of $x$ at an even multiple and the opposite at an odd one. Keeping that
2054    ///   identity is what makes the function odd.
2055    /// - If $x/u$ is an odd multiple of $1/4$, the result is exactly $\\pm0.0$, with the sign of
2056    ///   the sine there.
2057    /// - If $x/u$ is an odd multiple of $1/8$, the result is exactly $\\pm1$.
2058    ///
2059    /// When $x/u$ in lowest terms has denominator 3 or 6, the result is $\pm\sqrt3/3$, and when it
2060    /// has denominator 12, $\pm\sqrt3$; each is computed from a single correctly rounded constant
2061    /// rather than from $\pi$, a sine, and a cosine, which is far faster.
2062    ///
2063    /// Overflow and underflow are as for [`Float::cot_with_period_prec_round`]. Overflow requires
2064    /// $x/u$ within $2^{-2^{30}}$ of a multiple of $1/2$ without being one, and underflow $x/u$
2065    /// within $2^{-2^{30}}$ of an odd multiple of $1/4$ without being one, either of which takes a
2066    /// denominator of more than $2^{30}$ bits; overflow also occurs for an $x/u$ so small that
2067    /// $2\pi x/u$ is below $2^{-2^{30}}$, which a [`Rational`] can be however large its denominator
2068    /// is not.
2069    ///
2070    /// If you know you'll be using `Nearest`, consider using
2071    /// [`Float::cot_with_period_rational_prec`] instead.
2072    ///
2073    /// # Worst-case complexity
2074    /// $T(n, m) = O(n (\log n)^3 \log\log n + (n+m) (\log (n+m))^2 \log\log (n+m))$
2075    ///
2076    /// $M(n, m) = O((n+m) \log (n+m))$
2077    ///
2078    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2079    /// `x.significant_bits()`: the fraction of a turn is reduced modulo 1 exactly, so only its size
2080    /// and the precision drive the cost, not the magnitude of $x$.
2081    ///
2082    /// # Panics
2083    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2084    /// with the given precision (which is the case unless $x/u$ is a multiple of $1/8$, or $x$ or
2085    /// $u$ is zero).
2086    ///
2087    /// # Examples
2088    /// ```
2089    /// use malachite_base::num::basic::traits::One;
2090    /// use malachite_base::rounding_modes::RoundingMode::*;
2091    /// use malachite_float::Float;
2092    /// use malachite_q::Rational;
2093    /// use std::cmp::Ordering::*;
2094    ///
2095    /// let (t, o) = Float::cot_with_period_rational_prec_round(Rational::ONE, 7, 10, Floor);
2096    /// assert_eq!(t.to_string(), "0.79688");
2097    /// assert_eq!(o, Less);
2098    ///
2099    /// let (t, o) = Float::cot_with_period_rational_prec_round(Rational::ONE, 7, 10, Ceiling);
2100    /// assert_eq!(t.to_string(), "0.79785");
2101    /// assert_eq!(o, Greater);
2102    ///
2103    /// // a quarter turn is exactly 0
2104    /// let (t, o) = Float::cot_with_period_rational_prec_round(
2105    ///     Rational::from_unsigneds(1u8, 4),
2106    ///     1,
2107    ///     10,
2108    ///     Exact,
2109    /// );
2110    /// assert_eq!(t.to_string(), "0.0");
2111    /// assert_eq!(o, Equal);
2112    ///
2113    /// // a twelfth of a turn: sqrt(3)
2114    /// let (t, o) = Float::cot_with_period_rational_prec_round(
2115    ///     Rational::from_unsigneds(1u8, 12),
2116    ///     1,
2117    ///     10,
2118    ///     Nearest,
2119    /// );
2120    /// assert_eq!(t.to_string(), "1.7324");
2121    /// assert_eq!(o, Greater);
2122    /// ```
2123    #[inline]
2124    #[allow(clippy::needless_pass_by_value)]
2125    pub fn cot_with_period_rational_prec_round(
2126        x: Rational,
2127        u: u64,
2128        prec: u64,
2129        rm: RoundingMode,
2130    ) -> (Self, Ordering) {
2131        Self::cot_with_period_rational_prec_round_ref(&x, u, prec, rm)
2132    }
2133
2134    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Rational`] measured in $u$ths of a turn,
2135    /// rounding the result to the specified precision and with the specified rounding mode, and
2136    /// returning the result as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`]
2137    /// is also returned, indicating whether the rounded cotangent is less than, equal to, or
2138    /// greater than the exact cotangent. Although `NaN`s are not comparable to any [`Float`],
2139    /// whenever this function returns a `NaN` it also returns `Equal`.
2140    ///
2141    /// See [`Float::cot_with_period_rational_prec_round`] for the error bounds, the special and
2142    /// closed-form cases, overflow, and the complexity; this function behaves the same way.
2143    ///
2144    /// # Panics
2145    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2146    /// with the given precision.
2147    ///
2148    /// # Examples
2149    /// ```
2150    /// use malachite_base::num::basic::traits::One;
2151    /// use malachite_base::rounding_modes::RoundingMode::*;
2152    /// use malachite_float::Float;
2153    /// use malachite_q::Rational;
2154    /// use std::cmp::Ordering::*;
2155    ///
2156    /// let (t, o) = Float::cot_with_period_rational_prec_round_ref(&Rational::ONE, 7, 10, Floor);
2157    /// assert_eq!(t.to_string(), "0.79688");
2158    /// assert_eq!(o, Less);
2159    /// ```
2160    pub fn cot_with_period_rational_prec_round_ref(
2161        x: &Rational,
2162        u: u64,
2163        prec: u64,
2164        rm: RoundingMode,
2165    ) -> (Self, Ordering) {
2166        assert_ne!(prec, 0);
2167        // for u = 0, return NaN
2168        if u == 0 {
2169            return (Self::NAN, Equal);
2170        }
2171        // cot(0) = infinity (a `Rational` zero has no sign)
2172        if *x == 0u32 {
2173            return (Self::INFINITY, Equal);
2174        }
2175        // q = x/u, reduced to (-1, 1) with the sign of x: cot(2 pi q) has period 1 in q, and a
2176        // multiple of u is a pole, where the sine is a zero with the sign of x and the cosine is 1,
2177        // so the cotangent is an infinity with that sign
2178        let q = x / Rational::from(u) % Rational::ONE;
2179        if q == 0u32 {
2180            return (
2181                if *x < 0u32 {
2182                    Self::NEGATIVE_INFINITY
2183                } else {
2184                    Self::INFINITY
2185                },
2186                Equal,
2187            );
2188        }
2189        cot_turns_helper(&q, prec, rm)
2190    }
2191
2192    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Rational`] measured in $u$ths of a turn,
2193    /// rounding the result to the nearest value of the specified precision, and returning the
2194    /// result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned,
2195    /// indicating whether the rounded cotangent is less than, equal to, or greater than the exact
2196    /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function
2197    /// returns a `NaN` it also returns `Equal`.
2198    ///
2199    /// If the cotangent is equidistant from two [`Float`]s with the specified precision, the
2200    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2201    /// description of the `Nearest` rounding mode.
2202    ///
2203    /// See [`Float::cot_with_period_rational_prec_round`] for the error bounds, the special and
2204    /// closed-form cases, overflow, and the complexity; this function behaves the same way with
2205    /// `Nearest`.
2206    ///
2207    /// If you want to use a rounding mode other than `Nearest`, consider using
2208    /// [`Float::cot_with_period_rational_prec_round`] instead.
2209    ///
2210    /// # Panics
2211    /// Panics if `prec` is zero.
2212    ///
2213    /// # Examples
2214    /// ```
2215    /// use malachite_base::num::basic::traits::One;
2216    /// use malachite_float::Float;
2217    /// use malachite_q::Rational;
2218    /// use std::cmp::Ordering::*;
2219    ///
2220    /// let (t, o) = Float::cot_with_period_rational_prec(Rational::ONE, 7, 10);
2221    /// assert_eq!(t.to_string(), "0.79785");
2222    /// assert_eq!(o, Greater);
2223    ///
2224    /// // an eighth of a turn is exactly 1
2225    /// let (t, o) = Float::cot_with_period_rational_prec(Rational::ONE, 8, 10);
2226    /// assert_eq!(t.to_string(), "1.0000");
2227    /// assert_eq!(o, Equal);
2228    /// ```
2229    #[inline]
2230    #[allow(clippy::needless_pass_by_value)]
2231    pub fn cot_with_period_rational_prec(x: Rational, u: u64, prec: u64) -> (Self, Ordering) {
2232        Self::cot_with_period_rational_prec_round_ref(&x, u, prec, Nearest)
2233    }
2234
2235    /// Computes $\cot(2\pi x/u)$, the cotangent of a [`Rational`] measured in $u$ths of a turn,
2236    /// rounding the result to the nearest value of the specified precision, and returning the
2237    /// result as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also
2238    /// returned, indicating whether the rounded cotangent is less than, equal to, or greater than
2239    /// the exact cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this
2240    /// function returns a `NaN` it also returns `Equal`.
2241    ///
2242    /// See [`Float::cot_with_period_rational_prec`] and
2243    /// [`Float::cot_with_period_rational_prec_round`]; this function behaves the same way.
2244    ///
2245    /// # Panics
2246    /// Panics if `prec` is zero.
2247    ///
2248    /// # Examples
2249    /// ```
2250    /// use malachite_base::num::basic::traits::One;
2251    /// use malachite_float::Float;
2252    /// use malachite_q::Rational;
2253    /// use std::cmp::Ordering::*;
2254    ///
2255    /// let (t, o) = Float::cot_with_period_rational_prec_ref(&Rational::ONE, 7, 10);
2256    /// assert_eq!(t.to_string(), "0.79785");
2257    /// assert_eq!(o, Greater);
2258    /// ```
2259    #[inline]
2260    pub fn cot_with_period_rational_prec_ref(x: &Rational, u: u64, prec: u64) -> (Self, Ordering) {
2261        Self::cot_with_period_rational_prec_round_ref(x, u, prec, Nearest)
2262    }
2263
2264    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2265    /// result to the specified precision and with the specified rounding mode. The [`Float`] is
2266    /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded cotangent
2267    /// is less than, equal to, or greater than the exact cotangent. Although `NaN`s are not
2268    /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2269    ///
2270    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_prec_round`] for
2271    /// the error bounds, the special and closed-form cases (integers are poles and give
2272    /// $\pm\infty$, with the sign of $x$ at an even integer and the opposite at an odd one;
2273    /// half-integers give $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give
2274    /// $\pm\sqrt3$; and multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow,
2275    /// underflow, and the complexity, with $u = 2$.
2276    ///
2277    /// # Panics
2278    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2279    /// with the given precision.
2280    ///
2281    /// # Examples
2282    /// ```
2283    /// use malachite_base::num::basic::traits::One;
2284    /// use malachite_base::rounding_modes::RoundingMode::*;
2285    /// use malachite_float::Float;
2286    /// use std::cmp::Ordering::*;
2287    ///
2288    /// let (t, o) = Float::from(0.1f64).cot_pi_prec_round(10, Floor);
2289    /// assert_eq!(t.to_string(), "3.0742");
2290    /// assert_eq!(o, Less);
2291    ///
2292    /// let (t, o) = Float::from(0.1f64).cot_pi_prec_round(10, Ceiling);
2293    /// assert_eq!(t.to_string(), "3.0781");
2294    /// assert_eq!(o, Greater);
2295    ///
2296    /// // an integer is a pole
2297    /// let (t, o) = Float::ONE.cot_pi_prec_round(10, Exact);
2298    /// assert_eq!(t.to_string(), "-Infinity");
2299    /// assert_eq!(o, Equal);
2300    /// ```
2301    #[inline]
2302    pub fn cot_pi_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
2303        self.cot_with_period_prec_round(2, prec, rm)
2304    }
2305
2306    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2307    /// result to the specified precision and with the specified rounding mode. The [`Float`] is
2308    /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
2309    /// cotangent is less than, equal to, or greater than the exact cotangent. Although `NaN`s are
2310    /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
2311    /// `Equal`.
2312    ///
2313    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_prec_round_ref`]
2314    /// for the error bounds, the special and closed-form cases (integers are poles and give
2315    /// $\pm\infty$, with the sign of $x$ at an even integer and the opposite at an odd one;
2316    /// half-integers give $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give
2317    /// $\pm\sqrt3$; and multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow,
2318    /// underflow, and the complexity, with $u = 2$.
2319    ///
2320    /// # Panics
2321    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2322    /// with the given precision.
2323    ///
2324    /// # Examples
2325    /// ```
2326    /// use malachite_base::num::basic::traits::One;
2327    /// use malachite_base::rounding_modes::RoundingMode::*;
2328    /// use malachite_float::Float;
2329    /// use std::cmp::Ordering::*;
2330    ///
2331    /// let (t, o) = (Float::from(0.1f64)).cot_pi_prec_round_ref(10, Floor);
2332    /// assert_eq!(t.to_string(), "3.0742");
2333    /// assert_eq!(o, Less);
2334    ///
2335    /// let (t, o) = (Float::from(0.1f64)).cot_pi_prec_round_ref(10, Ceiling);
2336    /// assert_eq!(t.to_string(), "3.0781");
2337    /// assert_eq!(o, Greater);
2338    ///
2339    /// // an integer is a pole
2340    /// let (t, o) = (&Float::ONE).cot_pi_prec_round_ref(10, Exact);
2341    /// assert_eq!(t.to_string(), "-Infinity");
2342    /// assert_eq!(o, Equal);
2343    /// ```
2344    #[inline]
2345    pub fn cot_pi_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
2346        self.cot_with_period_prec_round_ref(2, prec, rm)
2347    }
2348
2349    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2350    /// result to the nearest value of the specified precision. The [`Float`] is taken by value. An
2351    /// [`Ordering`] is also returned, indicating whether the rounded cotangent is less than, equal
2352    /// to, or greater than the exact cotangent. Although `NaN`s are not comparable to any
2353    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2354    ///
2355    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_prec`] for the
2356    /// error bounds, the special and closed-form cases (integers are poles and give $\pm\infty$,
2357    /// with the sign of $x$ at an even integer and the opposite at an odd one; half-integers give
2358    /// $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give $\pm\sqrt3$; and
2359    /// multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow, underflow, and the
2360    /// complexity, with $u = 2$.
2361    ///
2362    /// # Panics
2363    /// Panics if `prec` is zero.
2364    ///
2365    /// # Examples
2366    /// ```
2367    /// use malachite_float::Float;
2368    /// use std::cmp::Ordering::*;
2369    ///
2370    /// let (t, o) = Float::from(0.1f64).cot_pi_prec(10);
2371    /// assert_eq!(t.to_string(), "3.0781");
2372    /// assert_eq!(o, Greater);
2373    ///
2374    /// let (t, o) = Float::from(0.1f64).cot_pi_prec(53);
2375    /// assert_eq!(t.to_string(), "3.0776835371752531");
2376    /// assert_eq!(o, Less);
2377    /// ```
2378    #[inline]
2379    pub fn cot_pi_prec(self, prec: u64) -> (Self, Ordering) {
2380        self.cot_with_period_prec(2, prec)
2381    }
2382
2383    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2384    /// result to the nearest value of the specified precision. The [`Float`] is taken by reference.
2385    /// An [`Ordering`] is also returned, indicating whether the rounded cotangent is less than,
2386    /// equal to, or greater than the exact cotangent. Although `NaN`s are not comparable to any
2387    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
2388    ///
2389    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_prec_ref`] for
2390    /// the error bounds, the special and closed-form cases (integers are poles and give
2391    /// $\pm\infty$, with the sign of $x$ at an even integer and the opposite at an odd one;
2392    /// half-integers give $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give
2393    /// $\pm\sqrt3$; and multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow,
2394    /// underflow, and the complexity, with $u = 2$.
2395    ///
2396    /// # Panics
2397    /// Panics if `prec` is zero.
2398    ///
2399    /// # Examples
2400    /// ```
2401    /// use malachite_float::Float;
2402    /// use std::cmp::Ordering::*;
2403    ///
2404    /// let (t, o) = (Float::from(0.1f64)).cot_pi_prec_ref(10);
2405    /// assert_eq!(t.to_string(), "3.0781");
2406    /// assert_eq!(o, Greater);
2407    ///
2408    /// let (t, o) = (Float::from(0.1f64)).cot_pi_prec_ref(53);
2409    /// assert_eq!(t.to_string(), "3.0776835371752531");
2410    /// assert_eq!(o, Less);
2411    /// ```
2412    #[inline]
2413    pub fn cot_pi_prec_ref(&self, prec: u64) -> (Self, Ordering) {
2414        self.cot_with_period_prec_ref(2, prec)
2415    }
2416
2417    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2418    /// result with the specified rounding mode. The precision of the output is the precision of the
2419    /// input. The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether
2420    /// the rounded cotangent is less than, equal to, or greater than the exact cotangent. Although
2421    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
2422    /// returns `Equal`.
2423    ///
2424    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_round`] for the
2425    /// error bounds, the special and closed-form cases (integers are poles and give $\pm\infty$,
2426    /// with the sign of $x$ at an even integer and the opposite at an odd one; half-integers give
2427    /// $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give $\pm\sqrt3$; and
2428    /// multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow, underflow, and the
2429    /// complexity, with $u = 2$.
2430    ///
2431    /// # Panics
2432    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
2433    /// precision.
2434    ///
2435    /// # Examples
2436    /// ```
2437    /// use malachite_base::rounding_modes::RoundingMode::*;
2438    /// use malachite_float::Float;
2439    /// use std::cmp::Ordering::*;
2440    ///
2441    /// let (t, o) = Float::from(0.1f64).cot_pi_round(Floor);
2442    /// assert_eq!(t.to_string(), "3.0776835371752531");
2443    /// assert_eq!(o, Less);
2444    ///
2445    /// let (t, o) = Float::from(0.1f64).cot_pi_round(Nearest);
2446    /// assert_eq!(t.to_string(), "3.0776835371752531");
2447    /// assert_eq!(o, Less);
2448    /// ```
2449    #[inline]
2450    pub fn cot_pi_round(self, rm: RoundingMode) -> (Self, Ordering) {
2451        self.cot_with_period_round(2, rm)
2452    }
2453
2454    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2455    /// result with the specified rounding mode. The precision of the output is the precision of the
2456    /// input. The [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating
2457    /// whether the rounded cotangent is less than, equal to, or greater than the exact cotangent.
2458    /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
2459    /// it also returns `Equal`.
2460    ///
2461    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_round_ref`] for
2462    /// the error bounds, the special and closed-form cases (integers are poles and give
2463    /// $\pm\infty$, with the sign of $x$ at an even integer and the opposite at an odd one;
2464    /// half-integers give $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give
2465    /// $\pm\sqrt3$; and multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow,
2466    /// underflow, and the complexity, with $u = 2$.
2467    ///
2468    /// # Panics
2469    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
2470    /// precision.
2471    ///
2472    /// # Examples
2473    /// ```
2474    /// use malachite_base::rounding_modes::RoundingMode::*;
2475    /// use malachite_float::Float;
2476    /// use std::cmp::Ordering::*;
2477    ///
2478    /// let (t, o) = (Float::from(0.1f64)).cot_pi_round_ref(Floor);
2479    /// assert_eq!(t.to_string(), "3.0776835371752531");
2480    /// assert_eq!(o, Less);
2481    ///
2482    /// let (t, o) = (Float::from(0.1f64)).cot_pi_round_ref(Nearest);
2483    /// assert_eq!(t.to_string(), "3.0776835371752531");
2484    /// assert_eq!(o, Less);
2485    /// ```
2486    #[inline]
2487    pub fn cot_pi_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
2488        self.cot_with_period_round_ref(2, rm)
2489    }
2490
2491    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2492    /// result to the precision of the input and to the nearest [`Float`]. The [`Float`] is taken by
2493    /// value.
2494    ///
2495    /// If the cotangent is equidistant from two [`Float`]s with the precision of the input, the
2496    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2497    /// description of the `Nearest` rounding mode.
2498    ///
2499    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period`] for the error
2500    /// bounds, the special and closed-form cases (integers are poles and give $\pm\infty$, with the
2501    /// sign of $x$ at an even integer and the opposite at an odd one; half-integers give $\pm0.0$;
2502    /// odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give $\pm\sqrt3$; and multiples
2503    /// of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow, underflow, and the complexity,
2504    /// with $u = 2$.
2505    ///
2506    /// If you want to use a rounding mode other than `Nearest`, consider using
2507    /// [`Float::cot_pi_round`] instead. If you want to specify an output precision, consider using
2508    /// [`Float::cot_pi_prec`]. If you want both of these things, consider using
2509    /// [`Float::cot_pi_prec_round`].
2510    ///
2511    /// # Examples
2512    /// ```
2513    /// use malachite_float::Float;
2514    ///
2515    /// let t = Float::from(0.1f64).cot_pi();
2516    /// assert_eq!(t.to_string(), "3.0776835371752531");
2517    ///
2518    /// // a half-integer is exactly 0
2519    /// assert_eq!(Float::from(0.5f64).cot_pi().to_string(), "0.0");
2520    /// ```
2521    #[inline]
2522    pub fn cot_pi(self) -> Self {
2523        let prec = self.significant_bits();
2524        self.cot_pi_prec(prec).0
2525    }
2526
2527    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2528    /// result to the precision of the input and to the nearest [`Float`]. The [`Float`] is taken by
2529    /// reference.
2530    ///
2531    /// If the cotangent is equidistant from two [`Float`]s with the precision of the input, the
2532    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2533    /// description of the `Nearest` rounding mode.
2534    ///
2535    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period`] for the error
2536    /// bounds, the special and closed-form cases (integers are poles and give $\pm\infty$, with the
2537    /// sign of $x$ at an even integer and the opposite at an odd one; half-integers give $\pm0.0$;
2538    /// odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give $\pm\sqrt3$; and multiples
2539    /// of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow, underflow, and the complexity,
2540    /// with $u = 2$.
2541    ///
2542    /// If you want to use a rounding mode other than `Nearest`, consider using
2543    /// [`Float::cot_pi_round_ref`] instead. If you want to specify an output precision, consider
2544    /// using [`Float::cot_pi_prec_ref`]. If you want both of these things, consider using
2545    /// [`Float::cot_pi_prec_round_ref`].
2546    ///
2547    /// # Examples
2548    /// ```
2549    /// use malachite_float::Float;
2550    ///
2551    /// let t = (&Float::from(0.1f64)).cot_pi_ref();
2552    /// assert_eq!(t.to_string(), "3.0776835371752531");
2553    /// ```
2554    #[inline]
2555    pub fn cot_pi_ref(&self) -> Self {
2556        self.cot_pi_prec_ref(self.significant_bits()).0
2557    }
2558
2559    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2560    /// result to the specified precision and with the specified rounding mode. The [`Float`] is
2561    /// replaced by the result, and an [`Ordering`] is returned, indicating whether the rounded
2562    /// cotangent is less than, equal to, or greater than the exact cotangent. Although `NaN`s are
2563    /// not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
2564    /// `Equal`.
2565    ///
2566    /// This is `cot_with_period` with a period of 2: see
2567    /// [`Float::cot_with_period_prec_round_assign`] for the error bounds, the special and
2568    /// closed-form cases (integers are poles and give $\pm\infty$, with the sign of $x$ at an even
2569    /// integer and the opposite at an odd one; half-integers give $\pm0.0$; odd multiples of $1/4$
2570    /// give $\pm1$; odd multiples of $1/6$ give $\pm\sqrt3$; and multiples of $1/3$ that are not
2571    /// integers give $\pm\sqrt3/3$), overflow, underflow, and the complexity, with $u = 2$.
2572    ///
2573    /// # Panics
2574    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2575    /// with the given precision.
2576    ///
2577    /// # Examples
2578    /// ```
2579    /// use malachite_base::rounding_modes::RoundingMode::*;
2580    /// use malachite_float::Float;
2581    /// use std::cmp::Ordering::*;
2582    ///
2583    /// let mut x = Float::from(0.1f64);
2584    /// assert_eq!(x.cot_pi_prec_round_assign(10, Floor), Less);
2585    /// assert_eq!(x.to_string(), "3.0742");
2586    ///
2587    /// let mut x = Float::from(0.1f64);
2588    /// assert_eq!(x.cot_pi_prec_round_assign(10, Ceiling), Greater);
2589    /// assert_eq!(x.to_string(), "3.0781");
2590    /// ```
2591    #[inline]
2592    pub fn cot_pi_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
2593        self.cot_with_period_prec_round_assign(2, prec, rm)
2594    }
2595
2596    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2597    /// result to the nearest value of the specified precision. The [`Float`] is replaced by the
2598    /// result, and an [`Ordering`] is returned, indicating whether the rounded cotangent is less
2599    /// than, equal to, or greater than the exact cotangent. Although `NaN`s are not comparable to
2600    /// any [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
2601    ///
2602    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_prec_assign`] for
2603    /// the error bounds, the special and closed-form cases (integers are poles and give
2604    /// $\pm\infty$, with the sign of $x$ at an even integer and the opposite at an odd one;
2605    /// half-integers give $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give
2606    /// $\pm\sqrt3$; and multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow,
2607    /// underflow, and the complexity, with $u = 2$.
2608    ///
2609    /// # Panics
2610    /// Panics if `prec` is zero.
2611    ///
2612    /// # Examples
2613    /// ```
2614    /// use malachite_float::Float;
2615    /// use std::cmp::Ordering::*;
2616    ///
2617    /// let mut x = Float::from(0.1f64);
2618    /// assert_eq!(x.cot_pi_prec_assign(10), Greater);
2619    /// assert_eq!(x.to_string(), "3.0781");
2620    /// ```
2621    #[inline]
2622    pub fn cot_pi_prec_assign(&mut self, prec: u64) -> Ordering {
2623        self.cot_with_period_prec_assign(2, prec)
2624    }
2625
2626    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2627    /// result with the specified rounding mode. The precision of the output is the precision of the
2628    /// input. The [`Float`] is replaced by the result, and an [`Ordering`] is returned, indicating
2629    /// whether the rounded cotangent is less than, equal to, or greater than the exact cotangent.
2630    /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets a `NaN` it
2631    /// also returns `Equal`.
2632    ///
2633    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period_round_assign`]
2634    /// for the error bounds, the special and closed-form cases (integers are poles and give
2635    /// $\pm\infty$, with the sign of $x$ at an even integer and the opposite at an odd one;
2636    /// half-integers give $\pm0.0$; odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give
2637    /// $\pm\sqrt3$; and multiples of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow,
2638    /// underflow, and the complexity, with $u = 2$.
2639    ///
2640    /// # Panics
2641    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
2642    /// precision.
2643    ///
2644    /// # Examples
2645    /// ```
2646    /// use malachite_base::rounding_modes::RoundingMode::*;
2647    /// use malachite_float::Float;
2648    /// use std::cmp::Ordering::*;
2649    ///
2650    /// let mut x = Float::from(0.1f64);
2651    /// assert_eq!(x.cot_pi_round_assign(Floor), Less);
2652    /// assert_eq!(x.to_string(), "3.0776835371752531");
2653    /// ```
2654    #[inline]
2655    pub fn cot_pi_round_assign(&mut self, rm: RoundingMode) -> Ordering {
2656        self.cot_with_period_round_assign(2, rm)
2657    }
2658
2659    /// Computes $\cot(\pi x)$, the cotangent of a [`Float`] measured in half-turns, rounding the
2660    /// result to the precision of the input and to the nearest [`Float`]. The [`Float`] is replaced
2661    /// by the result.
2662    ///
2663    /// If the cotangent is equidistant from two [`Float`]s with the precision of the input, the
2664    /// [`Float`] with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a
2665    /// description of the `Nearest` rounding mode.
2666    ///
2667    /// This is `cot_with_period` with a period of 2: see [`Float::cot_with_period`] for the error
2668    /// bounds, the special and closed-form cases (integers are poles and give $\pm\infty$, with the
2669    /// sign of $x$ at an even integer and the opposite at an odd one; half-integers give $\pm0.0$;
2670    /// odd multiples of $1/4$ give $\pm1$; odd multiples of $1/6$ give $\pm\sqrt3$; and multiples
2671    /// of $1/3$ that are not integers give $\pm\sqrt3/3$), overflow, underflow, and the complexity,
2672    /// with $u = 2$.
2673    ///
2674    /// If you want to use a rounding mode other than `Nearest`, consider using
2675    /// [`Float::cot_pi_round_assign`] instead. If you want to specify an output precision, consider
2676    /// using [`Float::cot_pi_prec_assign`]. If you want both of these things, consider using
2677    /// [`Float::cot_pi_prec_round_assign`].
2678    ///
2679    /// # Examples
2680    /// ```
2681    /// use malachite_float::Float;
2682    ///
2683    /// let mut x = Float::from(0.1f64);
2684    /// x.cot_pi_assign();
2685    /// assert_eq!(x.to_string(), "3.0776835371752531");
2686    /// ```
2687    #[inline]
2688    pub fn cot_pi_assign(&mut self) {
2689        let prec = self.significant_bits();
2690        self.cot_pi_prec_assign(prec);
2691    }
2692
2693    /// Computes $\cot(\pi x)$, the cotangent of a [`Rational`] measured in half-turns, rounding the
2694    /// result to the specified precision and with the specified rounding mode and returning the
2695    /// result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned,
2696    /// indicating whether the rounded cotangent is less than, equal to, or greater than the exact
2697    /// cotangent.
2698    ///
2699    /// This is `cot_with_period_rational` with a period of 2: see
2700    /// [`Float::cot_with_period_rational_prec_round`] for the error bounds, the special and
2701    /// closed-form cases, overflow, and the complexity, with $u = 2$.
2702    ///
2703    /// # Panics
2704    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2705    /// with the given precision.
2706    ///
2707    /// # Examples
2708    /// ```
2709    /// use malachite_base::rounding_modes::RoundingMode::*;
2710    /// use malachite_float::Float;
2711    /// use malachite_q::Rational;
2712    /// use std::cmp::Ordering::*;
2713    ///
2714    /// let (t, o) = Float::cot_pi_rational_prec_round(Rational::from_unsigneds(1u8, 7), 10, Floor);
2715    /// assert_eq!(t.to_string(), "2.0742");
2716    /// assert_eq!(o, Less);
2717    ///
2718    /// // a quarter of a half-turn is exactly 1
2719    /// let (t, o) = Float::cot_pi_rational_prec_round(Rational::from_unsigneds(1u8, 4), 10, Exact);
2720    /// assert_eq!(t.to_string(), "1.0000");
2721    /// assert_eq!(o, Equal);
2722    /// ```
2723    #[inline]
2724    #[allow(clippy::needless_pass_by_value)]
2725    pub fn cot_pi_rational_prec_round(
2726        x: Rational,
2727        prec: u64,
2728        rm: RoundingMode,
2729    ) -> (Self, Ordering) {
2730        Self::cot_with_period_rational_prec_round_ref(&x, 2, prec, rm)
2731    }
2732
2733    /// Computes $\cot(\pi x)$, the cotangent of a [`Rational`] measured in half-turns, rounding the
2734    /// result to the specified precision and with the specified rounding mode and returning the
2735    /// result as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also
2736    /// returned, indicating whether the rounded cotangent is less than, equal to, or greater than
2737    /// the exact cotangent.
2738    ///
2739    /// This is `cot_with_period_rational` with a period of 2: see
2740    /// [`Float::cot_with_period_rational_prec_round_ref`] for the error bounds, the special and
2741    /// closed-form cases, overflow, and the complexity, with $u = 2$.
2742    ///
2743    /// # Panics
2744    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
2745    /// with the given precision.
2746    ///
2747    /// # Examples
2748    /// ```
2749    /// use malachite_base::rounding_modes::RoundingMode::*;
2750    /// use malachite_float::Float;
2751    /// use malachite_q::Rational;
2752    /// use std::cmp::Ordering::*;
2753    ///
2754    /// let (t, o) =
2755    ///     Float::cot_pi_rational_prec_round_ref(&Rational::from_unsigneds(1u8, 7), 10, Ceiling);
2756    /// assert_eq!(t.to_string(), "2.0781");
2757    /// assert_eq!(o, Greater);
2758    /// ```
2759    #[inline]
2760    pub fn cot_pi_rational_prec_round_ref(
2761        x: &Rational,
2762        prec: u64,
2763        rm: RoundingMode,
2764    ) -> (Self, Ordering) {
2765        Self::cot_with_period_rational_prec_round_ref(x, 2, prec, rm)
2766    }
2767
2768    /// Computes $\cot(\pi x)$, the cotangent of a [`Rational`] measured in half-turns, rounding the
2769    /// result to the nearest value of the specified precision and returning the result as a
2770    /// [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned, indicating
2771    /// whether the rounded cotangent is less than, equal to, or greater than the exact cotangent.
2772    ///
2773    /// This is `cot_with_period_rational` with a period of 2: see
2774    /// [`Float::cot_with_period_rational_prec`] for the error bounds, the special and closed-form
2775    /// cases, overflow, and the complexity, with $u = 2$.
2776    ///
2777    /// # Panics
2778    /// Panics if `prec` is zero.
2779    ///
2780    /// # Examples
2781    /// ```
2782    /// use malachite_float::Float;
2783    /// use malachite_q::Rational;
2784    /// use std::cmp::Ordering::*;
2785    ///
2786    /// let (t, o) = Float::cot_pi_rational_prec(Rational::from_unsigneds(1u8, 7), 53);
2787    /// assert_eq!(t.to_string(), "2.0765213965723364");
2788    /// assert_eq!(o, Less);
2789    /// ```
2790    #[inline]
2791    #[allow(clippy::needless_pass_by_value)]
2792    pub fn cot_pi_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
2793        Self::cot_with_period_rational_prec_ref(&x, 2, prec)
2794    }
2795
2796    /// Computes $\cot(\pi x)$, the cotangent of a [`Rational`] measured in half-turns, rounding the
2797    /// result to the nearest value of the specified precision and returning the result as a
2798    /// [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also returned,
2799    /// indicating whether the rounded cotangent is less than, equal to, or greater than the exact
2800    /// cotangent.
2801    ///
2802    /// This is `cot_with_period_rational` with a period of 2: see
2803    /// [`Float::cot_with_period_rational_prec_ref`] for the error bounds, the special and
2804    /// closed-form cases, overflow, and the complexity, with $u = 2$.
2805    ///
2806    /// # Panics
2807    /// Panics if `prec` is zero.
2808    ///
2809    /// # Examples
2810    /// ```
2811    /// use malachite_float::Float;
2812    /// use malachite_q::Rational;
2813    /// use std::cmp::Ordering::*;
2814    ///
2815    /// let (t, o) = Float::cot_pi_rational_prec_ref(&Rational::from_unsigneds(1u8, 7), 53);
2816    /// assert_eq!(t.to_string(), "2.0765213965723364");
2817    /// assert_eq!(o, Less);
2818    /// ```
2819    #[inline]
2820    pub fn cot_pi_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
2821        Self::cot_with_period_rational_prec_ref(x, 2, prec)
2822    }
2823}
2824
2825impl Cot for Float {
2826    type Output = Self;
2827
2828    /// Computes $\cot x$, the cotangent of a [`Float`], taking it by value.
2829    ///
2830    /// If the output has a precision, it is the precision of the input. If the cotangent is
2831    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
2832    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
2833    /// rounding mode.
2834    ///
2835    /// $$
2836    /// f(x) = \cot x+\varepsilon.
2837    /// $$
2838    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
2839    /// - If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$, where $p$ is
2840    ///   the precision of the input.
2841    ///
2842    /// Special cases:
2843    /// - $f(\text{NaN})=\text{NaN}$
2844    /// - $f(\pm\infty)=\text{NaN}$
2845    /// - $f(\pm0.0)=\pm\infty$
2846    ///
2847    /// See the [`Float::cot_round`] documentation for information on overflow.
2848    ///
2849    /// If you want to use a rounding mode other than `Nearest`, consider using [`Float::cot_round`]
2850    /// instead. If you want to specify the output precision, consider using [`Float::cot_prec`]. If
2851    /// you want both of these things, consider using [`Float::cot_prec_round`].
2852    ///
2853    /// # Worst-case complexity
2854    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
2855    ///
2856    /// $M(n, e) = O((n+e) \log (n+e))$
2857    ///
2858    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
2859    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
2860    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
2861    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
2862    /// e$ bits. Unlike most functions, `cot` therefore gets slower as the magnitude of its input
2863    /// grows, not just as the precision does.
2864    ///
2865    /// # Examples
2866    /// ```
2867    /// use malachite_base::num::arithmetic::traits::Cot;
2868    /// use malachite_base::num::basic::traits::*;
2869    /// use malachite_float::Float;
2870    ///
2871    /// assert!(Float::NAN.cot().is_nan());
2872    /// assert!(Float::INFINITY.cot().is_nan());
2873    /// assert!(Float::NEGATIVE_INFINITY.cot().is_nan());
2874    /// assert_eq!(Float::ZERO.cot().to_string(), "Infinity");
2875    /// assert_eq!(Float::NEGATIVE_ZERO.cot().to_string(), "-Infinity");
2876    /// assert_eq!(
2877    ///     Float::from_unsigned_prec(1u32, 100).0.cot().to_string(),
2878    ///     "0.64209261593433070300641998659417"
2879    /// );
2880    /// assert_eq!(
2881    ///     Float::from_unsigned_prec(100u32, 100).0.cot().to_string(),
2882    ///     "-1.7029569194264692160987314595571"
2883    /// );
2884    /// ```
2885    #[inline]
2886    fn cot(self) -> Self {
2887        let prec = self.significant_bits();
2888        self.cot_prec_round(prec, Nearest).0
2889    }
2890}
2891
2892impl Cot for &Float {
2893    type Output = Float;
2894
2895    /// Computes $\cot x$, the cotangent of a [`Float`], taking it by reference.
2896    ///
2897    /// If the output has a precision, it is the precision of the input. If the cotangent is
2898    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
2899    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
2900    /// rounding mode.
2901    ///
2902    /// $$
2903    /// f(x) = \cot x+\varepsilon.
2904    /// $$
2905    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
2906    /// - If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$, where $p$ is
2907    ///   the precision of the input.
2908    ///
2909    /// Special cases:
2910    /// - $f(\text{NaN})=\text{NaN}$
2911    /// - $f(\pm\infty)=\text{NaN}$
2912    /// - $f(\pm0.0)=\pm\infty$
2913    ///
2914    /// See the [`Float::cot_round`] documentation for information on overflow.
2915    ///
2916    /// If you want to use a rounding mode other than `Nearest`, consider using
2917    /// [`Float::cot_round_ref`] instead. If you want to specify the output precision, consider
2918    /// using [`Float::cot_prec_ref`]. If you want both of these things, consider using
2919    /// [`Float::cot_prec_round_ref`].
2920    ///
2921    /// # Worst-case complexity
2922    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
2923    ///
2924    /// $M(n, e) = O((n+e) \log (n+e))$
2925    ///
2926    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
2927    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
2928    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
2929    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
2930    /// e$ bits. Unlike most functions, `cot` therefore gets slower as the magnitude of its input
2931    /// grows, not just as the precision does.
2932    ///
2933    /// # Examples
2934    /// ```
2935    /// use malachite_base::num::arithmetic::traits::Cot;
2936    /// use malachite_base::num::basic::traits::*;
2937    /// use malachite_float::Float;
2938    ///
2939    /// assert!(Float::NAN.cot().is_nan());
2940    /// assert!(Float::INFINITY.cot().is_nan());
2941    /// assert!(Float::NEGATIVE_INFINITY.cot().is_nan());
2942    /// assert_eq!(Float::ZERO.cot().to_string(), "Infinity");
2943    /// assert_eq!(Float::NEGATIVE_ZERO.cot().to_string(), "-Infinity");
2944    /// assert_eq!(
2945    ///     (&Float::from_unsigned_prec(1u32, 100).0).cot().to_string(),
2946    ///     "0.64209261593433070300641998659417"
2947    /// );
2948    /// assert_eq!(
2949    ///     (&Float::from_unsigned_prec(100u32, 100).0)
2950    ///         .cot()
2951    ///         .to_string(),
2952    ///     "-1.7029569194264692160987314595571"
2953    /// );
2954    /// ```
2955    #[inline]
2956    fn cot(self) -> Float {
2957        self.cot_prec_round_ref(self.significant_bits(), Nearest).0
2958    }
2959}
2960
2961impl CotAssign for Float {
2962    /// Computes $\cot x$, the cotangent of a [`Float`], in place.
2963    ///
2964    /// If the output has a precision, it is the precision of the input. If the cotangent is
2965    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
2966    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
2967    /// rounding mode.
2968    ///
2969    /// $$
2970    /// x \gets \cot x+\varepsilon.
2971    /// $$
2972    /// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
2973    /// - If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$, where $p$ is
2974    ///   the precision of the input.
2975    ///
2976    /// See the [`Float::cot`] documentation for information on special cases and overflow.
2977    ///
2978    /// If you want to use a rounding mode other than `Nearest`, consider using
2979    /// [`Float::cot_round_assign`] instead. If you want to specify the output precision, consider
2980    /// using [`Float::cot_prec_assign`]. If you want both of these things, consider using
2981    /// [`Float::cot_prec_round_assign`].
2982    ///
2983    /// # Worst-case complexity
2984    /// $T(n, e) = O(n (\log n)^3 \log\log n + (n+e) (\log (n+e))^2 \log\log (n+e))$
2985    ///
2986    /// $M(n, e) = O((n+e) \log (n+e))$
2987    ///
2988    /// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $e$ is
2989    /// the exponent of `self` (0 if `self` has no exponent or a negative one): the Taylor series at
2990    /// working precision $n$, summed by binary splitting for large $n$, costs the first term, and
2991    /// for $|x| \geq 4$ the argument is reduced modulo $2\pi$, which requires $\pi$ to about $n +
2992    /// e$ bits. Unlike most functions, `cot` therefore gets slower as the magnitude of its input
2993    /// grows, not just as the precision does.
2994    ///
2995    /// # Examples
2996    /// ```
2997    /// use malachite_base::num::arithmetic::traits::CotAssign;
2998    /// use malachite_base::num::basic::traits::*;
2999    /// use malachite_float::Float;
3000    ///
3001    /// let mut x = Float::NAN;
3002    /// x.cot_assign();
3003    /// assert!(x.is_nan());
3004    ///
3005    /// let mut x = Float::INFINITY;
3006    /// x.cot_assign();
3007    /// assert!(x.is_nan());
3008    ///
3009    /// let mut x = Float::NEGATIVE_INFINITY;
3010    /// x.cot_assign();
3011    /// assert!(x.is_nan());
3012    ///
3013    /// let mut x = Float::ZERO;
3014    /// x.cot_assign();
3015    /// assert_eq!(x.to_string(), "Infinity");
3016    ///
3017    /// let mut x = Float::NEGATIVE_ZERO;
3018    /// x.cot_assign();
3019    /// assert_eq!(x.to_string(), "-Infinity");
3020    ///
3021    /// let mut x = Float::from_unsigned_prec(1u32, 100).0;
3022    /// x.cot_assign();
3023    /// assert_eq!(x.to_string(), "0.64209261593433070300641998659417");
3024    ///
3025    /// let mut x = Float::from_unsigned_prec(100u32, 100).0;
3026    /// x.cot_assign();
3027    /// assert_eq!(x.to_string(), "-1.7029569194264692160987314595571");
3028    /// ```
3029    #[inline]
3030    fn cot_assign(&mut self) {
3031        let prec = self.significant_bits();
3032        self.cot_prec_round_assign(prec, Nearest);
3033    }
3034}
3035
3036/// Computes $\cot x$, the cotangent of a primitive float, correctly rounded. Neither the standard
3037/// library nor `libm` provides a cotangent.
3038///
3039/// $$
3040/// f(x) = \cot x+\varepsilon.
3041/// $$
3042/// - If $x$ is not finite, $\varepsilon$ may be ignored or assumed to be 0.
3043/// - If $x$ is finite, then $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$, where $p$ is the
3044///   precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
3045///
3046/// Special cases:
3047/// - $f(\text{NaN})=\text{NaN}$
3048/// - $f(\pm\infty)=\text{NaN}$
3049/// - $f(\pm0.0)=\pm\infty$
3050///
3051/// Overflow is possible: the cotangent of a tiny $x$ is close to $1/x$, so an $x$ with $|x|$ below
3052/// about $2^{-128}$ has a cotangent beyond the largest [`f32`], and one below about $2^{-1024}$
3053/// beyond the largest [`f64`]; the result is then $\pm\infty$. No [`f32`] or [`f64`] is close
3054/// enough to a nonzero multiple of $\pi$ for its cotangent to overflow that way, nor close enough
3055/// to an odd multiple of $\pi/2$ for it to underflow: the floats are spaced far more widely there
3056/// than either would take.
3057///
3058/// # Worst-case complexity
3059/// Constant time and additional memory.
3060///
3061/// # Examples
3062/// ```
3063/// use malachite_base::num::basic::traits::NegativeInfinity;
3064/// use malachite_base::num::float::NiceFloat;
3065/// use malachite_float::float::arithmetic::cot::primitive_float_cot;
3066///
3067/// assert!(primitive_float_cot(f32::NAN).is_nan());
3068/// assert!(primitive_float_cot(f32::INFINITY).is_nan());
3069/// assert!(primitive_float_cot(f32::NEGATIVE_INFINITY).is_nan());
3070/// assert_eq!(
3071///     NiceFloat(primitive_float_cot(0.0f32)),
3072///     NiceFloat(f32::INFINITY)
3073/// );
3074/// assert_eq!(
3075///     NiceFloat(primitive_float_cot(-0.0f32)),
3076///     NiceFloat(f32::NEGATIVE_INFINITY)
3077/// );
3078/// assert_eq!(
3079///     NiceFloat(primitive_float_cot(1.0f32)),
3080///     NiceFloat(0.64209265)
3081/// );
3082/// assert_eq!(
3083///     NiceFloat(primitive_float_cot(1.0f64)),
3084///     NiceFloat(0.6420926159343308)
3085/// );
3086/// ```
3087#[inline]
3088#[allow(clippy::type_repetition_in_bounds)]
3089pub fn primitive_float_cot<T: PrimitiveFloat>(x: T) -> T
3090where
3091    Float: From<T> + PartialOrd<T>,
3092    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
3093{
3094    emulate_float_to_float_fn(Float::cot_prec, x)
3095}
3096
3097/// Computes $\cot x$, the cotangent of a [`Rational`], returning the result as a primitive float.
3098///
3099/// $$
3100/// f(x) = \cot x+\varepsilon,
3101/// $$
3102/// where $|\varepsilon| < 2^{\lfloor\log_2 |\cot x|\rfloor-p}$, and $p$ is the precision of the
3103/// output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
3104///
3105/// Special cases:
3106/// - $f(0)=\infty$
3107///
3108/// Both ends are possible: a [`Rational`] within about $2^{-129}$ of a nonzero multiple of $\pi$
3109/// has a cotangent beyond the largest [`f32`], and one within about $2^{-1025}$ of one beyond the
3110/// largest [`f64`]; so does any [`Rational`] small enough that its reciprocal alone leaves the
3111/// range, and $0$ itself, whose cotangent is $\infty$. A [`Rational`] as close to an odd multiple
3112/// of $\pi/2$ underflows instead, to $\pm0.0$.
3113///
3114/// # Worst-case complexity
3115/// $T(m, e) = O((m+e) (\log (m+e))^2 \log\log (m+e))$
3116///
3117/// $M(m, e) = O((m+e) \log (m+e))$
3118///
3119/// where $T$ is time, $M$ is additional memory, $m$ is `x.significant_bits()`, and $e$ is
3120/// `x.floor_log_base_2_abs()` (taken as 0 when it is negative or $x = 0$): for $|x| \geq 3$ the
3121/// argument is reduced modulo $2\pi$, which needs $\pi$ to about $e$ bits.
3122///
3123/// # Examples
3124/// ```
3125/// use malachite_base::num::basic::traits::Zero;
3126/// use malachite_base::num::float::NiceFloat;
3127/// use malachite_float::float::arithmetic::cot::primitive_float_cot_rational;
3128/// use malachite_q::Rational;
3129///
3130/// assert_eq!(
3131///     NiceFloat(primitive_float_cot_rational::<f64>(&Rational::ZERO)),
3132///     NiceFloat(f64::INFINITY)
3133/// );
3134/// assert_eq!(
3135///     NiceFloat(primitive_float_cot_rational::<f64>(
3136///         &Rational::from_unsigneds(1u8, 3)
3137///     )),
3138///     NiceFloat(2.888057036277277)
3139/// );
3140/// assert_eq!(
3141///     NiceFloat(primitive_float_cot_rational::<f32>(
3142///         &Rational::from_unsigneds(1u8, 3)
3143///     )),
3144///     NiceFloat(2.888057)
3145/// );
3146/// assert_eq!(
3147///     NiceFloat(primitive_float_cot_rational::<f64>(&Rational::from(10000))),
3148///     NiceFloat(3.11554495756144)
3149/// );
3150/// ```
3151#[inline]
3152#[allow(clippy::type_repetition_in_bounds)]
3153pub fn primitive_float_cot_rational<T: PrimitiveFloat>(x: &Rational) -> T
3154where
3155    Float: PartialOrd<T>,
3156    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
3157{
3158    emulate_rational_to_float_fn(Float::cot_rational_prec_ref, x)
3159}
3160
3161/// Computes $\cot(2\pi x/u)$, the cotangent of a primitive float measured in $u$ths of a turn (so
3162/// that `u = 360` is degrees).
3163///
3164/// $$
3165/// f(x,u) = \cot(2\pi x/u)+\varepsilon.
3166/// $$
3167/// - If $x$ is not finite, $u=0$, or $x/u$ is a multiple of $1/8$, $\varepsilon$ may be ignored or
3168///   assumed to be 0.
3169/// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |\cot(2\pi x/u)|\rfloor-p}$, where $p$ is the
3170///   precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
3171///
3172/// Special cases:
3173/// - $f(\text{NaN},u)=\text{NaN}$
3174/// - $f(\pm\infty,u)=\text{NaN}$
3175/// - $f(x,0)=\text{NaN}$
3176/// - $f(\pm0.0,u)=\pm\infty$
3177/// - If $x/u$ is a multiple of $1/2$, the cotangent has a pole there, and the result is exactly
3178///   $\\pm\\infty$: the sine is a zero carrying the sign of $x$ and the cosine is $\\pm1$, so the
3179///   sign is that of $x$ at an even multiple and the opposite at an odd one.
3180/// - If $x/u$ is an odd multiple of $1/4$, the result is exactly $\\pm0.0$, and if it is an odd
3181///   multiple of $1/8$, exactly $\\pm1$.
3182/// - If $x/u$ in lowest terms has denominator 3 or 6, the result is $\\pm\\sqrt3/3$, and if it has
3183///   denominator 12, $\\pm\\sqrt3$.
3184///
3185/// Overflow happens at a pole, where the result is exactly $\pm\infty$, and for a tiny $x/u$, whose
3186/// cotangent is close to $u/(2\pi x)$: an [`f32`] or [`f64`] whose fraction of a turn is not a
3187/// multiple of $1/2$ is more than $2^{-66}$ of a turn away from one, so a cotangent that is not a
3188/// pole stays below $2^{64}$ unless the angle itself is tiny. Underflow happens only at an odd
3189/// quarter turn, where the result is exactly $\pm0.0$: a fraction of a turn that is not one is more
3190/// than $2^{-66}$ away from it, so the cotangent stays above $2^{-67}$.
3191///
3192/// # Worst-case complexity
3193/// Constant time and additional memory.
3194///
3195/// # Examples
3196/// ```
3197/// use malachite_base::num::basic::traits::NegativeInfinity;
3198/// use malachite_base::num::float::NiceFloat;
3199/// use malachite_float::float::arithmetic::cot::primitive_float_cot_with_period;
3200///
3201/// assert!(primitive_float_cot_with_period(f32::NAN, 360).is_nan());
3202/// assert!(primitive_float_cot_with_period(f32::INFINITY, 360).is_nan());
3203/// assert!(primitive_float_cot_with_period(f32::NEGATIVE_INFINITY, 360).is_nan());
3204/// assert!(primitive_float_cot_with_period(1.0f32, 0).is_nan());
3205/// assert_eq!(
3206///     NiceFloat(primitive_float_cot_with_period(-0.0f32, 360)),
3207///     NiceFloat(f32::NEGATIVE_INFINITY)
3208/// );
3209/// // a quarter turn is exactly 0
3210/// assert_eq!(
3211///     NiceFloat(primitive_float_cot_with_period(90.0f32, 360)),
3212///     NiceFloat(0.0)
3213/// );
3214/// // a half turn is a pole
3215/// assert_eq!(
3216///     NiceFloat(primitive_float_cot_with_period(180.0f32, 360)),
3217///     NiceFloat(f32::NEGATIVE_INFINITY)
3218/// );
3219/// // a sixth of a turn: sqrt(3)/3
3220/// assert_eq!(
3221///     NiceFloat(primitive_float_cot_with_period(60.0f32, 360)),
3222///     NiceFloat(0.57735026)
3223/// );
3224/// // a twelfth of a turn: sqrt(3)
3225/// assert_eq!(
3226///     NiceFloat(primitive_float_cot_with_period(30.0f64, 360)),
3227///     NiceFloat(1.7320508075688772)
3228/// );
3229/// assert_eq!(
3230///     NiceFloat(primitive_float_cot_with_period(1.0f32, 7)),
3231///     NiceFloat(0.7974734)
3232/// );
3233/// assert_eq!(
3234///     NiceFloat(primitive_float_cot_with_period(1.0f64, 7)),
3235///     NiceFloat(0.7974733888824039)
3236/// );
3237/// ```
3238#[inline]
3239#[allow(clippy::type_repetition_in_bounds)]
3240pub fn primitive_float_cot_with_period<T: PrimitiveFloat>(x: T, u: u64) -> T
3241where
3242    Float: From<T> + PartialOrd<T>,
3243    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
3244{
3245    emulate_float_to_float_fn(|x, prec| Float::cot_with_period_prec(x, u, prec), x)
3246}
3247
3248/// Computes $\cot(2\pi x/u)$, the cotangent of a [`Rational`] measured in $u$ths of a turn (so that
3249/// `u = 360` is degrees), returning the result as a primitive float.
3250///
3251/// $$
3252/// f(x,u) = \cot(2\pi x/u)+\varepsilon.
3253/// $$
3254/// - If $u=0$ or $x/u$ is a multiple of $1/8$, $\varepsilon$ may be ignored or assumed to be 0.
3255/// - Otherwise, $|\varepsilon| < 2^{\lfloor\log_2 |\cot(2\pi x/u)|\rfloor-p}$, where $p$ is the
3256///   precision of the output (24 if `T` is a [`f32`] and 53 if `T` is a [`f64`]).
3257///
3258/// Special cases:
3259/// - $f(x,0)=\text{NaN}$
3260/// - $f(0,u)=\infty$
3261/// - If $x/u$ is a multiple of $1/2$, the cotangent has a pole there, and the result is exactly
3262///   $\\pm\\infty$: the sine is a zero carrying the sign of $x$ and the cosine is $\\pm1$, so the
3263///   sign is that of $x$ at an even multiple and the opposite at an odd one.
3264/// - If $x/u$ is an odd multiple of $1/4$, the result is exactly $\\pm0.0$, and if it is an odd
3265///   multiple of $1/8$, exactly $\\pm1$.
3266/// - If $x/u$ in lowest terms has denominator 3 or 6, the result is $\\pm\\sqrt3/3$, and if it has
3267///   denominator 12, $\\pm\\sqrt3$.
3268///
3269/// Overflow is possible away from a pole too: a fraction of a turn within about $2^{-130}$ of a
3270/// multiple of $1/2$ has a cotangent beyond the largest [`f32`], and one within about $2^{-1026}$
3271/// of one beyond the largest [`f64`]; so does a fraction of a turn small enough on its own, which a
3272/// [`Rational`] can be however large its denominator is not. The result is then $\pm\infty$. A
3273/// fraction of a turn as close to an odd multiple of $1/4$ underflows instead, to $\pm0.0$.
3274///
3275/// # Worst-case complexity
3276/// $T(m) = O(m (\log m)^2 \log\log m)$
3277///
3278/// $M(m) = O(m \log m)$
3279///
3280/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`: the fraction of
3281/// a turn is reduced modulo 1 exactly, so the magnitude of $x$ does not drive the cost.
3282///
3283/// # Examples
3284/// ```
3285/// use malachite_base::num::basic::traits::Zero;
3286/// use malachite_base::num::float::NiceFloat;
3287/// use malachite_float::float::arithmetic::cot::primitive_float_cot_with_period_rational;
3288/// use malachite_q::Rational;
3289///
3290/// assert!(primitive_float_cot_with_period_rational::<f64>(&Rational::ZERO, 0).is_nan());
3291/// assert_eq!(
3292///     NiceFloat(primitive_float_cot_with_period_rational::<f64>(
3293///         &Rational::ZERO,
3294///         360
3295///     )),
3296///     NiceFloat(f64::INFINITY)
3297/// );
3298/// // a quarter turn is exactly 0
3299/// assert_eq!(
3300///     NiceFloat(primitive_float_cot_with_period_rational::<f64>(
3301///         &Rational::from_unsigneds(1u8, 4),
3302///         1
3303///     )),
3304///     NiceFloat(0.0)
3305/// );
3306/// // an eighth of a turn is exactly 1
3307/// assert_eq!(
3308///     NiceFloat(primitive_float_cot_with_period_rational::<f64>(
3309///         &Rational::from_unsigneds(1u8, 8),
3310///         1
3311///     )),
3312///     NiceFloat(1.0)
3313/// );
3314/// // a twelfth of a turn: sqrt(3)
3315/// assert_eq!(
3316///     NiceFloat(primitive_float_cot_with_period_rational::<f64>(
3317///         &Rational::from_unsigneds(1u8, 12),
3318///         1
3319///     )),
3320///     NiceFloat(1.7320508075688772)
3321/// );
3322/// assert_eq!(
3323///     NiceFloat(primitive_float_cot_with_period_rational::<f32>(
3324///         &Rational::from_unsigneds(1u8, 7),
3325///         1
3326///     )),
3327///     NiceFloat(0.7974734)
3328/// );
3329/// assert_eq!(
3330///     NiceFloat(primitive_float_cot_with_period_rational::<f64>(
3331///         &Rational::from_unsigneds(1u8, 7),
3332///         1
3333///     )),
3334///     NiceFloat(0.7974733888824039)
3335/// );
3336/// ```
3337#[inline]
3338#[allow(clippy::type_repetition_in_bounds)]
3339pub fn primitive_float_cot_with_period_rational<T: PrimitiveFloat>(x: &Rational, u: u64) -> T
3340where
3341    Float: PartialOrd<T>,
3342    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
3343{
3344    emulate_rational_to_float_fn(
3345        |x, prec| Float::cot_with_period_rational_prec_ref(x, u, prec),
3346        x,
3347    )
3348}
3349
3350/// Computes $\cot(\pi x)$, the cotangent of a primitive float measured in half-turns.
3351///
3352/// This is `primitive_float_cot_with_period` with a period of 2: see
3353/// [`primitive_float_cot_with_period`] for the error bound and the special cases, with $u = 2$.
3354/// Integers are poles and give exactly $\pm\infty$, with the sign of $x$ at an even integer and the
3355/// opposite at an odd one; half-integers give exactly $\pm0.0$; odd multiples of $1/4$ give exactly
3356/// $\pm1$; odd multiples of $1/6$ give $\pm\sqrt3$; and multiples of $1/3$ that are not integers
3357/// give $\pm\sqrt3/3$.
3358///
3359/// # Worst-case complexity
3360/// Constant time and additional memory.
3361///
3362/// # Examples
3363/// ```
3364/// use malachite_base::num::basic::traits::NegativeInfinity;
3365/// use malachite_base::num::float::NiceFloat;
3366/// use malachite_float::float::arithmetic::cot::primitive_float_cot_pi;
3367///
3368/// assert!(primitive_float_cot_pi(f32::NAN).is_nan());
3369/// // a half-integer is exactly 0
3370/// assert_eq!(NiceFloat(primitive_float_cot_pi(0.5f32)), NiceFloat(0.0));
3371/// // an integer is a pole
3372/// assert_eq!(
3373///     NiceFloat(primitive_float_cot_pi(1.0f64)),
3374///     NiceFloat(f64::NEGATIVE_INFINITY)
3375/// );
3376/// // an odd multiple of a quarter is exactly 1
3377/// assert_eq!(NiceFloat(primitive_float_cot_pi(0.25f32)), NiceFloat(1.0));
3378/// assert_eq!(
3379///     NiceFloat(primitive_float_cot_pi(0.1f32)),
3380///     NiceFloat(3.0776834)
3381/// );
3382/// assert_eq!(
3383///     NiceFloat(primitive_float_cot_pi(0.1f64)),
3384///     NiceFloat(3.077683537175253)
3385/// );
3386/// ```
3387#[inline]
3388#[allow(clippy::type_repetition_in_bounds)]
3389pub fn primitive_float_cot_pi<T: PrimitiveFloat>(x: T) -> T
3390where
3391    Float: From<T> + PartialOrd<T>,
3392    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
3393{
3394    primitive_float_cot_with_period(x, 2)
3395}
3396
3397/// Computes $\cot(\pi x)$, the cotangent of a [`Rational`] measured in half-turns, returning the
3398/// result as a primitive float.
3399///
3400/// This is `primitive_float_cot_with_period_rational` with a period of 2: see
3401/// [`primitive_float_cot_with_period_rational`] for the error bound, the special cases, and the
3402/// complexity, with $u = 2$.
3403///
3404/// # Worst-case complexity
3405/// $T(m) = O(m (\log m)^2 \log\log m)$
3406///
3407/// $M(m) = O(m \log m)$
3408///
3409/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
3410///
3411/// # Examples
3412/// ```
3413/// use malachite_base::num::basic::traits::OneHalf;
3414/// use malachite_base::num::float::NiceFloat;
3415/// use malachite_float::float::arithmetic::cot::primitive_float_cot_pi_rational;
3416/// use malachite_q::Rational;
3417///
3418/// // a half of a half-turn is exactly 0
3419/// assert_eq!(
3420///     NiceFloat(primitive_float_cot_pi_rational::<f64>(&Rational::ONE_HALF)),
3421///     NiceFloat(0.0)
3422/// );
3423/// // a sixth of a half-turn: sqrt(3)
3424/// assert_eq!(
3425///     NiceFloat(primitive_float_cot_pi_rational::<f64>(
3426///         &Rational::from_unsigneds(1u8, 6)
3427///     )),
3428///     NiceFloat(1.7320508075688772)
3429/// );
3430/// assert_eq!(
3431///     NiceFloat(primitive_float_cot_pi_rational::<f64>(
3432///         &Rational::from_unsigneds(1u8, 7)
3433///     )),
3434///     NiceFloat(2.0765213965723364)
3435/// );
3436/// ```
3437#[inline]
3438#[allow(clippy::type_repetition_in_bounds)]
3439pub fn primitive_float_cot_pi_rational<T: PrimitiveFloat>(x: &Rational) -> T
3440where
3441    Float: PartialOrd<T>,
3442    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
3443{
3444    primitive_float_cot_with_period_rational(x, 2)
3445}