Skip to main content

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