malachite_float/float/arithmetic/acoth.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
10use crate::float::arithmetic::acsch::{RECIPROCAL_SAFE_EXPONENT, via_exact_reciprocal};
11use crate::float::arithmetic::asinh::round_with_error;
12use crate::{Float, emulate_float_to_float_fn, emulate_rational_to_float_fn};
13use core::cmp::Ordering::{self, *};
14use malachite_base::fail_on_untested_path;
15use malachite_base::num::arithmetic::traits::{
16 Abs, Acoth, AcothAssign, CeilingLogBase2, IsPowerOf2, Ln, Ln1PlusX, Reciprocal,
17};
18use malachite_base::num::basic::floats::PrimitiveFloat;
19use malachite_base::num::basic::integers::PrimitiveInt;
20use malachite_base::num::basic::traits::{
21 Infinity as InfinityTrait, NaN as NaNTrait, NegativeInfinity, NegativeZero, One, Two,
22 Zero as ZeroTrait,
23};
24use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
25use malachite_base::num::logic::traits::SignificantBits;
26use malachite_base::rounding_modes::RoundingMode::{self, *};
27use malachite_nz::platform::Limb;
28use malachite_q::Rational;
29
30// Computes acoth(x) = atanh(1/x) = ln((x + 1)/(x - 1))/2 for a finite `Float` x with |x| > 1. MPFR
31// has no acoth, so this is Malachite's own algorithm. The work is done on |x|, acoth being odd.
32//
33// Going through 1/x would be ill-conditioned near |x| = 1, where atanh has an infinite derivative,
34// so instead acoth(|x|) = ln(1 + u)/2 with u = 2/d and d = |x| - 1, which is exact for |x| <= 2
35// (Sterbenz) and is otherwise rounded down, so that it cannot overflow for an |x| near the largest
36// finite `Float`, with a relative error below 2^(1-wp). Then u has a relative error below 3 *
37// 2^-wp, which, since ln(1+u) >= u/(1+u), moves ln(1+u) by less than 3 ulps; its own rounding adds
38// 1/2 ulp, and the halving is exact: under 2^2 ulps.
39//
40// When |x| is a power of 2, 1/x is exact, and acoth(x) = atanh(1/x) is taken from `atanh` by
41// `via_exact_reciprocal`: acoth(2^k) lies just above 2^-k, an exactly representable value.
42//
43// If 2/d would overflow, which needs an x with a precision above 2^30, ln(|x| + 1) - ln(d) is used
44// instead.
45fn acoth_prec_round_normal_ref(x: &Float, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
46 assert_ne!(rm, Exact, "Inexact acoth");
47 if let Some(result) = via_exact_reciprocal(x, prec, rm, Float::atanh_prec_round) {
48 return result;
49 }
50 let negative = *x < 0u32;
51 let x_abs = x.abs();
52 let near_one = x_abs.get_exponent().unwrap() == 1; // 1 < |x| < 2
53 // d = |x| - 1, exact for |x| < 2
54 let exact_d = near_one.then(|| {
55 let (d, o) = x_abs.sub_prec_ref_val(Float::ONE, x_abs.get_prec().unwrap());
56 assert_eq!(o, Equal);
57 d
58 });
59 let mut working_prec = prec + prec.ceiling_log_base_2() + 10;
60 let mut increment = Limb::WIDTH;
61 loop {
62 let d_rounded;
63 let d = if let Some(d) = &exact_d {
64 d
65 } else {
66 d_rounded = x_abs
67 .sub_prec_round_ref_val(Float::ONE, working_prec, Floor)
68 .0;
69 &d_rounded
70 };
71 let t = if i64::from(d.get_exponent().unwrap()) < RECIPROCAL_SAFE_EXPONENT {
72 fail_on_untested_path("acoth_prec_round_normal_ref, 2/(|x| - 1) may overflow");
73 // ln(|x| + 1) - ln(d)
74 x_abs.add_prec_ref_val(Float::ONE, working_prec).0.ln() - d.ln_prec_ref(working_prec).0
75 } else {
76 // ln(1 + 2/d)
77 Float::TWO.div_prec_val_ref(d, working_prec).0.ln_1_plus_x()
78 } >> 1u32;
79 if let Some(result) =
80 round_with_error(if negative { -t } else { t }, working_prec, 2, prec, rm)
81 {
82 return result;
83 }
84 working_prec += increment;
85 increment = working_prec >> 1;
86 }
87}
88
89impl Float {
90 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
91 /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
92 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded inverse
93 /// hyperbolic cotangent is less than, equal to, or greater than the exact inverse hyperbolic
94 /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function
95 /// returns a `NaN` it also returns `Equal`.
96 ///
97 /// See [`RoundingMode`] for a description of the possible rounding modes.
98 ///
99 /// $$
100 /// f(x,p,m) = \operatorname{acoth} x+\varepsilon.
101 /// $$
102 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
103 /// assumed to be 0.
104 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
105 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$.
106 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
107 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$.
108 ///
109 /// If the output has a precision, it is `prec`.
110 ///
111 /// Special cases:
112 /// - $f(\text{NaN},p,m)=\text{NaN}$
113 /// - $f(\infty,p,m)=0.0$
114 /// - $f(-\infty,p,m)=-0.0$
115 /// - $f(\pm1,p,m)=\pm\infty$
116 /// - $f(x,p,m)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
117 ///
118 /// The result never overflows or underflows: for a finite $x$ with $|x|>1$ and precision $q$,
119 /// $1/|x| < |\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2^q)$, and every finite [`Float`]
120 /// is below $2^{2^{30}-1}$.
121 ///
122 /// If you know you'll be using `Nearest`, consider using [`Float::acoth_prec`] instead. If you
123 /// know that your target precision is the precision of the input, consider using
124 /// [`Float::acoth_round`] instead. If both of these things are true, consider using
125 /// [`Float::acoth`] instead.
126 ///
127 /// # Worst-case complexity
128 /// $T(n) = O(n (\log n)^2 \log\log n)$
129 ///
130 /// $M(n) = O(n \log n)$
131 ///
132 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
133 /// self.significant_bits())`.
134 ///
135 /// # Panics
136 /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
137 /// since the inverse hyperbolic cotangent of such a [`Float`] is never exactly representable,
138 /// or if `prec` is zero.
139 ///
140 /// # Examples
141 /// ```
142 /// use malachite_base::rounding_modes::RoundingMode::*;
143 /// use malachite_float::Float;
144 /// use std::cmp::Ordering::*;
145 ///
146 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round(5, Floor);
147 /// assert_eq!(c.to_string(), "0.531");
148 /// assert_eq!(o, Less);
149 ///
150 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round(5, Ceiling);
151 /// assert_eq!(c.to_string(), "0.562");
152 /// assert_eq!(o, Greater);
153 ///
154 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round(5, Nearest);
155 /// assert_eq!(c.to_string(), "0.562");
156 /// assert_eq!(o, Greater);
157 ///
158 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round(20, Floor);
159 /// assert_eq!(c.to_string(), "0.54930592");
160 /// assert_eq!(o, Less);
161 ///
162 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round(20, Ceiling);
163 /// assert_eq!(c.to_string(), "0.54930687");
164 /// assert_eq!(o, Greater);
165 ///
166 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round(20, Nearest);
167 /// assert_eq!(c.to_string(), "0.54930592");
168 /// assert_eq!(o, Less);
169 /// ```
170 #[inline]
171 pub fn acoth_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
172 self.acoth_prec_round_ref(prec, rm)
173 }
174
175 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
176 /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
177 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded inverse
178 /// hyperbolic cotangent is less than, equal to, or greater than the exact inverse hyperbolic
179 /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function
180 /// returns a `NaN` it also returns `Equal`.
181 ///
182 /// See [`RoundingMode`] for a description of the possible rounding modes.
183 ///
184 /// $$
185 /// f(x,p,m) = \operatorname{acoth} x+\varepsilon.
186 /// $$
187 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
188 /// assumed to be 0.
189 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
190 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$.
191 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
192 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$.
193 ///
194 /// If the output has a precision, it is `prec`.
195 ///
196 /// Special cases:
197 /// - $f(\text{NaN},p,m)=\text{NaN}$
198 /// - $f(\infty,p,m)=0.0$
199 /// - $f(-\infty,p,m)=-0.0$
200 /// - $f(\pm1,p,m)=\pm\infty$
201 /// - $f(x,p,m)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
202 ///
203 /// The result never overflows or underflows: for a finite $x$ with $|x|>1$ and precision $q$,
204 /// $1/|x| < |\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2^q)$, and every finite [`Float`]
205 /// is below $2^{2^{30}-1}$.
206 ///
207 /// If you know you'll be using `Nearest`, consider using [`Float::acoth_prec_ref`] instead. If
208 /// you know that your target precision is the precision of the input, consider using
209 /// [`Float::acoth_round_ref`] instead. If both of these things are true, consider using
210 /// `(&Float).acoth()` instead.
211 ///
212 /// # Worst-case complexity
213 /// $T(n) = O(n (\log n)^2 \log\log n)$
214 ///
215 /// $M(n) = O(n \log n)$
216 ///
217 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
218 /// self.significant_bits())`.
219 ///
220 /// # Panics
221 /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
222 /// since the inverse hyperbolic cotangent of such a [`Float`] is never exactly representable,
223 /// or if `prec` is zero.
224 ///
225 /// # Examples
226 /// ```
227 /// use malachite_base::rounding_modes::RoundingMode::*;
228 /// use malachite_float::Float;
229 /// use std::cmp::Ordering::*;
230 ///
231 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round_ref(5, Floor);
232 /// assert_eq!(c.to_string(), "0.531");
233 /// assert_eq!(o, Less);
234 ///
235 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round_ref(5, Ceiling);
236 /// assert_eq!(c.to_string(), "0.562");
237 /// assert_eq!(o, Greater);
238 ///
239 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round_ref(5, Nearest);
240 /// assert_eq!(c.to_string(), "0.562");
241 /// assert_eq!(o, Greater);
242 ///
243 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round_ref(20, Floor);
244 /// assert_eq!(c.to_string(), "0.54930592");
245 /// assert_eq!(o, Less);
246 ///
247 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round_ref(20, Ceiling);
248 /// assert_eq!(c.to_string(), "0.54930687");
249 /// assert_eq!(o, Greater);
250 ///
251 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_round_ref(20, Nearest);
252 /// assert_eq!(c.to_string(), "0.54930592");
253 /// assert_eq!(o, Less);
254 /// ```
255 pub fn acoth_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
256 assert_ne!(prec, 0);
257 match &self.0 {
258 // acoth(NaN) = NaN, and acoth(±0) = NaN since |0| < 1
259 NaN | Zero { .. } => (Self::NAN, Equal),
260 // acoth(±inf) = ±0
261 Infinity { sign } => (
262 if *sign {
263 Self::ZERO
264 } else {
265 Self::NEGATIVE_ZERO
266 },
267 Equal,
268 ),
269 // acoth(x) = NaN for |x| < 1, and acoth(±1) = ±inf
270 Finite {
271 sign,
272 exponent,
273 significand,
274 ..
275 } => {
276 if *exponent <= 0 {
277 (Self::NAN, Equal)
278 } else if *exponent == 1 && significand.is_power_of_2() {
279 (
280 if *sign {
281 Self::INFINITY
282 } else {
283 Self::NEGATIVE_INFINITY
284 },
285 Equal,
286 )
287 } else {
288 acoth_prec_round_normal_ref(self, prec, rm)
289 }
290 }
291 }
292 }
293
294 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
295 /// the result to the nearest value of the specified precision. The [`Float`] is taken by value.
296 /// An [`Ordering`] is also returned, indicating whether the rounded inverse hyperbolic
297 /// cotangent is less than, equal to, or greater than the exact inverse hyperbolic cotangent.
298 /// Although `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN`
299 /// it also returns `Equal`.
300 ///
301 /// If the inverse hyperbolic cotangent is equidistant from two [`Float`]s with the specified
302 /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
303 /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
304 ///
305 /// $$
306 /// f(x,p) = \operatorname{acoth} x+\varepsilon.
307 /// $$
308 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
309 /// assumed to be 0.
310 /// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
311 /// |\operatorname{acoth} x|\rfloor-p}$.
312 ///
313 /// If the output has a precision, it is `prec`.
314 ///
315 /// Special cases:
316 /// - $f(\text{NaN},p)=\text{NaN}$
317 /// - $f(\infty,p)=0.0$
318 /// - $f(-\infty,p)=-0.0$
319 /// - $f(\pm1,p)=\pm\infty$
320 /// - $f(x,p)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
321 ///
322 /// The result never overflows or underflows: for a finite $x$ with $|x|>1$ and precision $q$,
323 /// $1/|x| < |\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2^q)$, and every finite [`Float`]
324 /// is below $2^{2^{30}-1}$.
325 ///
326 /// If you want to use a rounding mode other than `Nearest`, consider using
327 /// [`Float::acoth_prec_round`] instead. If you know that your target precision is the precision
328 /// of the input, consider using [`Float::acoth`] instead.
329 ///
330 /// # Worst-case complexity
331 /// $T(n) = O(n (\log n)^2 \log\log n)$
332 ///
333 /// $M(n) = O(n \log n)$
334 ///
335 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
336 /// self.significant_bits())`.
337 ///
338 /// # Panics
339 /// Panics if `prec` is zero.
340 ///
341 /// # Examples
342 /// ```
343 /// use malachite_float::Float;
344 /// use std::cmp::Ordering::*;
345 ///
346 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec(5);
347 /// assert_eq!(c.to_string(), "0.562");
348 /// assert_eq!(o, Greater);
349 ///
350 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec(20);
351 /// assert_eq!(c.to_string(), "0.54930592");
352 /// assert_eq!(o, Less);
353 /// ```
354 #[inline]
355 pub fn acoth_prec(self, prec: u64) -> (Self, Ordering) {
356 self.acoth_prec_round(prec, Nearest)
357 }
358
359 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
360 /// the result to the nearest value of the specified precision. The [`Float`] is taken by
361 /// reference. An [`Ordering`] is also returned, indicating whether the rounded inverse
362 /// hyperbolic cotangent is less than, equal to, or greater than the exact inverse hyperbolic
363 /// cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this function
364 /// returns a `NaN` it also returns `Equal`.
365 ///
366 /// If the inverse hyperbolic cotangent is equidistant from two [`Float`]s with the specified
367 /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
368 /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
369 ///
370 /// $$
371 /// f(x,p) = \operatorname{acoth} x+\varepsilon.
372 /// $$
373 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
374 /// assumed to be 0.
375 /// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
376 /// |\operatorname{acoth} x|\rfloor-p}$.
377 ///
378 /// If the output has a precision, it is `prec`.
379 ///
380 /// Special cases:
381 /// - $f(\text{NaN},p)=\text{NaN}$
382 /// - $f(\infty,p)=0.0$
383 /// - $f(-\infty,p)=-0.0$
384 /// - $f(\pm1,p)=\pm\infty$
385 /// - $f(x,p)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
386 ///
387 /// The result never overflows or underflows: for a finite $x$ with $|x|>1$ and precision $q$,
388 /// $1/|x| < |\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2^q)$, and every finite [`Float`]
389 /// is below $2^{2^{30}-1}$.
390 ///
391 /// If you want to use a rounding mode other than `Nearest`, consider using
392 /// [`Float::acoth_prec_round_ref`] instead. If you know that your target precision is the
393 /// precision of the input, consider using `(&Float).acoth()` instead.
394 ///
395 /// # Worst-case complexity
396 /// $T(n) = O(n (\log n)^2 \log\log n)$
397 ///
398 /// $M(n) = O(n \log n)$
399 ///
400 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
401 /// self.significant_bits())`.
402 ///
403 /// # Panics
404 /// Panics if `prec` is zero.
405 ///
406 /// # Examples
407 /// ```
408 /// use malachite_float::Float;
409 /// use std::cmp::Ordering::*;
410 ///
411 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_ref(5);
412 /// assert_eq!(c.to_string(), "0.562");
413 /// assert_eq!(o, Greater);
414 ///
415 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_prec_ref(20);
416 /// assert_eq!(c.to_string(), "0.54930592");
417 /// assert_eq!(o, Less);
418 /// ```
419 #[inline]
420 pub fn acoth_prec_ref(&self, prec: u64) -> (Self, Ordering) {
421 self.acoth_prec_round_ref(prec, Nearest)
422 }
423
424 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
425 /// the result with the specified rounding mode. The [`Float`] is taken by value. An
426 /// [`Ordering`] is also returned, indicating whether the rounded inverse hyperbolic cotangent
427 /// is less than, equal to, or greater than the exact inverse hyperbolic cotangent. Although
428 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
429 /// returns `Equal`.
430 ///
431 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
432 /// description of the possible rounding modes.
433 ///
434 /// $$
435 /// f(x,m) = \operatorname{acoth} x+\varepsilon.
436 /// $$
437 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
438 /// assumed to be 0.
439 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
440 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$, where $p$ is the
441 /// precision of the input.
442 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
443 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the
444 /// precision of the input.
445 ///
446 /// If the output has a precision, it is the precision of the input.
447 ///
448 /// Special cases:
449 /// - $f(\text{NaN},m)=\text{NaN}$
450 /// - $f(\infty,m)=0.0$
451 /// - $f(-\infty,m)=-0.0$
452 /// - $f(\pm1,m)=\pm\infty$
453 /// - $f(x,m)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
454 ///
455 /// The result never overflows or underflows: for a finite $x$ with $|x|>1$ and precision $q$,
456 /// $1/|x| < |\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2^q)$, and every finite [`Float`]
457 /// is below $2^{2^{30}-1}$.
458 ///
459 /// If you want to specify an output precision, consider using [`Float::acoth_prec_round`]
460 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
461 /// [`Float::acoth`] instead.
462 ///
463 /// # Worst-case complexity
464 /// $T(n) = O(n (\log n)^2 \log\log n)$
465 ///
466 /// $M(n) = O(n \log n)$
467 ///
468 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
469 ///
470 /// # Panics
471 /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
472 /// since the inverse hyperbolic cotangent of such a [`Float`] is never exactly representable.
473 ///
474 /// # Examples
475 /// ```
476 /// use malachite_base::rounding_modes::RoundingMode::*;
477 /// use malachite_float::Float;
478 /// use std::cmp::Ordering::*;
479 ///
480 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_round(Floor);
481 /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
482 /// assert_eq!(o, Less);
483 ///
484 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_round(Ceiling);
485 /// assert_eq!(c.to_string(), "0.54930614433405484569762261846192");
486 /// assert_eq!(o, Greater);
487 ///
488 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_round(Nearest);
489 /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
490 /// assert_eq!(o, Less);
491 /// ```
492 #[inline]
493 pub fn acoth_round(self, rm: RoundingMode) -> (Self, Ordering) {
494 let prec = self.significant_bits();
495 self.acoth_prec_round(prec, rm)
496 }
497
498 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
499 /// the result with the specified rounding mode. The [`Float`] is taken by reference. An
500 /// [`Ordering`] is also returned, indicating whether the rounded inverse hyperbolic cotangent
501 /// is less than, equal to, or greater than the exact inverse hyperbolic cotangent. Although
502 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
503 /// returns `Equal`.
504 ///
505 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
506 /// description of the possible rounding modes.
507 ///
508 /// $$
509 /// f(x,m) = \operatorname{acoth} x+\varepsilon.
510 /// $$
511 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
512 /// assumed to be 0.
513 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
514 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$, where $p$ is the
515 /// precision of the input.
516 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
517 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the
518 /// precision of the input.
519 ///
520 /// If the output has a precision, it is the precision of the input.
521 ///
522 /// Special cases:
523 /// - $f(\text{NaN},m)=\text{NaN}$
524 /// - $f(\infty,m)=0.0$
525 /// - $f(-\infty,m)=-0.0$
526 /// - $f(\pm1,m)=\pm\infty$
527 /// - $f(x,m)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
528 ///
529 /// The result never overflows or underflows: for a finite $x$ with $|x|>1$ and precision $q$,
530 /// $1/|x| < |\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2^q)$, and every finite [`Float`]
531 /// is below $2^{2^{30}-1}$.
532 ///
533 /// If you want to specify an output precision, consider using [`Float::acoth_prec_round_ref`]
534 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
535 /// `(&Float).acoth()` instead.
536 ///
537 /// # Worst-case complexity
538 /// $T(n) = O(n (\log n)^2 \log\log n)$
539 ///
540 /// $M(n) = O(n \log n)$
541 ///
542 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
543 ///
544 /// # Panics
545 /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
546 /// since the inverse hyperbolic cotangent of such a [`Float`] is never exactly representable.
547 ///
548 /// # Examples
549 /// ```
550 /// use malachite_base::rounding_modes::RoundingMode::*;
551 /// use malachite_float::Float;
552 /// use std::cmp::Ordering::*;
553 ///
554 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_round_ref(Floor);
555 /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
556 /// assert_eq!(o, Less);
557 ///
558 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_round_ref(Ceiling);
559 /// assert_eq!(c.to_string(), "0.54930614433405484569762261846192");
560 /// assert_eq!(o, Greater);
561 ///
562 /// let (c, o) = (Float::one_prec(100) << 1u32).acoth_round_ref(Nearest);
563 /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
564 /// assert_eq!(o, Less);
565 /// ```
566 #[inline]
567 pub fn acoth_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
568 self.acoth_prec_round_ref(self.significant_bits(), rm)
569 }
570
571 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
572 /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
573 /// replaced by the result, and an [`Ordering`] is returned, indicating whether the rounded
574 /// inverse hyperbolic cotangent is less than, equal to, or greater than the exact inverse
575 /// hyperbolic cotangent. Although `NaN`s are not comparable to any [`Float`], whenever this
576 /// function sets a `NaN` it also returns `Equal`.
577 ///
578 /// See [`RoundingMode`] for a description of the possible rounding modes.
579 ///
580 /// $$
581 /// x \gets \operatorname{acoth} x+\varepsilon.
582 /// $$
583 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
584 /// assumed to be 0.
585 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
586 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$.
587 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
588 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$.
589 ///
590 /// If the output has a precision, it is `prec`.
591 ///
592 /// See the [`Float::acoth_prec_round`] documentation for information on special cases,
593 /// overflow, and underflow.
594 ///
595 /// If you know you'll be using `Nearest`, consider using [`Float::acoth_prec_assign`] instead.
596 /// If you know that your target precision is the precision of the input, consider using
597 /// [`Float::acoth_round_assign`] instead. If both of these things are true, consider using
598 /// [`Float::acoth_assign`] instead.
599 ///
600 /// # Worst-case complexity
601 /// $T(n) = O(n (\log n)^2 \log\log n)$
602 ///
603 /// $M(n) = O(n \log n)$
604 ///
605 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
606 /// self.significant_bits())`.
607 ///
608 /// # Panics
609 /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
610 /// since the inverse hyperbolic cotangent of such a [`Float`] is never exactly representable,
611 /// or if `prec` is zero.
612 ///
613 /// # Examples
614 /// ```
615 /// use malachite_base::rounding_modes::RoundingMode::*;
616 /// use malachite_float::Float;
617 /// use std::cmp::Ordering::*;
618 ///
619 /// let mut x = Float::one_prec(100) << 1u32;
620 /// assert_eq!(x.acoth_prec_round_assign(5, Floor), Less);
621 /// assert_eq!(x.to_string(), "0.531");
622 ///
623 /// let mut x = Float::one_prec(100) << 1u32;
624 /// assert_eq!(x.acoth_prec_round_assign(5, Ceiling), Greater);
625 /// assert_eq!(x.to_string(), "0.562");
626 ///
627 /// let mut x = Float::one_prec(100) << 1u32;
628 /// assert_eq!(x.acoth_prec_round_assign(5, Nearest), Greater);
629 /// assert_eq!(x.to_string(), "0.562");
630 ///
631 /// let mut x = Float::one_prec(100) << 1u32;
632 /// assert_eq!(x.acoth_prec_round_assign(20, Floor), Less);
633 /// assert_eq!(x.to_string(), "0.54930592");
634 ///
635 /// let mut x = Float::one_prec(100) << 1u32;
636 /// assert_eq!(x.acoth_prec_round_assign(20, Ceiling), Greater);
637 /// assert_eq!(x.to_string(), "0.54930687");
638 ///
639 /// let mut x = Float::one_prec(100) << 1u32;
640 /// assert_eq!(x.acoth_prec_round_assign(20, Nearest), Less);
641 /// assert_eq!(x.to_string(), "0.54930592");
642 /// ```
643 #[inline]
644 pub fn acoth_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
645 let o;
646 (*self, o) = self.acoth_prec_round_ref(prec, rm);
647 o
648 }
649
650 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
651 /// the result to the nearest value of the specified precision. The [`Float`] is replaced by the
652 /// result, and an [`Ordering`] is returned, indicating whether the rounded inverse hyperbolic
653 /// cotangent is less than, equal to, or greater than the exact inverse hyperbolic cotangent.
654 /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets a `NaN` it
655 /// also returns `Equal`.
656 ///
657 /// If the inverse hyperbolic cotangent is equidistant from two [`Float`]s with the specified
658 /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
659 /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
660 ///
661 /// $$
662 /// x \gets \operatorname{acoth} x+\varepsilon.
663 /// $$
664 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
665 /// assumed to be 0.
666 /// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
667 /// |\operatorname{acoth} x|\rfloor-p}$.
668 ///
669 /// If the output has a precision, it is `prec`.
670 ///
671 /// See the [`Float::acoth_prec`] documentation for information on special cases, overflow, and
672 /// underflow.
673 ///
674 /// If you want to use a rounding mode other than `Nearest`, consider using
675 /// [`Float::acoth_prec_round_assign`] instead. If you know that your target precision is the
676 /// precision of the input, consider using [`Float::acoth_assign`] instead.
677 ///
678 /// # Worst-case complexity
679 /// $T(n) = O(n (\log n)^2 \log\log n)$
680 ///
681 /// $M(n) = O(n \log n)$
682 ///
683 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
684 /// self.significant_bits())`.
685 ///
686 /// # Panics
687 /// Panics if `prec` is zero.
688 ///
689 /// # Examples
690 /// ```
691 /// use malachite_float::Float;
692 /// use std::cmp::Ordering::*;
693 ///
694 /// let mut x = Float::one_prec(100) << 1u32;
695 /// assert_eq!(x.acoth_prec_assign(5), Greater);
696 /// assert_eq!(x.to_string(), "0.562");
697 ///
698 /// let mut x = Float::one_prec(100) << 1u32;
699 /// assert_eq!(x.acoth_prec_assign(20), Less);
700 /// assert_eq!(x.to_string(), "0.54930592");
701 /// ```
702 #[inline]
703 pub fn acoth_prec_assign(&mut self, prec: u64) -> Ordering {
704 self.acoth_prec_round_assign(prec, Nearest)
705 }
706
707 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], rounding
708 /// the result with the specified rounding mode. The [`Float`] is replaced by the result, and an
709 /// [`Ordering`] is returned, indicating whether the rounded inverse hyperbolic cotangent is
710 /// less than, equal to, or greater than the exact inverse hyperbolic cotangent. Although `NaN`s
711 /// are not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
712 /// `Equal`.
713 ///
714 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
715 /// description of the possible rounding modes.
716 ///
717 /// $$
718 /// x \gets \operatorname{acoth} x+\varepsilon.
719 /// $$
720 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
721 /// assumed to be 0.
722 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
723 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$, where $p$ is the
724 /// precision of the input.
725 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
726 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the
727 /// precision of the input.
728 ///
729 /// If the output has a precision, it is the precision of the input.
730 ///
731 /// See the [`Float::acoth_round`] documentation for information on special cases, overflow, and
732 /// underflow.
733 ///
734 /// If you want to specify an output precision, consider using
735 /// [`Float::acoth_prec_round_assign`] instead. If you know you'll be using the `Nearest`
736 /// rounding mode, consider using [`Float::acoth_assign`] instead.
737 ///
738 /// # Worst-case complexity
739 /// $T(n) = O(n (\log n)^2 \log\log n)$
740 ///
741 /// $M(n) = O(n \log n)$
742 ///
743 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
744 ///
745 /// # Panics
746 /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
747 /// since the inverse hyperbolic cotangent of such a [`Float`] is never exactly representable.
748 ///
749 /// # Examples
750 /// ```
751 /// use malachite_base::rounding_modes::RoundingMode::*;
752 /// use malachite_float::Float;
753 /// use std::cmp::Ordering::*;
754 ///
755 /// let mut x = Float::one_prec(100) << 1u32;
756 /// assert_eq!(x.acoth_round_assign(Floor), Less);
757 /// assert_eq!(x.to_string(), "0.54930614433405484569762261846113");
758 ///
759 /// let mut x = Float::one_prec(100) << 1u32;
760 /// assert_eq!(x.acoth_round_assign(Ceiling), Greater);
761 /// assert_eq!(x.to_string(), "0.54930614433405484569762261846192");
762 ///
763 /// let mut x = Float::one_prec(100) << 1u32;
764 /// assert_eq!(x.acoth_round_assign(Nearest), Less);
765 /// assert_eq!(x.to_string(), "0.54930614433405484569762261846113");
766 /// ```
767 #[inline]
768 pub fn acoth_round_assign(&mut self, rm: RoundingMode) -> Ordering {
769 let prec = self.significant_bits();
770 self.acoth_prec_round_assign(prec, rm)
771 }
772}
773
774impl Float {
775 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Rational`],
776 /// rounding the result to the specified precision and with the specified rounding mode and
777 /// returning the result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is
778 /// also returned, indicating whether the rounded inverse hyperbolic cotangent is less than,
779 /// equal to, or greater than the exact inverse hyperbolic cotangent. Although `NaN`s are not
780 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
781 ///
782 /// See [`RoundingMode`] for a description of the possible rounding modes.
783 ///
784 /// $$
785 /// f(x,p,m) = \operatorname{acoth} x+\varepsilon.
786 /// $$
787 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
788 /// assumed to be 0.
789 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
790 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$.
791 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
792 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$.
793 ///
794 /// These bounds do not apply when the result underflows; see below.
795 ///
796 /// If the output has a precision, it is `prec`.
797 ///
798 /// Special cases:
799 /// - $f(\pm1,p,m)=\pm\infty$
800 /// - $f(x,p,m)=\text{NaN}$ if $|x|<1$
801 ///
802 /// Overflow and underflow:
803 /// - The result never overflows: for $|x|>1$ with denominator $d$, $|x| - 1 \geq 1/d$, so
804 /// $|\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2d)$.
805 /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
806 /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
807 /// instead.
808 /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
809 /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
810 /// instead.
811 /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
812 /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
813 /// instead.
814 /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
815 /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
816 /// returned instead.
817 ///
818 /// Underflow requires $|x| > 2^{2^{30}}$, since $|\operatorname{acoth} x| > 1/|x|$.
819 ///
820 /// If you know you'll be using `Nearest`, consider using [`Float::acoth_rational_prec`]
821 /// instead.
822 ///
823 /// # Worst-case complexity
824 /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
825 ///
826 /// $M(n, m) = O(n \log n + m \log m)$
827 ///
828 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
829 /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
830 /// the input is handled with `Rational` arithmetic.
831 ///
832 /// # Panics
833 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
834 /// with the given precision (which is the case for every $x$ with $|x|>1$).
835 ///
836 /// # Examples
837 /// ```
838 /// use malachite_base::rounding_modes::RoundingMode::*;
839 /// use malachite_float::Float;
840 /// use malachite_q::Rational;
841 /// use std::cmp::Ordering::*;
842 ///
843 /// let (c, o) = Float::acoth_rational_prec_round(Rational::from(3u32), 5, Floor);
844 /// assert_eq!(c.to_string(), "0.344");
845 /// assert_eq!(o, Less);
846 ///
847 /// let (c, o) = Float::acoth_rational_prec_round(Rational::from(3u32), 5, Ceiling);
848 /// assert_eq!(c.to_string(), "0.359");
849 /// assert_eq!(o, Greater);
850 ///
851 /// let (c, o) = Float::acoth_rational_prec_round(Rational::from(3u32), 20, Floor);
852 /// assert_eq!(c.to_string(), "0.34657335");
853 /// assert_eq!(o, Less);
854 ///
855 /// let (c, o) = Float::acoth_rational_prec_round(Rational::from(3u32), 20, Ceiling);
856 /// assert_eq!(c.to_string(), "0.34657383");
857 /// assert_eq!(o, Greater);
858 /// ```
859 #[inline]
860 #[allow(clippy::needless_pass_by_value)]
861 pub fn acoth_rational_prec_round(x: Rational, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
862 Self::acoth_rational_prec_round_ref(&x, prec, rm)
863 }
864
865 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Rational`],
866 /// rounding the result to the specified precision and with the specified rounding mode and
867 /// returning the result as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`]
868 /// is also returned, indicating whether the rounded inverse hyperbolic cotangent is less than,
869 /// equal to, or greater than the exact inverse hyperbolic cotangent. Although `NaN`s are not
870 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
871 ///
872 /// See [`RoundingMode`] for a description of the possible rounding modes.
873 ///
874 /// $$
875 /// f(x,p,m) = \operatorname{acoth} x+\varepsilon.
876 /// $$
877 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
878 /// assumed to be 0.
879 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is not `Nearest`, then
880 /// $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p+1}$.
881 /// - If $\operatorname{acoth} x$ is finite and nonzero, and $m$ is `Nearest`, then
882 /// $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$.
883 ///
884 /// These bounds do not apply when the result underflows; see below.
885 ///
886 /// If the output has a precision, it is `prec`.
887 ///
888 /// Special cases:
889 /// - $f(\pm1,p,m)=\pm\infty$
890 /// - $f(x,p,m)=\text{NaN}$ if $|x|<1$
891 ///
892 /// Overflow and underflow:
893 /// - The result never overflows: for $|x|>1$ with denominator $d$, $|x| - 1 \geq 1/d$, so
894 /// $|\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2d)$.
895 /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
896 /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
897 /// instead.
898 /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
899 /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
900 /// instead.
901 /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
902 /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
903 /// instead.
904 /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
905 /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
906 /// returned instead.
907 ///
908 /// Underflow requires $|x| > 2^{2^{30}}$, since $|\operatorname{acoth} x| > 1/|x|$.
909 ///
910 /// If you know you'll be using `Nearest`, consider using [`Float::acoth_rational_prec_ref`]
911 /// instead.
912 ///
913 /// # Worst-case complexity
914 /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
915 ///
916 /// $M(n, m) = O(n \log n + m \log m)$
917 ///
918 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
919 /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
920 /// the input is handled with `Rational` arithmetic.
921 ///
922 /// # Panics
923 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
924 /// with the given precision (which is the case for every $x$ with $|x|>1$).
925 ///
926 /// # Examples
927 /// ```
928 /// use malachite_base::rounding_modes::RoundingMode::*;
929 /// use malachite_float::Float;
930 /// use malachite_q::Rational;
931 /// use std::cmp::Ordering::*;
932 ///
933 /// let (c, o) = Float::acoth_rational_prec_round_ref(&Rational::from(3u32), 5, Floor);
934 /// assert_eq!(c.to_string(), "0.344");
935 /// assert_eq!(o, Less);
936 ///
937 /// let (c, o) = Float::acoth_rational_prec_round_ref(&Rational::from(3u32), 5, Ceiling);
938 /// assert_eq!(c.to_string(), "0.359");
939 /// assert_eq!(o, Greater);
940 ///
941 /// let (c, o) = Float::acoth_rational_prec_round_ref(&Rational::from(3u32), 20, Floor);
942 /// assert_eq!(c.to_string(), "0.34657335");
943 /// assert_eq!(o, Less);
944 ///
945 /// let (c, o) = Float::acoth_rational_prec_round_ref(&Rational::from(3u32), 20, Ceiling);
946 /// assert_eq!(c.to_string(), "0.34657383");
947 /// assert_eq!(o, Greater);
948 /// ```
949 pub fn acoth_rational_prec_round_ref(
950 x: &Rational,
951 prec: u64,
952 rm: RoundingMode,
953 ) -> (Self, Ordering) {
954 assert_ne!(prec, 0);
955 if *x == 0u32 {
956 // acoth(0) = NaN, since |0| < 1
957 return (Self::NAN, Equal);
958 }
959 // acoth(x) = atanh(1/x), and the reciprocal of a `Rational` is exact
960 Self::atanh_rational_prec_round(x.reciprocal(), prec, rm)
961 }
962
963 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Rational`],
964 /// rounding the result to the nearest value of the specified precision and returning the result
965 /// as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned,
966 /// indicating whether the rounded inverse hyperbolic cotangent is less than, equal to, or
967 /// greater than the exact inverse hyperbolic cotangent. Although `NaN`s are not comparable to
968 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
969 ///
970 /// If the inverse hyperbolic cotangent is equidistant from two [`Float`]s with the specified
971 /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
972 /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
973 ///
974 /// $$
975 /// f(x,p) = \operatorname{acoth} x+\varepsilon,
976 /// $$
977 /// where, if $\operatorname{acoth} x$ is finite and nonzero, $|\varepsilon| \leq
978 /// 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$ (unless the result underflows; see
979 /// below).
980 ///
981 /// If the output has a precision, it is `prec`.
982 ///
983 /// Special cases:
984 /// - $f(\pm1,p)=\pm\infty$
985 /// - $f(x,p)=\text{NaN}$ if $|x|<1$
986 ///
987 /// Overflow and underflow:
988 /// - The result never overflows: for $|x|>1$ with denominator $d$, $|x| - 1 \geq 1/d$, so
989 /// $|\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2d)$.
990 /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
991 /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
992 /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
993 /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
994 ///
995 /// Underflow requires $|x| > 2^{2^{30}}$, since $|\operatorname{acoth} x| > 1/|x|$.
996 ///
997 /// If you want to use a rounding mode other than `Nearest`, consider using
998 /// [`Float::acoth_rational_prec_round`] instead.
999 ///
1000 /// # Worst-case complexity
1001 /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
1002 ///
1003 /// $M(n, m) = O(n \log n + m \log m)$
1004 ///
1005 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1006 /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
1007 /// the input is handled with `Rational` arithmetic.
1008 ///
1009 /// # Panics
1010 /// Panics if `prec` is zero.
1011 ///
1012 /// # Examples
1013 /// ```
1014 /// use malachite_base::num::basic::traits::{One, OneHalf};
1015 /// use malachite_float::Float;
1016 /// use malachite_q::Rational;
1017 /// use std::cmp::Ordering::*;
1018 ///
1019 /// let (c, o) = Float::acoth_rational_prec(Rational::from(3u32), 5);
1020 /// assert_eq!(c.to_string(), "0.344");
1021 /// assert_eq!(o, Less);
1022 ///
1023 /// let (c, o) = Float::acoth_rational_prec(Rational::from(3u32), 20);
1024 /// assert_eq!(c.to_string(), "0.34657335");
1025 /// assert_eq!(o, Less);
1026 ///
1027 /// let (c, o) = Float::acoth_rational_prec(Rational::ONE, 10);
1028 /// assert_eq!(c.to_string(), "Infinity");
1029 /// assert_eq!(o, Equal);
1030 ///
1031 /// let (c, o) = Float::acoth_rational_prec(Rational::ONE_HALF, 10);
1032 /// assert!(c.is_nan());
1033 /// assert_eq!(o, Equal);
1034 /// ```
1035 #[inline]
1036 #[allow(clippy::needless_pass_by_value)]
1037 pub fn acoth_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
1038 Self::acoth_rational_prec_round_ref(&x, prec, Nearest)
1039 }
1040
1041 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Rational`],
1042 /// rounding the result to the nearest value of the specified precision and returning the result
1043 /// as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also returned,
1044 /// indicating whether the rounded inverse hyperbolic cotangent is less than, equal to, or
1045 /// greater than the exact inverse hyperbolic cotangent. Although `NaN`s are not comparable to
1046 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1047 ///
1048 /// If the inverse hyperbolic cotangent is equidistant from two [`Float`]s with the specified
1049 /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
1050 /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
1051 ///
1052 /// $$
1053 /// f(x,p) = \operatorname{acoth} x+\varepsilon,
1054 /// $$
1055 /// where, if $\operatorname{acoth} x$ is finite and nonzero, $|\varepsilon| \leq
1056 /// 2^{\lfloor\log_2 |\operatorname{acoth} x|\rfloor-p}$ (unless the result underflows; see
1057 /// below).
1058 ///
1059 /// If the output has a precision, it is `prec`.
1060 ///
1061 /// Special cases:
1062 /// - $f(\pm1,p)=\pm\infty$
1063 /// - $f(x,p)=\text{NaN}$ if $|x|<1$
1064 ///
1065 /// Overflow and underflow:
1066 /// - The result never overflows: for $|x|>1$ with denominator $d$, $|x| - 1 \geq 1/d$, so
1067 /// $|\operatorname{acoth} x| \leq \frac{1}{2}\ln(1 + 2d)$.
1068 /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1069 /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1070 /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
1071 /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1072 ///
1073 /// Underflow requires $|x| > 2^{2^{30}}$, since $|\operatorname{acoth} x| > 1/|x|$.
1074 ///
1075 /// If you want to use a rounding mode other than `Nearest`, consider using
1076 /// [`Float::acoth_rational_prec_round_ref`] instead.
1077 ///
1078 /// # Worst-case complexity
1079 /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
1080 ///
1081 /// $M(n, m) = O(n \log n + m \log m)$
1082 ///
1083 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1084 /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
1085 /// the input is handled with `Rational` arithmetic.
1086 ///
1087 /// # Panics
1088 /// Panics if `prec` is zero.
1089 ///
1090 /// # Examples
1091 /// ```
1092 /// use malachite_base::num::basic::traits::{One, OneHalf};
1093 /// use malachite_float::Float;
1094 /// use malachite_q::Rational;
1095 /// use std::cmp::Ordering::*;
1096 ///
1097 /// let (c, o) = Float::acoth_rational_prec_ref(&Rational::from(3u32), 5);
1098 /// assert_eq!(c.to_string(), "0.344");
1099 /// assert_eq!(o, Less);
1100 ///
1101 /// let (c, o) = Float::acoth_rational_prec_ref(&Rational::from(3u32), 20);
1102 /// assert_eq!(c.to_string(), "0.34657335");
1103 /// assert_eq!(o, Less);
1104 ///
1105 /// let (c, o) = Float::acoth_rational_prec_ref(&Rational::ONE, 10);
1106 /// assert_eq!(c.to_string(), "Infinity");
1107 /// assert_eq!(o, Equal);
1108 ///
1109 /// let (c, o) = Float::acoth_rational_prec_ref(&Rational::ONE_HALF, 10);
1110 /// assert!(c.is_nan());
1111 /// assert_eq!(o, Equal);
1112 /// ```
1113 #[inline]
1114 pub fn acoth_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
1115 Self::acoth_rational_prec_round_ref(x, prec, Nearest)
1116 }
1117}
1118
1119impl Acoth for Float {
1120 type Output = Self;
1121
1122 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], taking
1123 /// it by value.
1124 ///
1125 /// If the output has a precision, it is the precision of the input. If the inverse hyperbolic
1126 /// cotangent is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1127 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1128 /// the `Nearest` rounding mode.
1129 ///
1130 /// $$
1131 /// f(x) = \operatorname{acoth} x+\varepsilon.
1132 /// $$
1133 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
1134 /// assumed to be 0.
1135 /// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1136 /// |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the precision of the input.
1137 ///
1138 /// Special cases:
1139 /// - $f(\text{NaN})=\text{NaN}$
1140 /// - $f(\infty)=0.0$
1141 /// - $f(-\infty)=-0.0$
1142 /// - $f(\pm1)=\pm\infty$
1143 /// - $f(x)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
1144 ///
1145 /// See the [`Float::acoth_round`] documentation for information on overflow and underflow.
1146 ///
1147 /// If you want to use a rounding mode other than `Nearest`, consider using
1148 /// [`Float::acoth_round`] instead. If you want to specify the output precision, consider using
1149 /// [`Float::acoth_prec`]. If you want both of these things, consider using
1150 /// [`Float::acoth_prec_round`].
1151 ///
1152 /// # Worst-case complexity
1153 /// $T(n) = O(n (\log n)^2 \log\log n)$
1154 ///
1155 /// $M(n) = O(n \log n)$
1156 ///
1157 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1158 ///
1159 /// # Examples
1160 /// ```
1161 /// use malachite_base::num::arithmetic::traits::Acoth;
1162 /// use malachite_base::num::basic::traits::*;
1163 /// use malachite_float::Float;
1164 ///
1165 /// assert!(Float::NAN.acoth().is_nan());
1166 /// assert_eq!(Float::INFINITY.acoth().to_string(), "0.0");
1167 /// assert_eq!(Float::NEGATIVE_INFINITY.acoth().to_string(), "-0.0");
1168 /// assert!(Float::ZERO.acoth().is_nan());
1169 /// assert_eq!(Float::ONE.acoth().to_string(), "Infinity");
1170 /// assert_eq!(Float::NEGATIVE_ONE.acoth().to_string(), "-Infinity");
1171 /// assert_eq!(
1172 /// (Float::one_prec(100) << 1u32).acoth().to_string(),
1173 /// "0.54930614433405484569762261846113"
1174 /// );
1175 /// assert_eq!(
1176 /// (Float::from_unsigned_prec(3u32, 100).0).acoth().to_string(),
1177 /// "0.34657359027997265470861606072899"
1178 /// );
1179 /// assert_eq!(
1180 /// (-(Float::one_prec(100) << 1u32)).acoth().to_string(),
1181 /// "-0.54930614433405484569762261846113"
1182 /// );
1183 /// ```
1184 #[inline]
1185 fn acoth(self) -> Self {
1186 let prec = self.significant_bits();
1187 self.acoth_prec_round(prec, Nearest).0
1188 }
1189}
1190
1191impl Acoth for &Float {
1192 type Output = Float;
1193
1194 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], taking
1195 /// it by reference.
1196 ///
1197 /// If the output has a precision, it is the precision of the input. If the inverse hyperbolic
1198 /// cotangent is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1199 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1200 /// the `Nearest` rounding mode.
1201 ///
1202 /// $$
1203 /// f(x) = \operatorname{acoth} x+\varepsilon.
1204 /// $$
1205 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
1206 /// assumed to be 0.
1207 /// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1208 /// |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the precision of the input.
1209 ///
1210 /// Special cases:
1211 /// - $f(\text{NaN})=\text{NaN}$
1212 /// - $f(\infty)=0.0$
1213 /// - $f(-\infty)=-0.0$
1214 /// - $f(\pm1)=\pm\infty$
1215 /// - $f(x)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
1216 ///
1217 /// See the [`Float::acoth_round`] documentation for information on overflow and underflow.
1218 ///
1219 /// If you want to use a rounding mode other than `Nearest`, consider using
1220 /// [`Float::acoth_round_ref`] instead. If you want to specify the output precision, consider
1221 /// using [`Float::acoth_prec_ref`]. If you want both of these things, consider using
1222 /// [`Float::acoth_prec_round_ref`].
1223 ///
1224 /// # Worst-case complexity
1225 /// $T(n) = O(n (\log n)^2 \log\log n)$
1226 ///
1227 /// $M(n) = O(n \log n)$
1228 ///
1229 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1230 ///
1231 /// # Examples
1232 /// ```
1233 /// use malachite_base::num::arithmetic::traits::Acoth;
1234 /// use malachite_base::num::basic::traits::*;
1235 /// use malachite_float::Float;
1236 ///
1237 /// assert!((&Float::NAN).acoth().is_nan());
1238 /// assert_eq!((&Float::INFINITY).acoth().to_string(), "0.0");
1239 /// assert_eq!((&Float::NEGATIVE_INFINITY).acoth().to_string(), "-0.0");
1240 /// assert!((&Float::ZERO).acoth().is_nan());
1241 /// assert_eq!((&Float::ONE).acoth().to_string(), "Infinity");
1242 /// assert_eq!((&Float::NEGATIVE_ONE).acoth().to_string(), "-Infinity");
1243 /// assert_eq!(
1244 /// (&(Float::one_prec(100) << 1u32)).acoth().to_string(),
1245 /// "0.54930614433405484569762261846113"
1246 /// );
1247 /// assert_eq!(
1248 /// (&Float::from_unsigned_prec(3u32, 100).0)
1249 /// .acoth()
1250 /// .to_string(),
1251 /// "0.34657359027997265470861606072899"
1252 /// );
1253 /// assert_eq!(
1254 /// (&(-(Float::one_prec(100) << 1u32))).acoth().to_string(),
1255 /// "-0.54930614433405484569762261846113"
1256 /// );
1257 /// ```
1258 #[inline]
1259 fn acoth(self) -> Float {
1260 self.acoth_prec_round_ref(self.significant_bits(), Nearest)
1261 .0
1262 }
1263}
1264
1265impl AcothAssign for Float {
1266 /// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Float`], in
1267 /// place.
1268 ///
1269 /// If the output has a precision, it is the precision of the input. If the inverse hyperbolic
1270 /// cotangent is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1271 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1272 /// the `Nearest` rounding mode.
1273 ///
1274 /// $$
1275 /// x \gets \operatorname{acoth} x+\varepsilon.
1276 /// $$
1277 /// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
1278 /// assumed to be 0.
1279 /// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1280 /// |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the precision of the input.
1281 ///
1282 /// See the [`Float::acoth`] documentation for information on special cases, overflow, and
1283 /// underflow.
1284 ///
1285 /// If you want to use a rounding mode other than `Nearest`, consider using
1286 /// [`Float::acoth_round_assign`] instead. If you want to specify the output precision, consider
1287 /// using [`Float::acoth_prec_assign`]. If you want both of these things, consider using
1288 /// [`Float::acoth_prec_round_assign`].
1289 ///
1290 /// # Worst-case complexity
1291 /// $T(n) = O(n (\log n)^2 \log\log n)$
1292 ///
1293 /// $M(n) = O(n \log n)$
1294 ///
1295 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1296 ///
1297 /// # Examples
1298 /// ```
1299 /// use malachite_base::num::arithmetic::traits::AcothAssign;
1300 /// use malachite_base::num::basic::traits::*;
1301 /// use malachite_float::Float;
1302 ///
1303 /// let mut x = Float::NAN;
1304 /// x.acoth_assign();
1305 /// assert!(x.is_nan());
1306 ///
1307 /// let mut x = Float::INFINITY;
1308 /// x.acoth_assign();
1309 /// assert_eq!(x.to_string(), "0.0");
1310 ///
1311 /// let mut x = Float::ZERO;
1312 /// x.acoth_assign();
1313 /// assert!(x.is_nan());
1314 ///
1315 /// let mut x = Float::ONE;
1316 /// x.acoth_assign();
1317 /// assert_eq!(x.to_string(), "Infinity");
1318 ///
1319 /// let mut x = Float::one_prec(100) << 1u32;
1320 /// x.acoth_assign();
1321 /// assert_eq!(x.to_string(), "0.54930614433405484569762261846113");
1322 ///
1323 /// let mut x = Float::from_unsigned_prec(3u32, 100).0;
1324 /// x.acoth_assign();
1325 /// assert_eq!(x.to_string(), "0.34657359027997265470861606072899");
1326 /// ```
1327 #[inline]
1328 fn acoth_assign(&mut self) {
1329 let prec = self.significant_bits();
1330 self.acoth_prec_round_assign(prec, Nearest);
1331 }
1332}
1333
1334/// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a primitive float. Using
1335/// this function is more accurate than using the default `acoth` function or the one provided by
1336/// `libm`.
1337///
1338/// $$
1339/// f(x) = \operatorname{acoth} x+\varepsilon.
1340/// $$
1341/// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed
1342/// to be 0.
1343/// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1344/// |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the precision of the output (24 if `T` is a
1345/// [`f32`] and 53 if `T` is a [`f64`]).
1346///
1347/// Special cases:
1348/// - $f(\text{NaN})=\text{NaN}$
1349/// - $f(\infty)=0.0$
1350/// - $f(-\infty)=-0.0$
1351/// - $f(\pm1)=\pm\infty$
1352/// - $f(x)=\text{NaN}$ if $|x|<1$, including $\pm0.0$
1353///
1354/// Overflow is not possible. The result is subnormal only when $x$ is, and then it is $x$ itself,
1355/// since $|\operatorname{acoth} x - x| < |x|^3/2$.
1356///
1357/// # Worst-case complexity
1358/// Constant time and additional memory.
1359///
1360/// # Examples
1361/// ```
1362/// use malachite_base::num::basic::traits::NegativeInfinity;
1363/// use malachite_base::num::float::NiceFloat;
1364/// use malachite_float::float::arithmetic::acoth::primitive_float_acoth;
1365///
1366/// assert!(primitive_float_acoth(f32::NAN).is_nan());
1367/// assert_eq!(
1368/// NiceFloat(primitive_float_acoth(f32::INFINITY)),
1369/// NiceFloat(0.0)
1370/// );
1371/// assert_eq!(
1372/// NiceFloat(primitive_float_acoth(f32::NEGATIVE_INFINITY)),
1373/// NiceFloat(-0.0)
1374/// );
1375/// assert!(primitive_float_acoth(0.0f32).is_nan());
1376/// assert_eq!(
1377/// NiceFloat(primitive_float_acoth(1.0f32)),
1378/// NiceFloat(f32::INFINITY)
1379/// );
1380/// assert_eq!(
1381/// NiceFloat(primitive_float_acoth(-1.0f32)),
1382/// NiceFloat(f32::NEGATIVE_INFINITY)
1383/// );
1384/// assert_eq!(
1385/// NiceFloat(primitive_float_acoth(2.0f32)),
1386/// NiceFloat(0.54930615)
1387/// );
1388/// assert_eq!(
1389/// NiceFloat(primitive_float_acoth(2.0f64)),
1390/// NiceFloat(0.5493061443340549)
1391/// );
1392/// assert_eq!(
1393/// NiceFloat(primitive_float_acoth(-1.5f64)),
1394/// NiceFloat(-0.8047189562170501)
1395/// );
1396/// ```
1397#[inline]
1398#[allow(clippy::type_repetition_in_bounds)]
1399pub fn primitive_float_acoth<T: PrimitiveFloat>(x: T) -> T
1400where
1401 Float: From<T> + PartialOrd<T>,
1402 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1403{
1404 emulate_float_to_float_fn(Float::acoth_prec, x)
1405}
1406
1407/// Computes $\operatorname{acoth} x$, the inverse hyperbolic cotangent of a [`Rational`], returning
1408/// the result as a primitive float. The result is correctly rounded.
1409///
1410/// $$
1411/// f(x) = \operatorname{acoth} x+\varepsilon.
1412/// $$
1413/// - If $\operatorname{acoth} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed
1414/// to be 0.
1415/// - If $\operatorname{acoth} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1416/// |\operatorname{acoth} x|\rfloor-p}$, where $p$ is the precision of the output (typically 24 if
1417/// `T` is a [`f32`] and 53 if `T` is a [`f64`], but less if the output is subnormal).
1418///
1419/// Special cases:
1420/// - $f(\pm1)=\pm\infty$
1421/// - $f(x)=\text{NaN}$ if $|x|<1$
1422///
1423/// Overflow is not possible. Underflow is: an `x` of large enough magnitude gives `0.0` or `-0.0`.
1424///
1425/// # Worst-case complexity
1426/// $T(m) = O(m (\log m)^2 \log\log m)$
1427///
1428/// $M(m) = O(m \log m)$
1429///
1430/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
1431///
1432/// # Examples
1433/// ```
1434/// use malachite_base::num::basic::traits::{One, Zero};
1435/// use malachite_base::num::float::NiceFloat;
1436/// use malachite_float::float::arithmetic::acoth::primitive_float_acoth_rational;
1437/// use malachite_q::Rational;
1438///
1439/// assert!(primitive_float_acoth_rational::<f64>(&Rational::ZERO).is_nan());
1440/// assert_eq!(
1441/// NiceFloat(primitive_float_acoth_rational::<f64>(&Rational::ONE)),
1442/// NiceFloat(f64::INFINITY)
1443/// );
1444/// assert_eq!(
1445/// NiceFloat(primitive_float_acoth_rational::<f64>(&Rational::from(3u32))),
1446/// NiceFloat(0.34657359027997264)
1447/// );
1448/// ```
1449#[inline]
1450#[allow(clippy::type_repetition_in_bounds)]
1451pub fn primitive_float_acoth_rational<T: PrimitiveFloat>(x: &Rational) -> T
1452where
1453 Float: PartialOrd<T>,
1454 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1455{
1456 emulate_rational_to_float_fn(Float::acoth_rational_prec_ref, x)
1457}