Skip to main content

malachite_float/float/arithmetic/
ln_1_plus_x.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::{Infinity, NaN, Zero};
16use crate::float::arithmetic::round_near_x::float_round_near_x;
17use crate::{Float, emulate_float_to_float_fn, float_infinity, float_nan, float_negative_infinity};
18use core::cmp::Ordering::{self, *};
19use malachite_base::fail_on_untested_path;
20use malachite_base::num::arithmetic::traits::{
21    CeilingLogBase2, Ln, Ln1PlusX, Ln1PlusXAssign, Parity,
22};
23use malachite_base::num::basic::floats::PrimitiveFloat;
24use malachite_base::num::basic::integers::PrimitiveInt;
25use malachite_base::num::basic::traits::One;
26use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
27use malachite_base::num::logic::traits::SignificantBits;
28use malachite_base::rounding_modes::RoundingMode::{self, *};
29use malachite_nz::natural::arithmetic::float::round::float_can_round;
30use malachite_nz::platform::Limb;
31use malachite_q::Rational;
32
33// Computes an approximation of ln(1+x) for x small, using the Taylor expansion. Assumes |x| < 1/2
34// (that is, EXP(x) <= -1), in which case |x/2| <= |ln(1+x)| <= |2x|. The result has precision
35// `prec`. Returns k such that the error is bounded by 2^k ulps of the result.
36//
37// This is mpfr_log1p_small from log1p.c, MPFR 4.3.0.
38fn ln_1_plus_x_small(x: &Float, prec: u64) -> (Float, u64) {
39    assert!(x.get_exponent().unwrap() <= -1); // ensures |x| < 1/2
40    // In the following, theta represents a value with |theta| <= 2^(1-prec) (might be a different
41    // value each time).
42    let mut t = Float::from_float_prec_ref(x, prec).0; // t = x * (1 + theta)
43    let mut y = t.clone(); // exact
44    let y_exp_m_prec = i64::from(y.get_exponent().unwrap()) - i64::exact_from(prec);
45    let mut i = 2u32;
46    loop {
47        t.mul_prec_assign_ref(x, prec); // t = x^i * (1 + theta)^i
48        // u = x^i / i * (1 + theta)^(i + 1)
49        let u = t.div_prec_ref_val(Float::from(i), prec).0;
50        // |u| < ulp(y). For x within a few binades of the smallest positive Float, x^i underflows
51        // to zero (MPFR computes in an extended exponent range where it cannot); a zero term means
52        // the remainder is certainly below ulp(y), the same break condition.
53        let Some(u_exp) = u.get_exponent() else {
54            break;
55        };
56        if i64::from(u_exp) <= y_exp_m_prec {
57            break;
58        }
59        if i.odd() {
60            y.add_prec_assign(u, prec); // error <= ulp(y)
61        } else {
62            y.sub_prec_assign(u, prec); // error <= ulp(y)
63        };
64        i += 1;
65    }
66    // The total error is bounded by (2 * i + 8) ulps of y; see the analysis in log1p.c.
67    let err = (u64::from(i) << 1) + 8;
68    let k = err.ceiling_log_base_2();
69    assert!(k < prec);
70    (y, k)
71}
72
73// This is mpfr_log1p from log1p.c, MPFR 4.3.0, where the input is finite and nonzero.
74fn ln_1_plus_x_prec_round_normal(x: &Float, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
75    let ex = i64::from(x.get_exponent().unwrap());
76    if ex < 0 {
77        // -0.5 < x < 0.5. For x > 0, |ln(1+x) - x| < x^2 / 2. For x > -0.5, |ln(1+x) - x| < x^2.
78        let (err1, dir) = if *x > 0u32 {
79            (-ex - 1, false)
80        } else {
81            (-ex, true)
82        };
83        if err1 > 0 {
84            let err = u64::exact_from(err1);
85            if err > prec + 1
86                && let Some(result) = float_round_near_x(x, err, dir, prec, rm)
87            {
88                return result;
89            }
90        }
91    }
92    // ln(1+x) is undefined for x < -1
93    match x.partial_cmp(&-1i32).unwrap() {
94        Equal => {
95            // ln_1_plus_x(-1) = -Infinity
96            return (float_negative_infinity!(), Equal);
97        }
98        Less => {
99            return (float_nan!(), Equal);
100        }
101        _ => {}
102    }
103    // The result is never exactly representable for finite nonzero x > -1.
104    assert_ne!(rm, Exact, "Inexact ln_1_plus_x");
105    // General case. Compute the precision of the intermediary variable: the optimal number of bits,
106    // see algorithms.tex.
107    let mut working_prec = prec + prec.ceiling_log_base_2() + 6;
108    // If |x| is smaller than 2^(-e), we will lose about e bits in ln(1+x).
109    if ex < 0 {
110        working_prec += u64::exact_from(-ex);
111    }
112    let mut increment = Limb::WIDTH;
113    // Assuming the AGM algorithm used by ln uses log2(p) steps for a precision of p bits, we try
114    // the Taylor variant whenever EXP(x) <= -p / log2(p). The + 1 avoids a division by 0 when prec
115    // = 1.
116    let k = 1 + prec.ceiling_log_base_2();
117    let small = ex < -i64::exact_from(prec / k);
118    loop {
119        let (t, err) = if small {
120            // This implies EXP(x) <= -1, thus x < 1/2.
121            let (t, k_err) = ln_1_plus_x_small(x, working_prec);
122            (t, working_prec - k_err)
123        } else {
124            let (t, o) = x.add_prec_ref_val(Float::ONE, working_prec); // 1 + x
125            if o == Equal {
126                // t = 1 + x exactly, and the result is simply ln(t).
127                return t.ln_prec_round(prec, rm);
128            }
129            // MPFR computes with an extended exponent range, so its 1 + x cannot overflow or
130            // underflow; ours can, and both cases need rescuing.
131            let t = if t == 0u32 {
132                // 1 + x underflowed, so x is just above -1 and 1 + x is positive but smaller than
133                // 2^MIN_EXPONENT. Reaching this branch requires the precision of x to exceed 2^30,
134                // which no generator produces.
135                fail_on_untested_path("ln_1_plus_x_prec_round_normal, 1 + x underflows");
136                // The sum 1 + x is an exact dyadic rational, so use the Rational implementation of
137                // ln.
138                return Float::ln_rational_prec_round(
139                    Rational::ONE + Rational::exact_from(x),
140                    prec,
141                    rm,
142                );
143            } else if t.is_infinite() {
144                // 1 + x overflowed, so x >= 2^working_prec and ln(1+x) differs from ln(x) by ln(1 +
145                // 1/x) < 2^(1-MAX_EXPONENT), far less than an ulp; use ln(x).
146                x.ln_prec_ref(working_prec).0
147            } else {
148                t.ln() // ln(1+x)
149            };
150            // The error is bounded by (1/2 + 2^(1-EXP(t))) * ulp(t) (cf algorithms.tex). If EXP(t)
151            // >= 2, then error <= ulp(t). If EXP(t) <= 1, then error <= 2^(2-EXP(t)) * ulp(t).
152            let t_exp = i64::from(t.get_exponent().unwrap());
153            let cancel = u64::exact_from(core::cmp::max(0, 2 - t_exp));
154            (t, working_prec - cancel)
155        };
156        if float_can_round(t.significand_ref().unwrap(), err, prec, rm) {
157            return Float::from_float_prec_round(t, prec, rm);
158        }
159        // Increase the precision.
160        working_prec += increment;
161        increment = working_prec >> 1;
162    }
163}
164
165impl Float {
166    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result to the specified
167    /// precision and with the specified rounding mode. The [`Float`] is taken by value. An
168    /// [`Ordering`] is also returned, indicating whether the rounded value is less than, equal to,
169    /// or greater than the exact value. Although `NaN`s are not comparable to any [`Float`],
170    /// whenever this function returns a `NaN` it also returns `Equal`.
171    ///
172    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
173    ///
174    /// See [`RoundingMode`] for a description of the possible rounding modes.
175    ///
176    /// $$
177    /// f(x,p,m) = \ln(1+x)+\varepsilon.
178    /// $$
179    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
180    ///   0.
181    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
182    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$.
183    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
184    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$.
185    ///
186    /// If the output has a precision, it is `prec`.
187    ///
188    /// Special cases:
189    /// - $f(\text{NaN},p,m)=\text{NaN}$
190    /// - $f(\infty,p,m)=\infty$
191    /// - $f(-\infty,p,m)=\text{NaN}$
192    /// - $f(\pm0.0,p,m)=\pm0.0$
193    /// - $f(-1,p,m)=-\infty$
194    /// - $f(x,p,m)=\text{NaN}$ for $x<-1$
195    ///
196    /// This function cannot overflow, but it can underflow:
197    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
198    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling`, `Up`, or `Nearest`, $2^{-2^{30}}$ is
199    ///   returned instead.
200    ///
201    /// If you know you'll be using `Nearest`, consider using [`Float::ln_1_plus_x_prec`] instead.
202    /// If you know that your target precision is the precision of the input, consider using
203    /// [`Float::ln_1_plus_x_round`] instead. If both of these things are true, consider using
204    /// [`Float::ln_1_plus_x`] instead.
205    ///
206    /// # Worst-case complexity
207    /// $T(n, m) = O(n (\log n)^2 \log\log n + m)$
208    ///
209    /// $M(n, m) = O(n \log n + m)$
210    ///
211    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
212    /// `self.significant_bits()`.
213    ///
214    /// # Panics
215    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
216    /// with the given precision. (The result cannot be represented exactly whenever the input is
217    /// finite, nonzero, and greater than $-1$.)
218    ///
219    /// # Examples
220    /// ```
221    /// use malachite_base::rounding_modes::RoundingMode::*;
222    /// use malachite_float::Float;
223    /// use std::cmp::Ordering::*;
224    ///
225    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
226    ///     .0
227    ///     .ln_1_plus_x_prec_round(5, Floor);
228    /// assert_eq!(ln.to_string(), "2.38");
229    /// assert_eq!(o, Less);
230    ///
231    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
232    ///     .0
233    ///     .ln_1_plus_x_prec_round(5, Ceiling);
234    /// assert_eq!(ln.to_string(), "2.50");
235    /// assert_eq!(o, Greater);
236    ///
237    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
238    ///     .0
239    ///     .ln_1_plus_x_prec_round(5, Nearest);
240    /// assert_eq!(ln.to_string(), "2.38");
241    /// assert_eq!(o, Less);
242    ///
243    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
244    ///     .0
245    ///     .ln_1_plus_x_prec_round(20, Floor);
246    /// assert_eq!(ln.to_string(), "2.3978920");
247    /// assert_eq!(o, Less);
248    ///
249    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
250    ///     .0
251    ///     .ln_1_plus_x_prec_round(20, Ceiling);
252    /// assert_eq!(ln.to_string(), "2.3978958");
253    /// assert_eq!(o, Greater);
254    ///
255    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
256    ///     .0
257    ///     .ln_1_plus_x_prec_round(20, Nearest);
258    /// assert_eq!(ln.to_string(), "2.3978958");
259    /// assert_eq!(o, Greater);
260    /// ```
261    #[inline]
262    pub fn ln_1_plus_x_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
263        assert_ne!(prec, 0);
264        match self {
265            Self(NaN | Infinity { sign: false }) => (float_nan!(), Equal),
266            float_infinity!() => (float_infinity!(), Equal),
267            // ln_1_plus_x(±0) = ±0
268            Self(Zero { .. }) => (self, Equal),
269            _ => ln_1_plus_x_prec_round_normal(&self, prec, rm),
270        }
271    }
272
273    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result to the specified
274    /// precision and with the specified rounding mode. The [`Float`] is taken by reference. An
275    /// [`Ordering`] is also returned, indicating whether the rounded value is less than, equal to,
276    /// or greater than the exact value. Although `NaN`s are not comparable to any [`Float`],
277    /// whenever this function returns a `NaN` it also returns `Equal`.
278    ///
279    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
280    ///
281    /// See [`RoundingMode`] for a description of the possible rounding modes.
282    ///
283    /// $$
284    /// f(x,p,m) = \ln(1+x)+\varepsilon.
285    /// $$
286    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
287    ///   0.
288    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
289    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$.
290    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
291    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$.
292    ///
293    /// If the output has a precision, it is `prec`.
294    ///
295    /// Special cases:
296    /// - $f(\text{NaN},p,m)=\text{NaN}$
297    /// - $f(\infty,p,m)=\infty$
298    /// - $f(-\infty,p,m)=\text{NaN}$
299    /// - $f(\pm0.0,p,m)=\pm0.0$
300    /// - $f(-1,p,m)=-\infty$
301    /// - $f(x,p,m)=\text{NaN}$ for $x<-1$
302    ///
303    /// This function cannot overflow, but it can underflow:
304    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
305    /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling`, `Up`, or `Nearest`, $2^{-2^{30}}$ is
306    ///   returned instead.
307    ///
308    /// If you know you'll be using `Nearest`, consider using [`Float::ln_1_plus_x_prec_ref`]
309    /// instead. If you know that your target precision is the precision of the input, consider
310    /// using [`Float::ln_1_plus_x_round_ref`] instead. If both of these things are true, consider
311    /// using `(&Float).ln_1_plus_x()` instead.
312    ///
313    /// # Worst-case complexity
314    /// $T(n, m) = O(n (\log n)^2 \log\log n + m)$
315    ///
316    /// $M(n, m) = O(n \log n + m)$
317    ///
318    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
319    /// `self.significant_bits()`.
320    ///
321    /// # Panics
322    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
323    /// with the given precision. (The result cannot be represented exactly whenever the input is
324    /// finite, nonzero, and greater than $-1$.)
325    ///
326    /// # Examples
327    /// ```
328    /// use malachite_base::rounding_modes::RoundingMode::*;
329    /// use malachite_float::Float;
330    /// use std::cmp::Ordering::*;
331    ///
332    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
333    ///     .0
334    ///     .ln_1_plus_x_prec_round_ref(5, Floor);
335    /// assert_eq!(ln.to_string(), "2.38");
336    /// assert_eq!(o, Less);
337    ///
338    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
339    ///     .0
340    ///     .ln_1_plus_x_prec_round_ref(5, Ceiling);
341    /// assert_eq!(ln.to_string(), "2.50");
342    /// assert_eq!(o, Greater);
343    ///
344    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
345    ///     .0
346    ///     .ln_1_plus_x_prec_round_ref(5, Nearest);
347    /// assert_eq!(ln.to_string(), "2.38");
348    /// assert_eq!(o, Less);
349    ///
350    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
351    ///     .0
352    ///     .ln_1_plus_x_prec_round_ref(20, Floor);
353    /// assert_eq!(ln.to_string(), "2.3978920");
354    /// assert_eq!(o, Less);
355    ///
356    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
357    ///     .0
358    ///     .ln_1_plus_x_prec_round_ref(20, Ceiling);
359    /// assert_eq!(ln.to_string(), "2.3978958");
360    /// assert_eq!(o, Greater);
361    ///
362    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
363    ///     .0
364    ///     .ln_1_plus_x_prec_round_ref(20, Nearest);
365    /// assert_eq!(ln.to_string(), "2.3978958");
366    /// assert_eq!(o, Greater);
367    /// ```
368    #[inline]
369    pub fn ln_1_plus_x_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
370        assert_ne!(prec, 0);
371        match self {
372            Self(NaN | Infinity { sign: false }) => (float_nan!(), Equal),
373            float_infinity!() => (float_infinity!(), Equal),
374            Self(Zero { sign }) => (Self(Zero { sign: *sign }), Equal),
375            _ => ln_1_plus_x_prec_round_normal(self, prec, rm),
376        }
377    }
378
379    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result to the nearest value of
380    /// the specified precision. The [`Float`] is taken by value. An [`Ordering`] is also returned,
381    /// indicating whether the rounded value is less than, equal to, or greater than the exact
382    /// value. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
383    /// `NaN` it also returns `Equal`.
384    ///
385    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
386    ///
387    /// If the result is equidistant from two [`Float`]s with the specified precision, the [`Float`]
388    /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
389    /// the `Nearest` rounding mode.
390    ///
391    /// $$
392    /// f(x,p) = \ln(1+x)+\varepsilon.
393    /// $$
394    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
395    ///   0.
396    /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
397    ///   |\ln(1+x)|\rfloor-p}$.
398    ///
399    /// If the output has a precision, it is `prec`.
400    ///
401    /// Special cases:
402    /// - $f(\text{NaN},p)=\text{NaN}$
403    /// - $f(\infty,p)=\infty$
404    /// - $f(-\infty,p)=\text{NaN}$
405    /// - $f(\pm0.0,p)=\pm0.0$
406    /// - $f(-1,p)=-\infty$
407    /// - $f(x,p)=\text{NaN}$ for $x<-1$
408    ///
409    /// This function cannot overflow, but it can underflow: if $0<f(x,p)<2^{-2^{30}}$,
410    /// $2^{-2^{30}}$ is returned instead.
411    ///
412    /// If you want to use a rounding mode other than `Nearest`, consider using
413    /// [`Float::ln_1_plus_x_prec_round`] instead. If you know that your target precision is the
414    /// precision of the input, consider using [`Float::ln_1_plus_x`] instead.
415    ///
416    /// # Worst-case complexity
417    /// $T(n, m) = O(n (\log n)^2 \log\log n + m)$
418    ///
419    /// $M(n, m) = O(n \log n + m)$
420    ///
421    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
422    /// `self.significant_bits()`.
423    ///
424    /// # Panics
425    /// Panics if `prec` is zero.
426    ///
427    /// # Examples
428    /// ```
429    /// use malachite_base::num::basic::traits::One;
430    /// use malachite_float::Float;
431    /// use std::cmp::Ordering::*;
432    ///
433    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100).0.ln_1_plus_x_prec(5);
434    /// assert_eq!(ln.to_string(), "2.38");
435    /// assert_eq!(o, Less);
436    ///
437    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100).0.ln_1_plus_x_prec(20);
438    /// assert_eq!(ln.to_string(), "2.3978958");
439    /// assert_eq!(o, Greater);
440    ///
441    /// let (ln, o) = Float::ONE.ln_1_plus_x_prec(20);
442    /// assert_eq!(ln.to_string(), "0.69314671");
443    /// assert_eq!(o, Less);
444    /// ```
445    #[inline]
446    pub fn ln_1_plus_x_prec(self, prec: u64) -> (Self, Ordering) {
447        self.ln_1_plus_x_prec_round(prec, Nearest)
448    }
449
450    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result to the nearest value of
451    /// the specified precision. The [`Float`] is taken by reference. An [`Ordering`] is also
452    /// returned, indicating whether the rounded value is less than, equal to, or greater than the
453    /// exact value. Although `NaN`s are not comparable to any [`Float`], whenever this function
454    /// returns a `NaN` it also returns `Equal`.
455    ///
456    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
457    ///
458    /// If the result is equidistant from two [`Float`]s with the specified precision, the [`Float`]
459    /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
460    /// the `Nearest` rounding mode.
461    ///
462    /// $$
463    /// f(x,p) = \ln(1+x)+\varepsilon.
464    /// $$
465    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
466    ///   0.
467    /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
468    ///   |\ln(1+x)|\rfloor-p}$.
469    ///
470    /// If the output has a precision, it is `prec`.
471    ///
472    /// Special cases:
473    /// - $f(\text{NaN},p)=\text{NaN}$
474    /// - $f(\infty,p)=\infty$
475    /// - $f(-\infty,p)=\text{NaN}$
476    /// - $f(\pm0.0,p)=\pm0.0$
477    /// - $f(-1,p)=-\infty$
478    /// - $f(x,p)=\text{NaN}$ for $x<-1$
479    ///
480    /// This function cannot overflow, but it can underflow: if $0<f(x,p)<2^{-2^{30}}$,
481    /// $2^{-2^{30}}$ is returned instead.
482    ///
483    /// If you want to use a rounding mode other than `Nearest`, consider using
484    /// [`Float::ln_1_plus_x_prec_round_ref`] instead. If you know that your target precision is the
485    /// precision of the input, consider using `(&Float).ln_1_plus_x()` instead.
486    ///
487    /// # Worst-case complexity
488    /// $T(n, m) = O(n (\log n)^2 \log\log n + m)$
489    ///
490    /// $M(n, m) = O(n \log n + m)$
491    ///
492    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
493    /// `self.significant_bits()`.
494    ///
495    /// # Panics
496    /// Panics if `prec` is zero.
497    ///
498    /// # Examples
499    /// ```
500    /// use malachite_base::num::basic::traits::One;
501    /// use malachite_float::Float;
502    /// use std::cmp::Ordering::*;
503    ///
504    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
505    ///     .0
506    ///     .ln_1_plus_x_prec_ref(5);
507    /// assert_eq!(ln.to_string(), "2.38");
508    /// assert_eq!(o, Less);
509    ///
510    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
511    ///     .0
512    ///     .ln_1_plus_x_prec_ref(20);
513    /// assert_eq!(ln.to_string(), "2.3978958");
514    /// assert_eq!(o, Greater);
515    ///
516    /// let (ln, o) = Float::ONE.ln_1_plus_x_prec_ref(20);
517    /// assert_eq!(ln.to_string(), "0.69314671");
518    /// assert_eq!(o, Less);
519    /// ```
520    #[inline]
521    pub fn ln_1_plus_x_prec_ref(&self, prec: u64) -> (Self, Ordering) {
522        self.ln_1_plus_x_prec_round_ref(prec, Nearest)
523    }
524
525    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result with the specified
526    /// rounding mode. The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating
527    /// whether the rounded value is less than, equal to, or greater than the exact value. Although
528    /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
529    /// returns `Equal`.
530    ///
531    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
532    ///
533    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
534    /// description of the possible rounding modes.
535    ///
536    /// $$
537    /// f(x,m) = \ln(1+x)+\varepsilon.
538    /// $$
539    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
540    ///   0.
541    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
542    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$, where $p$ is the precision of the input.
543    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
544    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
545    ///
546    /// If the output has a precision, it is the precision of the input.
547    ///
548    /// Special cases:
549    /// - $f(\text{NaN},m)=\text{NaN}$
550    /// - $f(\infty,m)=\infty$
551    /// - $f(-\infty,m)=\text{NaN}$
552    /// - $f(\pm0.0,m)=\pm0.0$
553    /// - $f(-1,m)=-\infty$
554    /// - $f(x,m)=\text{NaN}$ for $x<-1$
555    ///
556    /// This function cannot overflow, but it can underflow:
557    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
558    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Ceiling`, `Up`, or `Nearest`, $2^{-2^{30}}$ is
559    ///   returned instead.
560    ///
561    /// If you want to specify an output precision, consider using [`Float::ln_1_plus_x_prec_round`]
562    /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
563    /// [`Float::ln_1_plus_x`] instead.
564    ///
565    /// # Worst-case complexity
566    /// $T(n) = O(n (\log n)^2 \log\log n)$
567    ///
568    /// $M(n) = O(n \log n)$
569    ///
570    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
571    ///
572    /// # Panics
573    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
574    /// precision. (The result cannot be represented exactly whenever the input is finite, nonzero,
575    /// and greater than $-1$.)
576    ///
577    /// # Examples
578    /// ```
579    /// use malachite_base::rounding_modes::RoundingMode::*;
580    /// use malachite_float::Float;
581    /// use std::cmp::Ordering::*;
582    ///
583    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
584    ///     .0
585    ///     .ln_1_plus_x_round(Floor);
586    /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779621");
587    /// assert_eq!(o, Less);
588    ///
589    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
590    ///     .0
591    ///     .ln_1_plus_x_round(Ceiling);
592    /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
593    /// assert_eq!(o, Greater);
594    ///
595    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
596    ///     .0
597    ///     .ln_1_plus_x_round(Nearest);
598    /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
599    /// assert_eq!(o, Greater);
600    /// ```
601    #[inline]
602    pub fn ln_1_plus_x_round(self, rm: RoundingMode) -> (Self, Ordering) {
603        let prec = self.significant_bits();
604        self.ln_1_plus_x_prec_round(prec, rm)
605    }
606
607    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result with the specified
608    /// rounding mode. The [`Float`] is taken by reference. An [`Ordering`] is also returned,
609    /// indicating whether the rounded value is less than, equal to, or greater than the exact
610    /// value. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
611    /// `NaN` it also returns `Equal`.
612    ///
613    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
614    ///
615    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
616    /// description of the possible rounding modes.
617    ///
618    /// $$
619    /// f(x,m) = \ln(1+x)+\varepsilon.
620    /// $$
621    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
622    ///   0.
623    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
624    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$, where $p$ is the precision of the input.
625    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
626    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
627    ///
628    /// If the output has a precision, it is the precision of the input.
629    ///
630    /// Special cases:
631    /// - $f(\text{NaN},m)=\text{NaN}$
632    /// - $f(\infty,m)=\infty$
633    /// - $f(-\infty,m)=\text{NaN}$
634    /// - $f(\pm0.0,m)=\pm0.0$
635    /// - $f(-1,m)=-\infty$
636    /// - $f(x,m)=\text{NaN}$ for $x<-1$
637    ///
638    /// This function cannot overflow, but it can underflow:
639    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
640    /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Ceiling`, `Up`, or `Nearest`, $2^{-2^{30}}$ is
641    ///   returned instead.
642    ///
643    /// If you want to specify an output precision, consider using
644    /// [`Float::ln_1_plus_x_prec_round_ref`] instead. If you know you'll be using the `Nearest`
645    /// rounding mode, consider using `(&Float).ln_1_plus_x()` instead.
646    ///
647    /// # Worst-case complexity
648    /// $T(n) = O(n (\log n)^2 \log\log n)$
649    ///
650    /// $M(n) = O(n \log n)$
651    ///
652    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
653    ///
654    /// # Panics
655    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
656    /// precision. (The result cannot be represented exactly whenever the input is finite, nonzero,
657    /// and greater than $-1$.)
658    ///
659    /// # Examples
660    /// ```
661    /// use malachite_base::rounding_modes::RoundingMode::*;
662    /// use malachite_float::Float;
663    /// use std::cmp::Ordering::*;
664    ///
665    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
666    ///     .0
667    ///     .ln_1_plus_x_round_ref(Floor);
668    /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779621");
669    /// assert_eq!(o, Less);
670    ///
671    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
672    ///     .0
673    ///     .ln_1_plus_x_round_ref(Ceiling);
674    /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
675    /// assert_eq!(o, Greater);
676    ///
677    /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
678    ///     .0
679    ///     .ln_1_plus_x_round_ref(Nearest);
680    /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
681    /// assert_eq!(o, Greater);
682    /// ```
683    #[inline]
684    pub fn ln_1_plus_x_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
685        self.ln_1_plus_x_prec_round_ref(self.significant_bits(), rm)
686    }
687
688    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place, rounding the result to the
689    /// specified precision and with the specified rounding mode. An [`Ordering`] is returned,
690    /// indicating whether the rounded value is less than, equal to, or greater than the exact
691    /// value. Although `NaN`s are not comparable to any [`Float`], whenever this function sets the
692    /// [`Float`] to `NaN` it also returns `Equal`.
693    ///
694    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
695    ///
696    /// See [`RoundingMode`] for a description of the possible rounding modes.
697    ///
698    /// $$
699    /// x \gets \ln(1+x)+\varepsilon.
700    /// $$
701    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
702    ///   0.
703    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
704    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$.
705    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
706    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$.
707    ///
708    /// If the output has a precision, it is `prec`.
709    ///
710    /// See the [`Float::ln_1_plus_x_prec_round`] documentation for information on special cases,
711    /// overflow, and underflow.
712    ///
713    /// If you know you'll be using `Nearest`, consider using [`Float::ln_1_plus_x_prec_assign`]
714    /// instead. If you know that your target precision is the precision of the input, consider
715    /// using [`Float::ln_1_plus_x_round_assign`] instead. If both of these things are true,
716    /// consider using [`Float::ln_1_plus_x_assign`] instead.
717    ///
718    /// # Worst-case complexity
719    /// $T(n, m) = O(n (\log n)^2 \log\log n + m)$
720    ///
721    /// $M(n, m) = O(n \log n + m)$
722    ///
723    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
724    /// `self.significant_bits()`.
725    ///
726    /// # Panics
727    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
728    /// with the given precision. (The result cannot be represented exactly whenever the input is
729    /// finite, nonzero, and greater than $-1$.)
730    ///
731    /// # Examples
732    /// ```
733    /// use malachite_base::rounding_modes::RoundingMode::*;
734    /// use malachite_float::Float;
735    /// use std::cmp::Ordering::*;
736    ///
737    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
738    /// assert_eq!(x.ln_1_plus_x_prec_round_assign(5, Floor), Less);
739    /// assert_eq!(x.to_string(), "2.38");
740    ///
741    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
742    /// assert_eq!(x.ln_1_plus_x_prec_round_assign(5, Ceiling), Greater);
743    /// assert_eq!(x.to_string(), "2.50");
744    ///
745    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
746    /// assert_eq!(x.ln_1_plus_x_prec_round_assign(5, Nearest), Less);
747    /// assert_eq!(x.to_string(), "2.38");
748    ///
749    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
750    /// assert_eq!(x.ln_1_plus_x_prec_round_assign(20, Floor), Less);
751    /// assert_eq!(x.to_string(), "2.3978920");
752    ///
753    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
754    /// assert_eq!(x.ln_1_plus_x_prec_round_assign(20, Ceiling), Greater);
755    /// assert_eq!(x.to_string(), "2.3978958");
756    ///
757    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
758    /// assert_eq!(x.ln_1_plus_x_prec_round_assign(20, Nearest), Greater);
759    /// assert_eq!(x.to_string(), "2.3978958");
760    /// ```
761    #[inline]
762    pub fn ln_1_plus_x_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
763        let (result, o) = core::mem::take(self).ln_1_plus_x_prec_round(prec, rm);
764        *self = result;
765        o
766    }
767
768    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place, rounding the result to the nearest
769    /// value of the specified precision. An [`Ordering`] is returned, indicating whether the
770    /// rounded value is less than, equal to, or greater than the exact value. Although `NaN`s are
771    /// not comparable to any [`Float`], whenever this function sets the [`Float`] to `NaN` it also
772    /// returns `Equal`.
773    ///
774    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
775    ///
776    /// If the result is equidistant from two [`Float`]s with the specified precision, the [`Float`]
777    /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
778    /// the `Nearest` rounding mode.
779    ///
780    /// $$
781    /// x \gets \ln(1+x)+\varepsilon.
782    /// $$
783    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
784    ///   0.
785    /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
786    ///   |\ln(1+x)|\rfloor-p}$.
787    ///
788    /// If the output has a precision, it is `prec`.
789    ///
790    /// See the [`Float::ln_1_plus_x_prec`] documentation for information on special cases,
791    /// overflow, and underflow.
792    ///
793    /// If you want to use a rounding mode other than `Nearest`, consider using
794    /// [`Float::ln_1_plus_x_prec_round_assign`] instead. If you know that your target precision is
795    /// the precision of the input, consider using [`Float::ln_1_plus_x_assign`] instead.
796    ///
797    /// # Worst-case complexity
798    /// $T(n, m) = O(n (\log n)^2 \log\log n + m)$
799    ///
800    /// $M(n, m) = O(n \log n + m)$
801    ///
802    /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
803    /// `self.significant_bits()`.
804    ///
805    /// # Panics
806    /// Panics if `prec` is zero.
807    ///
808    /// # Examples
809    /// ```
810    /// use malachite_float::Float;
811    /// use std::cmp::Ordering::*;
812    ///
813    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
814    /// assert_eq!(x.ln_1_plus_x_prec_assign(5), Less);
815    /// assert_eq!(x.to_string(), "2.38");
816    ///
817    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
818    /// assert_eq!(x.ln_1_plus_x_prec_assign(20), Greater);
819    /// assert_eq!(x.to_string(), "2.3978958");
820    /// ```
821    #[inline]
822    pub fn ln_1_plus_x_prec_assign(&mut self, prec: u64) -> Ordering {
823        self.ln_1_plus_x_prec_round_assign(prec, Nearest)
824    }
825
826    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place, rounding the result with the
827    /// specified rounding mode. An [`Ordering`] is returned, indicating whether the rounded value
828    /// is less than, equal to, or greater than the exact value. Although `NaN`s are not comparable
829    /// to any [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns
830    /// `Equal`.
831    ///
832    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
833    ///
834    /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
835    /// description of the possible rounding modes.
836    ///
837    /// $$
838    /// x \gets \ln(1+x)+\varepsilon.
839    /// $$
840    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
841    ///   0.
842    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
843    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$, where $p$ is the precision of the input.
844    /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
845    ///   2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
846    ///
847    /// If the output has a precision, it is the precision of the input.
848    ///
849    /// See the [`Float::ln_1_plus_x_round`] documentation for information on special cases,
850    /// overflow, and underflow.
851    ///
852    /// If you want to specify an output precision, consider using
853    /// [`Float::ln_1_plus_x_prec_round_assign`] instead. If you know you'll be using the `Nearest`
854    /// rounding mode, consider using [`Float::ln_1_plus_x_assign`] instead.
855    ///
856    /// # Worst-case complexity
857    /// $T(n) = O(n (\log n)^2 \log\log n)$
858    ///
859    /// $M(n) = O(n \log n)$
860    ///
861    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
862    ///
863    /// # Panics
864    /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
865    /// precision. (The result cannot be represented exactly whenever the input is finite, nonzero,
866    /// and greater than $-1$.)
867    ///
868    /// # Examples
869    /// ```
870    /// use malachite_base::rounding_modes::RoundingMode::*;
871    /// use malachite_float::Float;
872    /// use std::cmp::Ordering::*;
873    ///
874    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
875    /// assert_eq!(x.ln_1_plus_x_round_assign(Floor), Less);
876    /// assert_eq!(x.to_string(), "2.3978952727983705440619435779621");
877    ///
878    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
879    /// assert_eq!(x.ln_1_plus_x_round_assign(Ceiling), Greater);
880    /// assert_eq!(x.to_string(), "2.3978952727983705440619435779652");
881    ///
882    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
883    /// assert_eq!(x.ln_1_plus_x_round_assign(Nearest), Greater);
884    /// assert_eq!(x.to_string(), "2.3978952727983705440619435779652");
885    /// ```
886    #[inline]
887    pub fn ln_1_plus_x_round_assign(&mut self, rm: RoundingMode) -> Ordering {
888        let prec = self.significant_bits();
889        self.ln_1_plus_x_prec_round_assign(prec, rm)
890    }
891}
892
893impl Ln1PlusX for Float {
894    type Output = Self;
895
896    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], taking the [`Float`] by value.
897    ///
898    /// If the output has a precision, it is the precision of the input. If the result is
899    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
900    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
901    /// rounding mode.
902    ///
903    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
904    ///
905    /// $$
906    /// f(x) = \ln(1+x)+\varepsilon.
907    /// $$
908    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
909    ///   0.
910    /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
911    ///   |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
912    ///
913    /// Special cases:
914    /// - $f(\text{NaN})=\text{NaN}$
915    /// - $f(\infty)=\infty$
916    /// - $f(-\infty)=\text{NaN}$
917    /// - $f(\pm0.0)=\pm0.0$
918    /// - $f(-1)=-\infty$
919    /// - $f(x)=\text{NaN}$ for $x<-1$
920    ///
921    /// This function cannot overflow, but it can underflow: if $0<f(x)<2^{-2^{30}}$, $2^{-2^{30}}$
922    /// is returned instead.
923    ///
924    /// If you want to use a rounding mode other than `Nearest`, consider using
925    /// [`Float::ln_1_plus_x_round`] instead. If you want to specify the output precision, consider
926    /// using [`Float::ln_1_plus_x_prec`]. If you want both of these things, consider using
927    /// [`Float::ln_1_plus_x_prec_round`].
928    ///
929    /// # Worst-case complexity
930    /// $T(n) = O(n (\log n)^2 \log\log n)$
931    ///
932    /// $M(n) = O(n \log n)$
933    ///
934    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
935    ///
936    /// # Examples
937    /// ```
938    /// use malachite_base::num::arithmetic::traits::Ln1PlusX;
939    /// use malachite_base::num::basic::traits::{
940    ///     Infinity, NaN, NegativeInfinity, NegativeOne, One,
941    /// };
942    /// use malachite_float::Float;
943    ///
944    /// assert!(Float::NAN.ln_1_plus_x().is_nan());
945    /// assert_eq!(Float::INFINITY.ln_1_plus_x(), Float::INFINITY);
946    /// assert!(Float::NEGATIVE_INFINITY.ln_1_plus_x().is_nan());
947    /// assert_eq!(Float::ONE.ln_1_plus_x().to_string(), "0.50");
948    /// assert_eq!(
949    ///     Float::from_unsigned_prec(10u32, 100)
950    ///         .0
951    ///         .ln_1_plus_x()
952    ///         .to_string(),
953    ///     "2.3978952727983705440619435779652"
954    /// );
955    /// assert_eq!(Float::NEGATIVE_ONE.ln_1_plus_x(), Float::NEGATIVE_INFINITY);
956    /// assert!(Float::from_signed_prec(-10, 100).0.ln_1_plus_x().is_nan());
957    /// ```
958    #[inline]
959    fn ln_1_plus_x(self) -> Self {
960        let prec = self.significant_bits();
961        self.ln_1_plus_x_prec(prec).0
962    }
963}
964
965impl Ln1PlusX for &Float {
966    type Output = Float;
967
968    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], taking the [`Float`] by reference.
969    ///
970    /// If the output has a precision, it is the precision of the input. If the result is
971    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
972    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
973    /// rounding mode.
974    ///
975    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
976    ///
977    /// $$
978    /// f(x) = \ln(1+x)+\varepsilon.
979    /// $$
980    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
981    ///   0.
982    /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
983    ///   |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
984    ///
985    /// Special cases:
986    /// - $f(\text{NaN})=\text{NaN}$
987    /// - $f(\infty)=\infty$
988    /// - $f(-\infty)=\text{NaN}$
989    /// - $f(\pm0.0)=\pm0.0$
990    /// - $f(-1)=-\infty$
991    /// - $f(x)=\text{NaN}$ for $x<-1$
992    ///
993    /// This function cannot overflow, but it can underflow: if $0<f(x)<2^{-2^{30}}$, $2^{-2^{30}}$
994    /// is returned instead.
995    ///
996    /// If you want to use a rounding mode other than `Nearest`, consider using
997    /// [`Float::ln_1_plus_x_round_ref`] instead. If you want to specify the output precision,
998    /// consider using [`Float::ln_1_plus_x_prec_ref`]. If you want both of these things, consider
999    /// using [`Float::ln_1_plus_x_prec_round_ref`].
1000    ///
1001    /// # Worst-case complexity
1002    /// $T(n) = O(n (\log n)^2 \log\log n)$
1003    ///
1004    /// $M(n) = O(n \log n)$
1005    ///
1006    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
1007    ///
1008    /// # Examples
1009    /// ```
1010    /// use malachite_base::num::arithmetic::traits::Ln1PlusX;
1011    /// use malachite_base::num::basic::traits::{
1012    ///     Infinity, NaN, NegativeInfinity, NegativeOne, One,
1013    /// };
1014    /// use malachite_float::Float;
1015    ///
1016    /// assert!((&Float::NAN).ln_1_plus_x().is_nan());
1017    /// assert_eq!((&Float::INFINITY).ln_1_plus_x(), Float::INFINITY);
1018    /// assert!((&Float::NEGATIVE_INFINITY).ln_1_plus_x().is_nan());
1019    /// assert_eq!((&Float::ONE).ln_1_plus_x().to_string(), "0.50");
1020    /// assert_eq!(
1021    ///     (&Float::from_unsigned_prec(10u32, 100).0)
1022    ///         .ln_1_plus_x()
1023    ///         .to_string(),
1024    ///     "2.3978952727983705440619435779652"
1025    /// );
1026    /// assert_eq!(
1027    ///     (&Float::NEGATIVE_ONE).ln_1_plus_x(),
1028    ///     Float::NEGATIVE_INFINITY
1029    /// );
1030    /// assert!(
1031    ///     (&Float::from_signed_prec(-10, 100).0)
1032    ///         .ln_1_plus_x()
1033    ///         .is_nan()
1034    /// );
1035    /// ```
1036    #[inline]
1037    fn ln_1_plus_x(self) -> Float {
1038        self.ln_1_plus_x_prec_round_ref(self.significant_bits(), Nearest)
1039            .0
1040    }
1041}
1042
1043impl Ln1PlusXAssign for Float {
1044    /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place.
1045    ///
1046    /// If the output has a precision, it is the precision of the input. If the result is
1047    /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
1048    /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
1049    /// rounding mode.
1050    ///
1051    /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
1052    ///
1053    /// $$
1054    /// x \gets \ln(1+x)+\varepsilon.
1055    /// $$
1056    /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
1057    ///   0.
1058    /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
1059    ///   |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
1060    ///
1061    /// See the [`Float::ln_1_plus_x`] documentation for information on special cases, overflow, and
1062    /// underflow.
1063    ///
1064    /// If you want to use a rounding mode other than `Nearest`, consider using
1065    /// [`Float::ln_1_plus_x_round_assign`] instead. If you want to specify the output precision,
1066    /// consider using [`Float::ln_1_plus_x_prec_assign`]. If you want both of these things,
1067    /// consider using [`Float::ln_1_plus_x_prec_round_assign`].
1068    ///
1069    /// # Worst-case complexity
1070    /// $T(n) = O(n (\log n)^2 \log\log n)$
1071    ///
1072    /// $M(n) = O(n \log n)$
1073    ///
1074    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
1075    ///
1076    /// # Examples
1077    /// ```
1078    /// use malachite_base::num::arithmetic::traits::Ln1PlusXAssign;
1079    /// use malachite_base::num::basic::traits::{
1080    ///     Infinity, NaN, NegativeInfinity, NegativeOne, One,
1081    /// };
1082    /// use malachite_float::Float;
1083    ///
1084    /// let mut x = Float::NAN;
1085    /// x.ln_1_plus_x_assign();
1086    /// assert!(x.is_nan());
1087    ///
1088    /// let mut x = Float::INFINITY;
1089    /// x.ln_1_plus_x_assign();
1090    /// assert_eq!(x, Float::INFINITY);
1091    ///
1092    /// let mut x = Float::NEGATIVE_INFINITY;
1093    /// x.ln_1_plus_x_assign();
1094    /// assert!(x.is_nan());
1095    ///
1096    /// let mut x = Float::ONE;
1097    /// x.ln_1_plus_x_assign();
1098    /// assert_eq!(x.to_string(), "0.50");
1099    ///
1100    /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
1101    /// x.ln_1_plus_x_assign();
1102    /// assert_eq!(x.to_string(), "2.3978952727983705440619435779652");
1103    ///
1104    /// let mut x = Float::NEGATIVE_ONE;
1105    /// x.ln_1_plus_x_assign();
1106    /// assert_eq!(x, Float::NEGATIVE_INFINITY);
1107    ///
1108    /// let mut x = Float::from_signed_prec(-10, 100).0;
1109    /// x.ln_1_plus_x_assign();
1110    /// assert!(x.is_nan());
1111    /// ```
1112    #[inline]
1113    fn ln_1_plus_x_assign(&mut self) {
1114        let prec = self.significant_bits();
1115        self.ln_1_plus_x_prec_round_assign(prec, Nearest);
1116    }
1117}
1118
1119/// Computes the natural logarithm of one plus a primitive float, $\ln(1+x)$. Using this function is
1120/// more accurate than using the primitive float `ln_1p` function (the standard library's `ln_1p` is
1121/// not correctly rounded).
1122///
1123/// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
1124///
1125/// $$
1126/// f(x) = \ln(1+x)+\varepsilon.
1127/// $$
1128/// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1129/// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1130///   |\ln(1+x)|\rfloor-p}$, where $p$ is precision of the output (typically 24 if `T` is a [`f32`]
1131///   and 53 if `T` is a [`f64`], but less if the output is subnormal).
1132///
1133/// Special cases:
1134/// - $f(\text{NaN})=\text{NaN}$
1135/// - $f(\infty)=\infty$
1136/// - $f(-\infty)=\text{NaN}$
1137/// - $f(\pm0.0)=\pm0.0$
1138/// - $f(-1.0)=-\infty$
1139/// - $f(x)=\text{NaN}$ for $x<-1$
1140///
1141/// Neither overflow nor underflow is possible.
1142///
1143/// # Worst-case complexity
1144/// Constant time and additional memory.
1145///
1146/// # Examples
1147/// ```
1148/// use malachite_base::num::basic::traits::NegativeInfinity;
1149/// use malachite_base::num::float::NiceFloat;
1150/// use malachite_float::float::arithmetic::ln_1_plus_x::primitive_float_ln_1_plus_x;
1151///
1152/// assert!(primitive_float_ln_1_plus_x(f32::NAN).is_nan());
1153/// assert_eq!(
1154///     NiceFloat(primitive_float_ln_1_plus_x(f32::INFINITY)),
1155///     NiceFloat(f32::INFINITY)
1156/// );
1157/// assert!(primitive_float_ln_1_plus_x(f32::NEGATIVE_INFINITY).is_nan());
1158/// assert_eq!(
1159///     NiceFloat(primitive_float_ln_1_plus_x(-1.0f32)),
1160///     NiceFloat(f32::NEGATIVE_INFINITY)
1161/// );
1162/// assert!(primitive_float_ln_1_plus_x(-2.0f32).is_nan());
1163/// assert_eq!(
1164///     NiceFloat(primitive_float_ln_1_plus_x(1.0f32)),
1165///     NiceFloat(0.6931472)
1166/// );
1167/// assert_eq!(
1168///     NiceFloat(primitive_float_ln_1_plus_x(7.0f32)),
1169///     NiceFloat(2.0794415)
1170/// );
1171/// ```
1172#[inline]
1173#[allow(clippy::type_repetition_in_bounds)]
1174pub fn primitive_float_ln_1_plus_x<T: PrimitiveFloat>(x: T) -> T
1175where
1176    Float: From<T> + PartialOrd<T>,
1177    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1178{
1179    emulate_float_to_float_fn(Float::ln_1_plus_x_prec, x)
1180}