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