Skip to main content

malachite_float/float/arithmetic/
atanh.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5//      Copyright 2001-2026 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
15use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
16use crate::float::arithmetic::acsch::RECIPROCAL_SAFE_EXPONENT;
17use crate::float::arithmetic::asinh::round_with_error;
18use crate::float::arithmetic::cosh::same_rounding;
19use crate::float::arithmetic::round_near_x::{
20    LEADING_TERM_MIN_EXPONENT, round_rational_leading_term, small_input_shortcut,
21};
22use crate::float::arithmetic::sin::{TINY_UNDERFLOW_EXPONENT, underflowed};
23use crate::{Float, emulate_float_to_float_fn, emulate_rational_to_float_fn};
24use core::cmp::Ordering::{self, *};
25use core::cmp::max;
26use malachite_base::fail_on_untested_path;
27use malachite_base::num::arithmetic::traits::{
28    Abs, Atanh, AtanhAssign, CeilingLogBase2, IsPowerOf2, Ln, Ln1PlusX, Square,
29};
30use malachite_base::num::basic::floats::PrimitiveFloat;
31use malachite_base::num::basic::integers::PrimitiveInt;
32use malachite_base::num::basic::traits::{
33    Infinity as InfinityTrait, NaN as NaNTrait, NegativeInfinity, NegativeZero, One, Two,
34    Zero as ZeroTrait,
35};
36use malachite_base::num::comparison::traits::OrdAbs;
37use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
38use malachite_base::num::logic::traits::SignificantBits;
39use malachite_base::rounding_modes::RoundingMode::{self, *};
40use malachite_nz::platform::Limb;
41use malachite_q::Rational;
42
43// This is mpfr_atanh_small from atanh.c, MPFR 4.2.2. It returns an approximation y of atanh(x), for
44// 0 < x <= 1/2, at precision p, together with k such that the error is bounded by 2^k ulp(y). MPFR
45// uses faithful rounding throughout; `Nearest` is used instead, which only improves the bounds.
46//
47// In the following, theta represents a value with |theta| <= 2^(1-p) (might be a different value
48// each time).
49fn atanh_small(x: &Float, p: u64) -> (Float, u64) {
50    // t = x * (1 + theta)
51    let mut t = Float::from_float_prec_ref(x, p).0;
52    // exact
53    let mut y = t.clone();
54    // x2 = x^2 * (1 + theta)
55    let x2 = x.square_prec_ref(p).0;
56    let p_i64 = i64::exact_from(p);
57    let mut i = 3u64;
58    // i as a `Float`, kept at 64 bits so that every odd i stays exact
59    let mut i_float = Float::from_unsigned_prec(3u32, 64).0;
60    loop {
61        // t = x^i * (1 + theta)^i
62        t *= &x2;
63        // u = x^i/i * (1 + theta)^(i+1)
64        let u = t.div_prec_ref_ref(&i_float, p).0;
65        if u == 0u32 {
66            // MPFR's extended exponent range keeps u from underflowing. Here a u that underflows to
67            // zero is negligible too; this needs x^i to underflow before |u| < ulp(y), and so a
68            // working precision above 2^30.
69            fail_on_untested_path("atanh_small, u underflows");
70            break;
71        }
72        // |u| < ulp(y)
73        if i64::from(u.get_exponent().unwrap()) <= i64::from(y.get_exponent().unwrap()) - p_i64 {
74            break;
75        }
76        // error <= ulp(y)
77        y += u;
78        i += 2;
79        i_float += Float::TWO;
80    }
81    // See algorithms.tex: the total error is bounded by (i+7)/2 ulp(y).
82    let err = (i + 8) >> 1; // ceil((i+7)/2)
83    let k = err.ceiling_log_base_2();
84    // if k + 2 < p, since k = ceil(log2(err)), we have err <= 2^k <= 2^(p-3), thus i+7 <= 2*err <=
85    // 2^(p-2), thus (i+7)*epsilon <= 1/2, which implies our assumption (i+1)*epsilon <= 1/2.
86    assert!(k + 2 < p);
87    (y, k)
88}
89
90// This is mpfr_atanh from atanh.c, MPFR 4.2.2, where the input is finite, nonzero, and less than 1
91// in absolute value. atanh = ln((1+x)/(1-x)) / 2, except when x is very small, in which case atanh
92// = x + tiny error, and when x is small, where the Taylor expansion is used directly.
93//
94// Outside the small case this departs from MPFR, which computes ln((1+x)/(1-x)): taking the
95// logarithm of a quotient near 1 loses about -EXP(atanh x) bits, which MPFR's error estimate
96// charges to the working precision and which often forces a retry. ln(1 + 2x/(1-x)), with
97// `ln_1_plus_x`, has a constant error bound instead; it was benchmarked as never slower and 2 to 4
98// times faster for |x| < 1/4.
99//
100// MPFR computes (1+x)/(1-x) in an extended exponent range. Here 2x/(1-x) overflows once 1 - x is
101// below about 2^(1 - MAX_EXPONENT), which needs an x with a precision above 2^30; for such an x the
102// logarithm is instead computed as ln(1+x) - ln(1-x).
103fn atanh_prec_round_normal_ref(xt: &Float, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
104    assert_ne!(rm, Exact, "Inexact atanh");
105    let exp_xt = i64::from(xt.get_exponent().unwrap());
106    // atanh(x) = x + x^3/3 + ... so the error is < 2^(3*EXP(x)-1)
107    if let Some(result) = small_input_shortcut(xt, -(exp_xt << 1), 1, true, prec, rm) {
108        return result;
109    }
110    let negative = *xt < 0u32;
111    let x = xt.abs();
112    // Compute initial precision
113    let nt = max(x.get_prec().unwrap(), prec);
114    let mut working_prec = nt + nt.ceiling_log_base_2() + 4;
115    let mut increment = Limb::WIDTH;
116    // small case: assuming the AGM algorithm used by ln uses log2(p) steps for a precision of p
117    // bits, try the special variant whenever EXP(x) <= -p/log2(p). The +1 avoids a division by 0
118    // when prec = 1. This implies EXP(x) <= -1, thus x < 1/2.
119    let k = 1 + prec.ceiling_log_base_2();
120    let small = exp_xt <= -1 - i64::exact_from(prec / k);
121    loop {
122        let (t, err) = if small {
123            let (t, k) = atanh_small(&x, working_prec);
124            (t, i64::exact_from(k))
125        } else {
126            // (1-x) with x = |xt|
127            let te = Float::ONE
128                .sub_prec_round_val_ref(&x, working_prec, Ceiling)
129                .0;
130            if i64::from(te.get_exponent().unwrap()) < RECIPROCAL_SAFE_EXPONENT {
131                fail_on_untested_path("atanh_prec_round_normal_ref, (1+x)/(1-x) may overflow");
132                // ln(1+x) - ln(1-x), halved
133                let t = (x
134                    .add_prec_round_ref_val(Float::ONE, working_prec, Floor)
135                    .0
136                    .ln()
137                    - te.ln())
138                    >> 1u32;
139                // error estimate: see algorithms.tex
140                let err = max(4 - i64::from(t.get_exponent().unwrap()), 0) + 1;
141                (t, err)
142            } else {
143                // ln(1 + u) with u = 2x/(1-x), halved. u has relative error below 3 * 2^-wp
144                // (2^(1-wp) from 1 - x rounded up, 2^-wp from the division). Since ln(1+u) >=
145                // u/(1+u), that moves ln(1+u) by less than 3.02 * 2^-wp ln(1+u), about 3 ulps, and
146                // its own rounding adds 1/2 ulp: under 2^3 ulps with room for an exponent boundary.
147                let t = (&x << 1u32).div_prec(te, working_prec).0.ln_1_plus_x() >> 1u32;
148                (t, 3)
149            }
150        };
151        // MPFR also stops on a zero t, which cannot occur here: |x| is at least about
152        // 2^(-prec/log2(prec)) outside the small case, and atanh_small returns at least x.
153        if let Some(result) =
154            round_with_error(if negative { -t } else { t }, working_prec, err, prec, rm)
155        {
156            return result;
157        }
158        // reactualisation of the precision
159        working_prec += increment;
160        increment = working_prec >> 1;
161    }
162}
163
164// Computes atanh(x) for a nonzero `Rational` x with |x| <= 1/2 from the series x + x^3/3 + x^5/5 +
165// ..., whose terms have the sign of x. With S the sum of the terms through x^k/k, the remaining
166// terms sum to less than |x|^(k+2)/((k+2)(1 - x^2)) in magnitude, so S and S plus that bound
167// bracket atanh(x); terms are added until the two ends round alike.
168fn atanh_series(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
169    let x2 = x.square();
170    let one_minus_x2 = Rational::ONE - &x2;
171    let mut power = x.clone(); // x^k
172    let mut sum = x.clone();
173    let mut k = 1u64;
174    loop {
175        power *= &x2;
176        k += 2;
177        let bound = &power / (Rational::from(k) * &one_minus_x2);
178        if let Some(result) = same_rounding(
179            Float::from_rational_prec_round_ref(&sum, prec, rm),
180            Float::from_rational_prec_round(&sum + bound, prec, rm),
181        ) {
182            return result;
183        }
184        sum += &power / Rational::from(k);
185    }
186}
187
188// Computes atanh(x) for a `Rational` x with 0 < |x| < 1, rounded to precision `prec` with rounding
189// mode `rm`. The result is never exactly representable, so `rm` must not be `Exact`.
190fn atanh_rational_helper(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
191    assert_ne!(rm, Exact, "Inexact atanh");
192    let positive = *x > 0u32;
193    let exp_x = x.floor_log_base_2_abs() + 1; // the MPFR-style exponent of x
194    if exp_x < TINY_UNDERFLOW_EXPONENT {
195        // |x| < 2^(MIN_EXPONENT - 3), so |atanh(x)| < |x| (1 + x^2) is below 2^(MIN_EXPONENT - 2),
196        // half the smallest positive Float, and the result is zero or that Float, by the rounding
197        // mode alone
198        return underflowed(positive, prec, rm);
199    }
200    // atanh(x) = x(1 + x^2/3 + ...), so |x| falls short of |atanh x| by less than 2^(3 EXP(x) - 1),
201    // as for `tan_rational`; once that is below the distance from x to the nearest (prec + 1)-bit
202    // dyadic other than x itself, x's own rounding, nudged away from zero, is the answer.
203    if exp_x > LEADING_TERM_MIN_EXPONENT
204        && -(exp_x << 1) > i64::exact_from(prec + x.denominator_ref().significant_bits()) + 4
205    {
206        return round_rational_leading_term(x.abs(), positive, true, prec, rm);
207    }
208    // For a small x the series converges by a factor of x^2 per term. It needs about T = prec/(2
209    // |EXP(x)|) terms, whose exact `Rational`s grow by about the D bits of x's denominator each, so
210    // it costs about T^2 (D + 64) against about prec log2(prec) for the logarithm. Benchmarks put
211    // the crossover at EXP(x)^2 * 12 (1 + log2(prec)) = prec (D + 64). The series also takes the
212    // inputs at the bottom of the exponent range, which the halving below could push out of it.
213    if exp_x < 0
214        && (u128::from(exp_x.unsigned_abs()).pow(2)
215            * 12
216            * u128::from(1 + prec.ceiling_log_base_2())
217            > u128::from(prec)
218                .saturating_mul(u128::from(x.denominator_ref().significant_bits()) + 64)
219            || exp_x <= LEADING_TERM_MIN_EXPONENT)
220    {
221        return atanh_series(x, prec, rm);
222    }
223    atanh_rational_via_ln(x, prec, rm)
224}
225
226// Computes atanh(x) for a `Rational` x with 0 < |x| < 1 whose result is far above the bottom of the
227// exponent range. For x = n/d, atanh(x) = ln((1 + x)/(1 - x))/2 = ln((d + n)/(d - n))/2, and the
228// quotient is an exact `Rational`, built straight from the numerator and denominator. Halving the
229// correctly rounded logarithm is then exact, so it gives the correctly rounded result.
230fn atanh_rational_via_ln(x: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
231    let n = x.numerator_ref();
232    let d = x.denominator_ref();
233    let (sum, difference) = (d + n, d - n);
234    let q = if *x > 0u32 {
235        Rational::from_naturals(sum, difference)
236    } else {
237        Rational::from_naturals(difference, sum)
238    };
239    let (y, o) = Float::ln_rational_prec_round(q, prec, rm);
240    (y >> 1u32, o)
241}
242
243impl Float {
244    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
245    /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
246    /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded inverse
247    /// hyperbolic tangent is less than, equal to, or greater than the exact inverse hyperbolic
248    /// tangent. Although `NaN`s are not comparable to any [`Float`], whenever this function returns
249    /// a `NaN` it also returns `Equal`.
250    ///
251    /// See [`RoundingMode`] for a description of the possible rounding modes.
252    ///
253    /// $$
254    /// f(x,p,m) = \operatorname{atanh} x+\varepsilon.
255    /// $$
256    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
257    ///   assumed to be 0.
258    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
259    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$.
260    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
261    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$.
262    ///
263    /// If the output has a precision, it is `prec`.
264    ///
265    /// Special cases:
266    /// - $f(\text{NaN},p,m)=\text{NaN}$
267    /// - $f(\pm\infty,p,m)=\text{NaN}$
268    /// - $f(\pm0.0,p,m)=\pm0.0$
269    /// - $f(\pm1,p,m)=\pm\infty$
270    /// - $f(x,p,m)=\text{NaN}$ if $|x|>1$
271    ///
272    /// The result never overflows or underflows: for a nonzero $x$ with $|x|<1$, $|x| \leq
273    /// |\operatorname{atanh} x| \leq \frac{q+1}{2}\ln 2$, where $q$ is the precision of $x$.
274    ///
275    /// If you know you'll be using `Nearest`, consider using [`Float::atanh_prec`] instead. If you
276    /// know that your target precision is the precision of the input, consider using
277    /// [`Float::atanh_round`] instead. If both of these things are true, consider using
278    /// [`Float::atanh`] instead.
279    ///
280    /// # Worst-case complexity
281    /// $T(n) = O(n (\log n)^2 \log\log n)$
282    ///
283    /// $M(n) = O(n \log n)$
284    ///
285    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
286    /// self.significant_bits())`.
287    ///
288    /// # Panics
289    /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
290    /// since the inverse hyperbolic tangent of such a [`Float`] is never exactly representable, or
291    /// if `prec` is zero.
292    ///
293    /// # Examples
294    /// ```
295    /// use malachite_base::rounding_modes::RoundingMode::*;
296    /// use malachite_float::Float;
297    /// use std::cmp::Ordering::*;
298    ///
299    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round(5, Floor);
300    /// assert_eq!(c.to_string(), "0.531");
301    /// assert_eq!(o, Less);
302    ///
303    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round(5, Ceiling);
304    /// assert_eq!(c.to_string(), "0.562");
305    /// assert_eq!(o, Greater);
306    ///
307    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round(5, Nearest);
308    /// assert_eq!(c.to_string(), "0.562");
309    /// assert_eq!(o, Greater);
310    ///
311    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round(20, Floor);
312    /// assert_eq!(c.to_string(), "0.54930592");
313    /// assert_eq!(o, Less);
314    ///
315    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round(20, Ceiling);
316    /// assert_eq!(c.to_string(), "0.54930687");
317    /// assert_eq!(o, Greater);
318    ///
319    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round(20, Nearest);
320    /// assert_eq!(c.to_string(), "0.54930592");
321    /// assert_eq!(o, Less);
322    /// ```
323    #[inline]
324    pub fn atanh_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
325        self.atanh_prec_round_ref(prec, rm)
326    }
327
328    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
329    /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
330    /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded inverse
331    /// hyperbolic tangent is less than, equal to, or greater than the exact inverse hyperbolic
332    /// tangent. Although `NaN`s are not comparable to any [`Float`], whenever this function returns
333    /// a `NaN` it also returns `Equal`.
334    ///
335    /// See [`RoundingMode`] for a description of the possible rounding modes.
336    ///
337    /// $$
338    /// f(x,p,m) = \operatorname{atanh} x+\varepsilon.
339    /// $$
340    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
341    ///   assumed to be 0.
342    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
343    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$.
344    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
345    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$.
346    ///
347    /// If the output has a precision, it is `prec`.
348    ///
349    /// Special cases:
350    /// - $f(\text{NaN},p,m)=\text{NaN}$
351    /// - $f(\pm\infty,p,m)=\text{NaN}$
352    /// - $f(\pm0.0,p,m)=\pm0.0$
353    /// - $f(\pm1,p,m)=\pm\infty$
354    /// - $f(x,p,m)=\text{NaN}$ if $|x|>1$
355    ///
356    /// The result never overflows or underflows: for a nonzero $x$ with $|x|<1$, $|x| \leq
357    /// |\operatorname{atanh} x| \leq \frac{q+1}{2}\ln 2$, where $q$ is the precision of $x$.
358    ///
359    /// If you know you'll be using `Nearest`, consider using [`Float::atanh_prec_ref`] instead. If
360    /// you know that your target precision is the precision of the input, consider using
361    /// [`Float::atanh_round_ref`] instead. If both of these things are true, consider using
362    /// `(&Float).atanh()` instead.
363    ///
364    /// # Worst-case complexity
365    /// $T(n) = O(n (\log n)^2 \log\log n)$
366    ///
367    /// $M(n) = O(n \log n)$
368    ///
369    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
370    /// self.significant_bits())`.
371    ///
372    /// # Panics
373    /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
374    /// since the inverse hyperbolic tangent of such a [`Float`] is never exactly representable, or
375    /// if `prec` is zero.
376    ///
377    /// # Examples
378    /// ```
379    /// use malachite_base::rounding_modes::RoundingMode::*;
380    /// use malachite_float::Float;
381    /// use std::cmp::Ordering::*;
382    ///
383    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round_ref(5, Floor);
384    /// assert_eq!(c.to_string(), "0.531");
385    /// assert_eq!(o, Less);
386    ///
387    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round_ref(5, Ceiling);
388    /// assert_eq!(c.to_string(), "0.562");
389    /// assert_eq!(o, Greater);
390    ///
391    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round_ref(5, Nearest);
392    /// assert_eq!(c.to_string(), "0.562");
393    /// assert_eq!(o, Greater);
394    ///
395    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round_ref(20, Floor);
396    /// assert_eq!(c.to_string(), "0.54930592");
397    /// assert_eq!(o, Less);
398    ///
399    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round_ref(20, Ceiling);
400    /// assert_eq!(c.to_string(), "0.54930687");
401    /// assert_eq!(o, Greater);
402    ///
403    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_round_ref(20, Nearest);
404    /// assert_eq!(c.to_string(), "0.54930592");
405    /// assert_eq!(o, Less);
406    /// ```
407    pub fn atanh_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
408        assert_ne!(prec, 0);
409        match &self.0 {
410            // atanh(NaN) = NaN, and atanh(+/-Inf) = NaN since tanh gives a result between -1 and 1
411            NaN | Infinity { .. } => (Self::NAN, Equal),
412            // atanh(+/-0) = +/-0
413            Zero { sign } => (
414                if *sign {
415                    Self::ZERO
416                } else {
417                    Self::NEGATIVE_ZERO
418                },
419                Equal,
420            ),
421            // atanh(x) = NaN as soon as |x| > 1, and atanh(+/-1) = +/-Inf
422            Finite {
423                sign,
424                exponent,
425                significand,
426                ..
427            } if *exponent > 0 => {
428                if *exponent == 1 && significand.is_power_of_2() {
429                    (
430                        if *sign {
431                            Self::INFINITY
432                        } else {
433                            Self::NEGATIVE_INFINITY
434                        },
435                        Equal,
436                    )
437                } else {
438                    (Self::NAN, Equal)
439                }
440            }
441            Finite { .. } => atanh_prec_round_normal_ref(self, prec, rm),
442        }
443    }
444
445    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
446    /// the result to the nearest value of the specified precision. The [`Float`] is taken by value.
447    /// An [`Ordering`] is also returned, indicating whether the rounded inverse hyperbolic tangent
448    /// is less than, equal to, or greater than the exact inverse hyperbolic tangent. Although
449    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
450    /// returns `Equal`.
451    ///
452    /// If the inverse hyperbolic tangent is equidistant from two [`Float`]s with the specified
453    /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
454    /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
455    ///
456    /// $$
457    /// f(x,p) = \operatorname{atanh} x+\varepsilon.
458    /// $$
459    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
460    ///   assumed to be 0.
461    /// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
462    ///   |\operatorname{atanh} x|\rfloor-p}$.
463    ///
464    /// If the output has a precision, it is `prec`.
465    ///
466    /// Special cases:
467    /// - $f(\text{NaN},p)=\text{NaN}$
468    /// - $f(\pm\infty,p)=\text{NaN}$
469    /// - $f(\pm0.0,p)=\pm0.0$
470    /// - $f(\pm1,p)=\pm\infty$
471    /// - $f(x,p)=\text{NaN}$ if $|x|>1$
472    ///
473    /// The result never overflows or underflows: for a nonzero $x$ with $|x|<1$, $|x| \leq
474    /// |\operatorname{atanh} x| \leq \frac{q+1}{2}\ln 2$, where $q$ is the precision of $x$.
475    ///
476    /// If you want to use a rounding mode other than `Nearest`, consider using
477    /// [`Float::atanh_prec_round`] instead. If you know that your target precision is the precision
478    /// of the input, consider using [`Float::atanh`] instead.
479    ///
480    /// # Worst-case complexity
481    /// $T(n) = O(n (\log n)^2 \log\log n)$
482    ///
483    /// $M(n) = O(n \log n)$
484    ///
485    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
486    /// self.significant_bits())`.
487    ///
488    /// # Panics
489    /// Panics if `prec` is zero.
490    ///
491    /// # Examples
492    /// ```
493    /// use malachite_float::Float;
494    /// use std::cmp::Ordering::*;
495    ///
496    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec(5);
497    /// assert_eq!(c.to_string(), "0.562");
498    /// assert_eq!(o, Greater);
499    ///
500    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec(20);
501    /// assert_eq!(c.to_string(), "0.54930592");
502    /// assert_eq!(o, Less);
503    /// ```
504    #[inline]
505    pub fn atanh_prec(self, prec: u64) -> (Self, Ordering) {
506        self.atanh_prec_round(prec, Nearest)
507    }
508
509    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
510    /// the result to the nearest value of the specified precision. The [`Float`] is taken by
511    /// reference. An [`Ordering`] is also returned, indicating whether the rounded inverse
512    /// hyperbolic tangent is less than, equal to, or greater than the exact inverse hyperbolic
513    /// tangent. Although `NaN`s are not comparable to any [`Float`], whenever this function returns
514    /// a `NaN` it also returns `Equal`.
515    ///
516    /// If the inverse hyperbolic tangent is equidistant from two [`Float`]s with the specified
517    /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
518    /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
519    ///
520    /// $$
521    /// f(x,p) = \operatorname{atanh} x+\varepsilon.
522    /// $$
523    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
524    ///   assumed to be 0.
525    /// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
526    ///   |\operatorname{atanh} x|\rfloor-p}$.
527    ///
528    /// If the output has a precision, it is `prec`.
529    ///
530    /// Special cases:
531    /// - $f(\text{NaN},p)=\text{NaN}$
532    /// - $f(\pm\infty,p)=\text{NaN}$
533    /// - $f(\pm0.0,p)=\pm0.0$
534    /// - $f(\pm1,p)=\pm\infty$
535    /// - $f(x,p)=\text{NaN}$ if $|x|>1$
536    ///
537    /// The result never overflows or underflows: for a nonzero $x$ with $|x|<1$, $|x| \leq
538    /// |\operatorname{atanh} x| \leq \frac{q+1}{2}\ln 2$, where $q$ is the precision of $x$.
539    ///
540    /// If you want to use a rounding mode other than `Nearest`, consider using
541    /// [`Float::atanh_prec_round_ref`] instead. If you know that your target precision is the
542    /// precision of the input, consider using `(&Float).atanh()` instead.
543    ///
544    /// # Worst-case complexity
545    /// $T(n) = O(n (\log n)^2 \log\log n)$
546    ///
547    /// $M(n) = O(n \log n)$
548    ///
549    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
550    /// self.significant_bits())`.
551    ///
552    /// # Panics
553    /// Panics if `prec` is zero.
554    ///
555    /// # Examples
556    /// ```
557    /// use malachite_float::Float;
558    /// use std::cmp::Ordering::*;
559    ///
560    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_ref(5);
561    /// assert_eq!(c.to_string(), "0.562");
562    /// assert_eq!(o, Greater);
563    ///
564    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_prec_ref(20);
565    /// assert_eq!(c.to_string(), "0.54930592");
566    /// assert_eq!(o, Less);
567    /// ```
568    #[inline]
569    pub fn atanh_prec_ref(&self, prec: u64) -> (Self, Ordering) {
570        self.atanh_prec_round_ref(prec, Nearest)
571    }
572
573    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
574    /// the result with the specified rounding mode. The [`Float`] is taken by value. An
575    /// [`Ordering`] is also returned, indicating whether the rounded inverse hyperbolic tangent is
576    /// less than, equal to, or greater than the exact inverse hyperbolic tangent. Although `NaN`s
577    /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
578    /// `Equal`.
579    ///
580    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
581    /// description of the possible rounding modes.
582    ///
583    /// $$
584    /// f(x,m) = \operatorname{atanh} x+\varepsilon.
585    /// $$
586    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
587    ///   assumed to be 0.
588    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
589    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$, where $p$ is the
590    ///   precision of the input.
591    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
592    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the
593    ///   precision of the input.
594    ///
595    /// If the output has a precision, it is the precision of the input.
596    ///
597    /// Special cases:
598    /// - $f(\text{NaN},m)=\text{NaN}$
599    /// - $f(\pm\infty,m)=\text{NaN}$
600    /// - $f(\pm0.0,m)=\pm0.0$
601    /// - $f(\pm1,m)=\pm\infty$
602    /// - $f(x,m)=\text{NaN}$ if $|x|>1$
603    ///
604    /// The result never overflows or underflows: for a nonzero $x$ with $|x|<1$, $|x| \leq
605    /// |\operatorname{atanh} x| \leq \frac{q+1}{2}\ln 2$, where $q$ is the precision of $x$.
606    ///
607    /// If you want to specify an output precision, consider using [`Float::atanh_prec_round`]
608    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
609    /// [`Float::atanh`] instead.
610    ///
611    /// # Worst-case complexity
612    /// $T(n) = O(n (\log n)^2 \log\log n)$
613    ///
614    /// $M(n) = O(n \log n)$
615    ///
616    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
617    ///
618    /// # Panics
619    /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
620    /// since the inverse hyperbolic tangent of such a [`Float`] is never exactly representable.
621    ///
622    /// # Examples
623    /// ```
624    /// use malachite_base::rounding_modes::RoundingMode::*;
625    /// use malachite_float::Float;
626    /// use std::cmp::Ordering::*;
627    ///
628    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_round(Floor);
629    /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
630    /// assert_eq!(o, Less);
631    ///
632    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_round(Ceiling);
633    /// assert_eq!(c.to_string(), "0.54930614433405484569762261846192");
634    /// assert_eq!(o, Greater);
635    ///
636    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_round(Nearest);
637    /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
638    /// assert_eq!(o, Less);
639    /// ```
640    #[inline]
641    pub fn atanh_round(self, rm: RoundingMode) -> (Self, Ordering) {
642        let prec = self.significant_bits();
643        self.atanh_prec_round(prec, rm)
644    }
645
646    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
647    /// the result with the specified rounding mode. The [`Float`] is taken by reference. An
648    /// [`Ordering`] is also returned, indicating whether the rounded inverse hyperbolic tangent is
649    /// less than, equal to, or greater than the exact inverse hyperbolic tangent. Although `NaN`s
650    /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
651    /// `Equal`.
652    ///
653    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
654    /// description of the possible rounding modes.
655    ///
656    /// $$
657    /// f(x,m) = \operatorname{atanh} x+\varepsilon.
658    /// $$
659    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
660    ///   assumed to be 0.
661    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
662    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$, where $p$ is the
663    ///   precision of the input.
664    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
665    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the
666    ///   precision of the input.
667    ///
668    /// If the output has a precision, it is the precision of the input.
669    ///
670    /// Special cases:
671    /// - $f(\text{NaN},m)=\text{NaN}$
672    /// - $f(\pm\infty,m)=\text{NaN}$
673    /// - $f(\pm0.0,m)=\pm0.0$
674    /// - $f(\pm1,m)=\pm\infty$
675    /// - $f(x,m)=\text{NaN}$ if $|x|>1$
676    ///
677    /// The result never overflows or underflows: for a nonzero $x$ with $|x|<1$, $|x| \leq
678    /// |\operatorname{atanh} x| \leq \frac{q+1}{2}\ln 2$, where $q$ is the precision of $x$.
679    ///
680    /// If you want to specify an output precision, consider using [`Float::atanh_prec_round_ref`]
681    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
682    /// `(&Float).atanh()` instead.
683    ///
684    /// # Worst-case complexity
685    /// $T(n) = O(n (\log n)^2 \log\log n)$
686    ///
687    /// $M(n) = O(n \log n)$
688    ///
689    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
690    ///
691    /// # Panics
692    /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
693    /// since the inverse hyperbolic tangent of such a [`Float`] is never exactly representable.
694    ///
695    /// # Examples
696    /// ```
697    /// use malachite_base::rounding_modes::RoundingMode::*;
698    /// use malachite_float::Float;
699    /// use std::cmp::Ordering::*;
700    ///
701    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_round_ref(Floor);
702    /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
703    /// assert_eq!(o, Less);
704    ///
705    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_round_ref(Ceiling);
706    /// assert_eq!(c.to_string(), "0.54930614433405484569762261846192");
707    /// assert_eq!(o, Greater);
708    ///
709    /// let (c, o) = (Float::one_prec(100) >> 1u32).atanh_round_ref(Nearest);
710    /// assert_eq!(c.to_string(), "0.54930614433405484569762261846113");
711    /// assert_eq!(o, Less);
712    /// ```
713    #[inline]
714    pub fn atanh_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
715        self.atanh_prec_round_ref(self.significant_bits(), rm)
716    }
717
718    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
719    /// the result to the specified precision and with the specified rounding mode. The [`Float`] is
720    /// replaced by the result, and an [`Ordering`] is returned, indicating whether the rounded
721    /// inverse hyperbolic tangent is less than, equal to, or greater than the exact inverse
722    /// hyperbolic tangent. Although `NaN`s are not comparable to any [`Float`], whenever this
723    /// function sets a `NaN` it also returns `Equal`.
724    ///
725    /// See [`RoundingMode`] for a description of the possible rounding modes.
726    ///
727    /// $$
728    /// x \gets \operatorname{atanh} x+\varepsilon.
729    /// $$
730    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
731    ///   assumed to be 0.
732    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
733    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$.
734    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
735    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$.
736    ///
737    /// If the output has a precision, it is `prec`.
738    ///
739    /// See the [`Float::atanh_prec_round`] documentation for information on special cases,
740    /// overflow, and underflow.
741    ///
742    /// If you know you'll be using `Nearest`, consider using [`Float::atanh_prec_assign`] instead.
743    /// If you know that your target precision is the precision of the input, consider using
744    /// [`Float::atanh_round_assign`] instead. If both of these things are true, consider using
745    /// [`Float::atanh_assign`] instead.
746    ///
747    /// # Worst-case complexity
748    /// $T(n) = O(n (\log n)^2 \log\log n)$
749    ///
750    /// $M(n) = O(n \log n)$
751    ///
752    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
753    /// self.significant_bits())`.
754    ///
755    /// # Panics
756    /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
757    /// since the inverse hyperbolic tangent of such a [`Float`] is never exactly representable, or
758    /// if `prec` is zero.
759    ///
760    /// # Examples
761    /// ```
762    /// use malachite_base::rounding_modes::RoundingMode::*;
763    /// use malachite_float::Float;
764    /// use std::cmp::Ordering::*;
765    ///
766    /// let mut x = Float::one_prec(100) >> 1u32;
767    /// assert_eq!(x.atanh_prec_round_assign(5, Floor), Less);
768    /// assert_eq!(x.to_string(), "0.531");
769    ///
770    /// let mut x = Float::one_prec(100) >> 1u32;
771    /// assert_eq!(x.atanh_prec_round_assign(5, Ceiling), Greater);
772    /// assert_eq!(x.to_string(), "0.562");
773    ///
774    /// let mut x = Float::one_prec(100) >> 1u32;
775    /// assert_eq!(x.atanh_prec_round_assign(5, Nearest), Greater);
776    /// assert_eq!(x.to_string(), "0.562");
777    ///
778    /// let mut x = Float::one_prec(100) >> 1u32;
779    /// assert_eq!(x.atanh_prec_round_assign(20, Floor), Less);
780    /// assert_eq!(x.to_string(), "0.54930592");
781    ///
782    /// let mut x = Float::one_prec(100) >> 1u32;
783    /// assert_eq!(x.atanh_prec_round_assign(20, Ceiling), Greater);
784    /// assert_eq!(x.to_string(), "0.54930687");
785    ///
786    /// let mut x = Float::one_prec(100) >> 1u32;
787    /// assert_eq!(x.atanh_prec_round_assign(20, Nearest), Less);
788    /// assert_eq!(x.to_string(), "0.54930592");
789    /// ```
790    #[inline]
791    pub fn atanh_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
792        let o;
793        (*self, o) = self.atanh_prec_round_ref(prec, rm);
794        o
795    }
796
797    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
798    /// the result to the nearest value of the specified precision. The [`Float`] is replaced by the
799    /// result, and an [`Ordering`] is returned, indicating whether the rounded inverse hyperbolic
800    /// tangent is less than, equal to, or greater than the exact inverse hyperbolic tangent.
801    /// Although `NaN`s are not comparable to any [`Float`], whenever this function sets a `NaN` it
802    /// also returns `Equal`.
803    ///
804    /// If the inverse hyperbolic tangent is equidistant from two [`Float`]s with the specified
805    /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
806    /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
807    ///
808    /// $$
809    /// x \gets \operatorname{atanh} x+\varepsilon.
810    /// $$
811    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
812    ///   assumed to be 0.
813    /// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
814    ///   |\operatorname{atanh} x|\rfloor-p}$.
815    ///
816    /// If the output has a precision, it is `prec`.
817    ///
818    /// See the [`Float::atanh_prec`] documentation for information on special cases, overflow, and
819    /// underflow.
820    ///
821    /// If you want to use a rounding mode other than `Nearest`, consider using
822    /// [`Float::atanh_prec_round_assign`] instead. If you know that your target precision is the
823    /// precision of the input, consider using [`Float::atanh_assign`] instead.
824    ///
825    /// # Worst-case complexity
826    /// $T(n) = O(n (\log n)^2 \log\log n)$
827    ///
828    /// $M(n) = O(n \log n)$
829    ///
830    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec,
831    /// self.significant_bits())`.
832    ///
833    /// # Panics
834    /// Panics if `prec` is zero.
835    ///
836    /// # Examples
837    /// ```
838    /// use malachite_float::Float;
839    /// use std::cmp::Ordering::*;
840    ///
841    /// let mut x = Float::one_prec(100) >> 1u32;
842    /// assert_eq!(x.atanh_prec_assign(5), Greater);
843    /// assert_eq!(x.to_string(), "0.562");
844    ///
845    /// let mut x = Float::one_prec(100) >> 1u32;
846    /// assert_eq!(x.atanh_prec_assign(20), Less);
847    /// assert_eq!(x.to_string(), "0.54930592");
848    /// ```
849    #[inline]
850    pub fn atanh_prec_assign(&mut self, prec: u64) -> Ordering {
851        self.atanh_prec_round_assign(prec, Nearest)
852    }
853
854    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], rounding
855    /// the result with the specified rounding mode. The [`Float`] is replaced by the result, and an
856    /// [`Ordering`] is returned, indicating whether the rounded inverse hyperbolic tangent is less
857    /// than, equal to, or greater than the exact inverse hyperbolic tangent. Although `NaN`s are
858    /// not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
859    /// `Equal`.
860    ///
861    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
862    /// description of the possible rounding modes.
863    ///
864    /// $$
865    /// x \gets \operatorname{atanh} x+\varepsilon.
866    /// $$
867    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
868    ///   assumed to be 0.
869    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
870    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$, where $p$ is the
871    ///   precision of the input.
872    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
873    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the
874    ///   precision of the input.
875    ///
876    /// If the output has a precision, it is the precision of the input.
877    ///
878    /// See the [`Float::atanh_round`] documentation for information on special cases, overflow, and
879    /// underflow.
880    ///
881    /// If you want to specify an output precision, consider using
882    /// [`Float::atanh_prec_round_assign`] instead. If you know you'll be using the `Nearest`
883    /// rounding mode, consider using [`Float::atanh_assign`] instead.
884    ///
885    /// # Worst-case complexity
886    /// $T(n) = O(n (\log n)^2 \log\log n)$
887    ///
888    /// $M(n) = O(n \log n)$
889    ///
890    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
891    ///
892    /// # Panics
893    /// Panics if `rm` is `Exact` and `self` is finite, nonzero, and less than 1 in absolute value,
894    /// since the inverse hyperbolic tangent of such a [`Float`] is never exactly representable.
895    ///
896    /// # Examples
897    /// ```
898    /// use malachite_base::rounding_modes::RoundingMode::*;
899    /// use malachite_float::Float;
900    /// use std::cmp::Ordering::*;
901    ///
902    /// let mut x = Float::one_prec(100) >> 1u32;
903    /// assert_eq!(x.atanh_round_assign(Floor), Less);
904    /// assert_eq!(x.to_string(), "0.54930614433405484569762261846113");
905    ///
906    /// let mut x = Float::one_prec(100) >> 1u32;
907    /// assert_eq!(x.atanh_round_assign(Ceiling), Greater);
908    /// assert_eq!(x.to_string(), "0.54930614433405484569762261846192");
909    ///
910    /// let mut x = Float::one_prec(100) >> 1u32;
911    /// assert_eq!(x.atanh_round_assign(Nearest), Less);
912    /// assert_eq!(x.to_string(), "0.54930614433405484569762261846113");
913    /// ```
914    #[inline]
915    pub fn atanh_round_assign(&mut self, rm: RoundingMode) -> Ordering {
916        let prec = self.significant_bits();
917        self.atanh_prec_round_assign(prec, rm)
918    }
919
920    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Rational`],
921    /// rounding the result to the specified precision and with the specified rounding mode and
922    /// returning the result as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is
923    /// also returned, indicating whether the rounded inverse hyperbolic tangent is less than, equal
924    /// to, or greater than the exact inverse hyperbolic tangent. Although `NaN`s are not comparable
925    /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
926    ///
927    /// See [`RoundingMode`] for a description of the possible rounding modes.
928    ///
929    /// $$
930    /// f(x,p,m) = \operatorname{atanh} x+\varepsilon.
931    /// $$
932    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
933    ///   assumed to be 0.
934    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
935    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$.
936    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
937    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$.
938    ///
939    /// These bounds do not apply when the result underflows; see below.
940    ///
941    /// If the output has a precision, it is `prec`.
942    ///
943    /// Special cases:
944    /// - $f(0,p,m)=0.0$
945    /// - $f(\pm1,p,m)=\pm\infty$
946    /// - $f(x,p,m)=\text{NaN}$ if $|x|>1$
947    ///
948    /// Overflow and underflow:
949    /// - The result never overflows: for $|x|<1$ with denominator $d$, $|\operatorname{atanh} x|
950    ///   \leq \frac{1}{2}\ln 2d$.
951    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
952    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
953    ///   instead.
954    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
955    /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
956    ///   instead.
957    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
958    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
959    ///   instead.
960    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
961    /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
962    ///   returned instead.
963    ///
964    /// Underflow requires an $x$ of magnitude below $2^{-2^{30}}$, the smallest positive [`Float`],
965    /// since $|\operatorname{atanh} x| > |x|$ for nonzero $x$.
966    ///
967    /// If you know you'll be using `Nearest`, consider using [`Float::atanh_rational_prec`]
968    /// instead.
969    ///
970    /// # Worst-case complexity
971    /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
972    ///
973    /// $M(n, m) = O(n \log n + m \log m)$
974    ///
975    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
976    /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
977    /// the input is handled with `Rational` arithmetic.
978    ///
979    /// # Panics
980    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
981    /// with the given precision (which is the case for every nonzero $x$ with $|x|<1$).
982    ///
983    /// # Examples
984    /// ```
985    /// use malachite_base::num::basic::traits::OneHalf;
986    /// use malachite_base::rounding_modes::RoundingMode::*;
987    /// use malachite_float::Float;
988    /// use malachite_q::Rational;
989    /// use std::cmp::Ordering::*;
990    ///
991    /// let (c, o) = Float::atanh_rational_prec_round(Rational::ONE_HALF, 5, Floor);
992    /// assert_eq!(c.to_string(), "0.531");
993    /// assert_eq!(o, Less);
994    ///
995    /// let (c, o) = Float::atanh_rational_prec_round(Rational::ONE_HALF, 5, Ceiling);
996    /// assert_eq!(c.to_string(), "0.562");
997    /// assert_eq!(o, Greater);
998    ///
999    /// let (c, o) = Float::atanh_rational_prec_round(Rational::from_signeds(-1i8, 2), 20, Floor);
1000    /// assert_eq!(c.to_string(), "-0.54930687");
1001    /// assert_eq!(o, Less);
1002    ///
1003    /// let (c, o) = Float::atanh_rational_prec_round(Rational::from_signeds(-1i8, 2), 20, Ceiling);
1004    /// assert_eq!(c.to_string(), "-0.54930592");
1005    /// assert_eq!(o, Greater);
1006    /// ```
1007    #[inline]
1008    #[allow(clippy::needless_pass_by_value)]
1009    pub fn atanh_rational_prec_round(x: Rational, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1010        Self::atanh_rational_prec_round_ref(&x, prec, rm)
1011    }
1012
1013    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Rational`],
1014    /// rounding the result to the specified precision and with the specified rounding mode and
1015    /// returning the result as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`]
1016    /// is also returned, indicating whether the rounded inverse hyperbolic tangent is less than,
1017    /// equal to, or greater than the exact inverse hyperbolic tangent. Although `NaN`s are not
1018    /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1019    ///
1020    /// See [`RoundingMode`] for a description of the possible rounding modes.
1021    ///
1022    /// $$
1023    /// f(x,p,m) = \operatorname{atanh} x+\varepsilon.
1024    /// $$
1025    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
1026    ///   assumed to be 0.
1027    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is not `Nearest`, then
1028    ///   $|\varepsilon| < 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p+1}$.
1029    /// - If $\operatorname{atanh} x$ is finite and nonzero, and $m$ is `Nearest`, then
1030    ///   $|\varepsilon| \leq 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$.
1031    ///
1032    /// These bounds do not apply when the result underflows; see below.
1033    ///
1034    /// If the output has a precision, it is `prec`.
1035    ///
1036    /// Special cases:
1037    /// - $f(0,p,m)=0.0$
1038    /// - $f(\pm1,p,m)=\pm\infty$
1039    /// - $f(x,p,m)=\text{NaN}$ if $|x|>1$
1040    ///
1041    /// Overflow and underflow:
1042    /// - The result never overflows: for $|x|<1$ with denominator $d$, $|\operatorname{atanh} x|
1043    ///   \leq \frac{1}{2}\ln 2d$.
1044    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1045    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1046    ///   instead.
1047    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$, and $m$ is `Nearest`, $0.0$ is returned instead.
1048    /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1049    ///   instead.
1050    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
1051    /// - If $-2^{-2^{30}}<f(x,p,m)<0$, and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
1052    ///   instead.
1053    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$, and $m$ is `Nearest`, $-0.0$ is returned instead.
1054    /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$, and $m$ is `Nearest`, $-2^{-2^{30}}$ is
1055    ///   returned instead.
1056    ///
1057    /// Underflow requires an $x$ of magnitude below $2^{-2^{30}}$, the smallest positive [`Float`],
1058    /// since $|\operatorname{atanh} x| > |x|$ for nonzero $x$.
1059    ///
1060    /// If you know you'll be using `Nearest`, consider using [`Float::atanh_rational_prec_ref`]
1061    /// instead.
1062    ///
1063    /// # Worst-case complexity
1064    /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
1065    ///
1066    /// $M(n, m) = O(n \log n + m \log m)$
1067    ///
1068    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1069    /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
1070    /// the input is handled with `Rational` arithmetic.
1071    ///
1072    /// # Panics
1073    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
1074    /// with the given precision (which is the case for every nonzero $x$ with $|x|<1$).
1075    ///
1076    /// # Examples
1077    /// ```
1078    /// use malachite_base::num::basic::traits::OneHalf;
1079    /// use malachite_base::rounding_modes::RoundingMode::*;
1080    /// use malachite_float::Float;
1081    /// use malachite_q::Rational;
1082    /// use std::cmp::Ordering::*;
1083    ///
1084    /// let (c, o) = Float::atanh_rational_prec_round_ref(&Rational::ONE_HALF, 5, Floor);
1085    /// assert_eq!(c.to_string(), "0.531");
1086    /// assert_eq!(o, Less);
1087    ///
1088    /// let (c, o) = Float::atanh_rational_prec_round_ref(&Rational::ONE_HALF, 5, Ceiling);
1089    /// assert_eq!(c.to_string(), "0.562");
1090    /// assert_eq!(o, Greater);
1091    ///
1092    /// let (c, o) =
1093    ///     Float::atanh_rational_prec_round_ref(&Rational::from_signeds(-1i8, 2), 20, Floor);
1094    /// assert_eq!(c.to_string(), "-0.54930687");
1095    /// assert_eq!(o, Less);
1096    ///
1097    /// let (c, o) =
1098    ///     Float::atanh_rational_prec_round_ref(&Rational::from_signeds(-1i8, 2), 20, Ceiling);
1099    /// assert_eq!(c.to_string(), "-0.54930592");
1100    /// assert_eq!(o, Greater);
1101    /// ```
1102    pub fn atanh_rational_prec_round_ref(
1103        x: &Rational,
1104        prec: u64,
1105        rm: RoundingMode,
1106    ) -> (Self, Ordering) {
1107        assert_ne!(prec, 0);
1108        if *x == 0u32 {
1109            // atanh(0) = 0, exactly
1110            return (Self::ZERO, Equal);
1111        }
1112        match x.cmp_abs(&Rational::ONE) {
1113            Less => atanh_rational_helper(x, prec, rm),
1114            // atanh(+/-1) = +/-Inf
1115            Equal => (
1116                if *x > 0u32 {
1117                    Self::INFINITY
1118                } else {
1119                    Self::NEGATIVE_INFINITY
1120                },
1121                Equal,
1122            ),
1123            Greater => (Self::NAN, Equal),
1124        }
1125    }
1126
1127    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Rational`],
1128    /// rounding the result to the nearest value of the specified precision and returning the result
1129    /// as a [`Float`]. The [`Rational`] is taken by value. An [`Ordering`] is also returned,
1130    /// indicating whether the rounded inverse hyperbolic tangent is less than, equal to, or greater
1131    /// than the exact inverse hyperbolic tangent. Although `NaN`s are not comparable to any
1132    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1133    ///
1134    /// If the inverse hyperbolic tangent is equidistant from two [`Float`]s with the specified
1135    /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
1136    /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
1137    ///
1138    /// $$
1139    /// f(x,p) = \operatorname{atanh} x+\varepsilon,
1140    /// $$
1141    /// where, if $\operatorname{atanh} x$ is finite and nonzero, $|\varepsilon| \leq
1142    /// 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$ (unless the result underflows; see
1143    /// below).
1144    ///
1145    /// If the output has a precision, it is `prec`.
1146    ///
1147    /// Special cases:
1148    /// - $f(0,p)=0.0$
1149    /// - $f(\pm1,p)=\pm\infty$
1150    /// - $f(x,p)=\text{NaN}$ if $|x|>1$
1151    ///
1152    /// Overflow and underflow:
1153    /// - The result never overflows: for $|x|<1$ with denominator $d$, $|\operatorname{atanh} x|
1154    ///   \leq \frac{1}{2}\ln 2d$.
1155    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1156    /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1157    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
1158    /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1159    ///
1160    /// Underflow requires an $x$ of magnitude below $2^{-2^{30}}$, the smallest positive [`Float`],
1161    /// since $|\operatorname{atanh} x| > |x|$ for nonzero $x$.
1162    ///
1163    /// If you want to use a rounding mode other than `Nearest`, consider using
1164    /// [`Float::atanh_rational_prec_round`] instead.
1165    ///
1166    /// # Worst-case complexity
1167    /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
1168    ///
1169    /// $M(n, m) = O(n \log n + m \log m)$
1170    ///
1171    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1172    /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
1173    /// the input is handled with `Rational` arithmetic.
1174    ///
1175    /// # Panics
1176    /// Panics if `prec` is zero.
1177    ///
1178    /// # Examples
1179    /// ```
1180    /// use malachite_base::num::basic::traits::{One, OneHalf, Two};
1181    /// use malachite_float::Float;
1182    /// use malachite_q::Rational;
1183    /// use std::cmp::Ordering::*;
1184    ///
1185    /// let (c, o) = Float::atanh_rational_prec(Rational::ONE_HALF, 5);
1186    /// assert_eq!(c.to_string(), "0.562");
1187    /// assert_eq!(o, Greater);
1188    ///
1189    /// let (c, o) = Float::atanh_rational_prec(Rational::ONE_HALF, 20);
1190    /// assert_eq!(c.to_string(), "0.54930592");
1191    /// assert_eq!(o, Less);
1192    ///
1193    /// let (c, o) = Float::atanh_rational_prec(Rational::ONE, 10);
1194    /// assert_eq!(c.to_string(), "Infinity");
1195    /// assert_eq!(o, Equal);
1196    ///
1197    /// let (c, o) = Float::atanh_rational_prec(Rational::TWO, 10);
1198    /// assert!(c.is_nan());
1199    /// assert_eq!(o, Equal);
1200    /// ```
1201    #[inline]
1202    #[allow(clippy::needless_pass_by_value)]
1203    pub fn atanh_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
1204        Self::atanh_rational_prec_round_ref(&x, prec, Nearest)
1205    }
1206
1207    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Rational`],
1208    /// rounding the result to the nearest value of the specified precision and returning the result
1209    /// as a [`Float`]. The [`Rational`] is taken by reference. An [`Ordering`] is also returned,
1210    /// indicating whether the rounded inverse hyperbolic tangent is less than, equal to, or greater
1211    /// than the exact inverse hyperbolic tangent. Although `NaN`s are not comparable to any
1212    /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1213    ///
1214    /// If the inverse hyperbolic tangent is equidistant from two [`Float`]s with the specified
1215    /// precision, the [`Float`] with fewer 1s in its binary expansion is chosen. See
1216    /// [`RoundingMode`] for a description of the `Nearest` rounding mode.
1217    ///
1218    /// $$
1219    /// f(x,p) = \operatorname{atanh} x+\varepsilon,
1220    /// $$
1221    /// where, if $\operatorname{atanh} x$ is finite and nonzero, $|\varepsilon| \leq
1222    /// 2^{\lfloor\log_2 |\operatorname{atanh} x|\rfloor-p}$ (unless the result underflows; see
1223    /// below).
1224    ///
1225    /// If the output has a precision, it is `prec`.
1226    ///
1227    /// Special cases:
1228    /// - $f(0,p)=0.0$
1229    /// - $f(\pm1,p)=\pm\infty$
1230    /// - $f(x,p)=\text{NaN}$ if $|x|>1$
1231    ///
1232    /// Overflow and underflow:
1233    /// - The result never overflows: for $|x|<1$ with denominator $d$, $|\operatorname{atanh} x|
1234    ///   \leq \frac{1}{2}\ln 2d$.
1235    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1236    /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1237    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
1238    /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
1239    ///
1240    /// Underflow requires an $x$ of magnitude below $2^{-2^{30}}$, the smallest positive [`Float`],
1241    /// since $|\operatorname{atanh} x| > |x|$ for nonzero $x$.
1242    ///
1243    /// If you want to use a rounding mode other than `Nearest`, consider using
1244    /// [`Float::atanh_rational_prec_round_ref`] instead.
1245    ///
1246    /// # Worst-case complexity
1247    /// $T(n, m) = O(n (\log n)^2 \log\log n + m (\log m)^2 \log\log m)$
1248    ///
1249    /// $M(n, m) = O(n \log n + m \log m)$
1250    ///
1251    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1252    /// `x.significant_bits()`: the logarithm is computed at a working precision of about $n$, and
1253    /// the input is handled with `Rational` arithmetic.
1254    ///
1255    /// # Panics
1256    /// Panics if `prec` is zero.
1257    ///
1258    /// # Examples
1259    /// ```
1260    /// use malachite_base::num::basic::traits::{One, OneHalf, Two};
1261    /// use malachite_float::Float;
1262    /// use malachite_q::Rational;
1263    /// use std::cmp::Ordering::*;
1264    ///
1265    /// let (c, o) = Float::atanh_rational_prec_ref(&Rational::ONE_HALF, 5);
1266    /// assert_eq!(c.to_string(), "0.562");
1267    /// assert_eq!(o, Greater);
1268    ///
1269    /// let (c, o) = Float::atanh_rational_prec_ref(&Rational::ONE_HALF, 20);
1270    /// assert_eq!(c.to_string(), "0.54930592");
1271    /// assert_eq!(o, Less);
1272    ///
1273    /// let (c, o) = Float::atanh_rational_prec_ref(&Rational::ONE, 10);
1274    /// assert_eq!(c.to_string(), "Infinity");
1275    /// assert_eq!(o, Equal);
1276    ///
1277    /// let (c, o) = Float::atanh_rational_prec_ref(&Rational::TWO, 10);
1278    /// assert!(c.is_nan());
1279    /// assert_eq!(o, Equal);
1280    /// ```
1281    #[inline]
1282    pub fn atanh_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
1283        Self::atanh_rational_prec_round_ref(x, prec, Nearest)
1284    }
1285}
1286
1287impl Atanh for Float {
1288    type Output = Self;
1289
1290    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], taking it
1291    /// by value.
1292    ///
1293    /// If the output has a precision, it is the precision of the input. If the inverse hyperbolic
1294    /// tangent is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
1295    /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
1296    /// `Nearest` rounding mode.
1297    ///
1298    /// $$
1299    /// f(x) = \operatorname{atanh} x+\varepsilon.
1300    /// $$
1301    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
1302    ///   assumed to be 0.
1303    /// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1304    ///   |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the precision of the input.
1305    ///
1306    /// Special cases:
1307    /// - $f(\text{NaN})=\text{NaN}$
1308    /// - $f(\pm\infty)=\text{NaN}$
1309    /// - $f(\pm0.0)=\pm0.0$
1310    /// - $f(\pm1)=\pm\infty$
1311    /// - $f(x)=\text{NaN}$ if $|x|>1$
1312    ///
1313    /// See the [`Float::atanh_round`] documentation for information on overflow and underflow.
1314    ///
1315    /// If you want to use a rounding mode other than `Nearest`, consider using
1316    /// [`Float::atanh_round`] instead. If you want to specify the output precision, consider using
1317    /// [`Float::atanh_prec`]. If you want both of these things, consider using
1318    /// [`Float::atanh_prec_round`].
1319    ///
1320    /// # Worst-case complexity
1321    /// $T(n) = O(n (\log n)^2 \log\log n)$
1322    ///
1323    /// $M(n) = O(n \log n)$
1324    ///
1325    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1326    ///
1327    /// # Examples
1328    /// ```
1329    /// use malachite_base::num::arithmetic::traits::Atanh;
1330    /// use malachite_base::num::basic::traits::*;
1331    /// use malachite_float::Float;
1332    ///
1333    /// assert!(Float::NAN.atanh().is_nan());
1334    /// assert!(Float::INFINITY.atanh().is_nan());
1335    /// assert!(Float::NEGATIVE_INFINITY.atanh().is_nan());
1336    /// assert_eq!(Float::ZERO.atanh().to_string(), "0.0");
1337    /// assert_eq!(Float::NEGATIVE_ZERO.atanh().to_string(), "-0.0");
1338    /// assert_eq!(Float::ONE.atanh().to_string(), "Infinity");
1339    /// assert_eq!(Float::NEGATIVE_ONE.atanh().to_string(), "-Infinity");
1340    /// assert!(Float::TWO.atanh().is_nan());
1341    /// assert_eq!(
1342    ///     (Float::one_prec(100) >> 1u32).atanh().to_string(),
1343    ///     "0.54930614433405484569762261846113"
1344    /// );
1345    /// assert_eq!(
1346    ///     (-(Float::from_unsigned_prec(3u32, 100).0 >> 2u32))
1347    ///         .atanh()
1348    ///         .to_string(),
1349    ///     "-0.97295507452765665255267637172144"
1350    /// );
1351    /// ```
1352    #[inline]
1353    fn atanh(self) -> Self {
1354        let prec = self.significant_bits();
1355        self.atanh_prec_round(prec, Nearest).0
1356    }
1357}
1358
1359impl Atanh for &Float {
1360    type Output = Float;
1361
1362    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], taking it
1363    /// by reference.
1364    ///
1365    /// If the output has a precision, it is the precision of the input. If the inverse hyperbolic
1366    /// tangent is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
1367    /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
1368    /// `Nearest` rounding mode.
1369    ///
1370    /// $$
1371    /// f(x) = \operatorname{atanh} x+\varepsilon.
1372    /// $$
1373    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
1374    ///   assumed to be 0.
1375    /// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1376    ///   |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the precision of the input.
1377    ///
1378    /// Special cases:
1379    /// - $f(\text{NaN})=\text{NaN}$
1380    /// - $f(\pm\infty)=\text{NaN}$
1381    /// - $f(\pm0.0)=\pm0.0$
1382    /// - $f(\pm1)=\pm\infty$
1383    /// - $f(x)=\text{NaN}$ if $|x|>1$
1384    ///
1385    /// See the [`Float::atanh_round`] documentation for information on overflow and underflow.
1386    ///
1387    /// If you want to use a rounding mode other than `Nearest`, consider using
1388    /// [`Float::atanh_round_ref`] instead. If you want to specify the output precision, consider
1389    /// using [`Float::atanh_prec_ref`]. If you want both of these things, consider using
1390    /// [`Float::atanh_prec_round_ref`].
1391    ///
1392    /// # Worst-case complexity
1393    /// $T(n) = O(n (\log n)^2 \log\log n)$
1394    ///
1395    /// $M(n) = O(n \log n)$
1396    ///
1397    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1398    ///
1399    /// # Examples
1400    /// ```
1401    /// use malachite_base::num::arithmetic::traits::Atanh;
1402    /// use malachite_base::num::basic::traits::*;
1403    /// use malachite_float::Float;
1404    ///
1405    /// assert!((&Float::NAN).atanh().is_nan());
1406    /// assert!((&Float::INFINITY).atanh().is_nan());
1407    /// assert!((&Float::NEGATIVE_INFINITY).atanh().is_nan());
1408    /// assert_eq!((&Float::ZERO).atanh().to_string(), "0.0");
1409    /// assert_eq!((&Float::NEGATIVE_ZERO).atanh().to_string(), "-0.0");
1410    /// assert_eq!((&Float::ONE).atanh().to_string(), "Infinity");
1411    /// assert_eq!((&Float::NEGATIVE_ONE).atanh().to_string(), "-Infinity");
1412    /// assert!((&Float::TWO).atanh().is_nan());
1413    /// assert_eq!(
1414    ///     (&(Float::one_prec(100) >> 1u32)).atanh().to_string(),
1415    ///     "0.54930614433405484569762261846113"
1416    /// );
1417    /// assert_eq!(
1418    ///     (&-(Float::from_unsigned_prec(3u32, 100).0 >> 2u32))
1419    ///         .atanh()
1420    ///         .to_string(),
1421    ///     "-0.97295507452765665255267637172144"
1422    /// );
1423    /// ```
1424    #[inline]
1425    fn atanh(self) -> Float {
1426        self.atanh_prec_round_ref(self.significant_bits(), Nearest)
1427            .0
1428    }
1429}
1430
1431impl AtanhAssign for Float {
1432    /// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Float`], in place.
1433    ///
1434    /// If the output has a precision, it is the precision of the input. If the inverse hyperbolic
1435    /// tangent is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
1436    /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
1437    /// `Nearest` rounding mode.
1438    ///
1439    /// $$
1440    /// x \gets \operatorname{atanh} x+\varepsilon.
1441    /// $$
1442    /// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or
1443    ///   assumed to be 0.
1444    /// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1445    ///   |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the precision of the input.
1446    ///
1447    /// See the [`Float::atanh`] documentation for information on special cases, overflow, and
1448    /// underflow.
1449    ///
1450    /// If you want to use a rounding mode other than `Nearest`, consider using
1451    /// [`Float::atanh_round_assign`] instead. If you want to specify the output precision, consider
1452    /// using [`Float::atanh_prec_assign`]. If you want both of these things, consider using
1453    /// [`Float::atanh_prec_round_assign`].
1454    ///
1455    /// # Worst-case complexity
1456    /// $T(n) = O(n (\log n)^2 \log\log n)$
1457    ///
1458    /// $M(n) = O(n \log n)$
1459    ///
1460    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1461    ///
1462    /// # Examples
1463    /// ```
1464    /// use malachite_base::num::arithmetic::traits::AtanhAssign;
1465    /// use malachite_base::num::basic::traits::*;
1466    /// use malachite_float::Float;
1467    ///
1468    /// let mut x = Float::NAN;
1469    /// x.atanh_assign();
1470    /// assert!(x.is_nan());
1471    ///
1472    /// let mut x = Float::INFINITY;
1473    /// x.atanh_assign();
1474    /// assert!(x.is_nan());
1475    ///
1476    /// let mut x = Float::NEGATIVE_INFINITY;
1477    /// x.atanh_assign();
1478    /// assert!(x.is_nan());
1479    ///
1480    /// let mut x = Float::ZERO;
1481    /// x.atanh_assign();
1482    /// assert_eq!(x.to_string(), "0.0");
1483    ///
1484    /// let mut x = Float::NEGATIVE_ZERO;
1485    /// x.atanh_assign();
1486    /// assert_eq!(x.to_string(), "-0.0");
1487    ///
1488    /// let mut x = Float::ONE;
1489    /// x.atanh_assign();
1490    /// assert_eq!(x.to_string(), "Infinity");
1491    ///
1492    /// let mut x = Float::NEGATIVE_ONE;
1493    /// x.atanh_assign();
1494    /// assert_eq!(x.to_string(), "-Infinity");
1495    ///
1496    /// let mut x = Float::TWO;
1497    /// x.atanh_assign();
1498    /// assert!(x.is_nan());
1499    ///
1500    /// let mut x = Float::one_prec(100) >> 1u32;
1501    /// x.atanh_assign();
1502    /// assert_eq!(x.to_string(), "0.54930614433405484569762261846113");
1503    ///
1504    /// let mut x = -(Float::from_unsigned_prec(3u32, 100).0 >> 2u32);
1505    /// x.atanh_assign();
1506    /// assert_eq!(x.to_string(), "-0.97295507452765665255267637172144");
1507    /// ```
1508    #[inline]
1509    fn atanh_assign(&mut self) {
1510        let prec = self.significant_bits();
1511        self.atanh_prec_round_assign(prec, Nearest);
1512    }
1513}
1514
1515/// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a primitive float. Using
1516/// this function is more accurate than using the default `atanh` function or the one provided by
1517/// `libm`.
1518///
1519/// $$
1520/// f(x) = \operatorname{atanh} x+\varepsilon.
1521/// $$
1522/// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed
1523///   to be 0.
1524/// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1525///   |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the precision of the output (24 if `T` is a
1526///   [`f32`] and 53 if `T` is a [`f64`]).
1527///
1528/// Special cases:
1529/// - $f(\text{NaN})=\text{NaN}$
1530/// - $f(\pm\infty)=\text{NaN}$
1531/// - $f(\pm0.0)=\pm0.0$
1532/// - $f(\pm1)=\pm\infty$
1533/// - $f(x)=\text{NaN}$ if $|x|>1$
1534///
1535/// Overflow is not possible. The result is subnormal only when $x$ is, and then it is $x$ itself,
1536/// since $|\operatorname{atanh} x - x| < |x|^3/2$.
1537///
1538/// # Worst-case complexity
1539/// Constant time and additional memory.
1540///
1541/// # Examples
1542/// ```
1543/// use malachite_base::num::basic::traits::NegativeInfinity;
1544/// use malachite_base::num::float::NiceFloat;
1545/// use malachite_float::float::arithmetic::atanh::primitive_float_atanh;
1546///
1547/// assert!(primitive_float_atanh(f32::NAN).is_nan());
1548/// assert!(primitive_float_atanh(f32::INFINITY).is_nan());
1549/// assert!(primitive_float_atanh(f32::NEGATIVE_INFINITY).is_nan());
1550/// assert_eq!(NiceFloat(primitive_float_atanh(0.0f32)), NiceFloat(0.0));
1551/// assert_eq!(NiceFloat(primitive_float_atanh(-0.0f32)), NiceFloat(-0.0));
1552/// assert_eq!(
1553///     NiceFloat(primitive_float_atanh(1.0f32)),
1554///     NiceFloat(f32::INFINITY)
1555/// );
1556/// assert!(primitive_float_atanh(2.0f32).is_nan());
1557/// assert_eq!(
1558///     NiceFloat(primitive_float_atanh(0.5f32)),
1559///     NiceFloat(0.54930615)
1560/// );
1561/// assert_eq!(
1562///     NiceFloat(primitive_float_atanh(0.5f64)),
1563///     NiceFloat(0.5493061443340549)
1564/// );
1565/// assert_eq!(
1566///     NiceFloat(primitive_float_atanh(-0.75f64)),
1567///     NiceFloat(-0.9729550745276566)
1568/// );
1569/// ```
1570#[inline]
1571#[allow(clippy::type_repetition_in_bounds)]
1572pub fn primitive_float_atanh<T: PrimitiveFloat>(x: T) -> T
1573where
1574    Float: From<T> + PartialOrd<T>,
1575    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1576{
1577    emulate_float_to_float_fn(Float::atanh_prec, x)
1578}
1579
1580/// Computes $\operatorname{atanh} x$, the inverse hyperbolic tangent of a [`Rational`], returning
1581/// the result as a primitive float. The result is correctly rounded.
1582///
1583/// $$
1584/// f(x) = \operatorname{atanh} x+\varepsilon.
1585/// $$
1586/// - If $\operatorname{atanh} x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed
1587///   to be 0.
1588/// - If $\operatorname{atanh} x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1589///   |\operatorname{atanh} x|\rfloor-p}$, where $p$ is the precision of the output (typically 24 if
1590///   `T` is a [`f32`] and 53 if `T` is a [`f64`], but less if the output is subnormal).
1591///
1592/// Special cases:
1593/// - $f(0)=0.0$
1594/// - $f(\pm1)=\pm\infty$
1595/// - $f(x)=\text{NaN}$ if $|x|>1$
1596///
1597/// Overflow is not possible for $|x|<1$. Underflow is: an `x` of small enough magnitude gives `0.0`
1598/// or `-0.0`.
1599///
1600/// # Worst-case complexity
1601/// $T(m) = O(m (\log m)^2 \log\log m)$
1602///
1603/// $M(m) = O(m \log m)$
1604///
1605/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
1606///
1607/// # Examples
1608/// ```
1609/// use malachite_base::num::basic::traits::{One, OneHalf, Two, Zero};
1610/// use malachite_base::num::float::NiceFloat;
1611/// use malachite_float::float::arithmetic::atanh::primitive_float_atanh_rational;
1612/// use malachite_q::Rational;
1613///
1614/// assert_eq!(
1615///     NiceFloat(primitive_float_atanh_rational::<f64>(&Rational::ZERO)),
1616///     NiceFloat(0.0)
1617/// );
1618/// assert_eq!(
1619///     NiceFloat(primitive_float_atanh_rational::<f64>(&Rational::ONE)),
1620///     NiceFloat(f64::INFINITY)
1621/// );
1622/// assert!(primitive_float_atanh_rational::<f64>(&Rational::TWO).is_nan());
1623/// assert_eq!(
1624///     NiceFloat(primitive_float_atanh_rational::<f64>(&Rational::ONE_HALF)),
1625///     NiceFloat(0.5493061443340549)
1626/// );
1627/// assert_eq!(
1628///     NiceFloat(primitive_float_atanh_rational::<f64>(
1629///         &Rational::from_unsigneds(1u8, 3)
1630///     )),
1631///     NiceFloat(0.34657359027997264)
1632/// );
1633/// ```
1634#[inline]
1635#[allow(clippy::type_repetition_in_bounds)]
1636pub fn primitive_float_atanh_rational<T: PrimitiveFloat>(x: &Rational) -> T
1637where
1638    Float: PartialOrd<T>,
1639    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1640{
1641    emulate_rational_to_float_fn(Float::atanh_rational_prec_ref, x)
1642}