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_extras::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 == 0 {
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) = O(n (\log n)^2 \log\log n)$
208 ///
209 /// $M(n) = O(n (\log n)^2)$
210 ///
211 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
212 ///
213 /// # Panics
214 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
215 /// with the given precision. (The result cannot be represented exactly whenever the input is
216 /// finite, nonzero, and greater than $-1$.)
217 ///
218 /// # Examples
219 /// ```
220 /// use malachite_base::rounding_modes::RoundingMode::*;
221 /// use malachite_float::Float;
222 /// use std::cmp::Ordering::*;
223 ///
224 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
225 /// .0
226 /// .ln_1_plus_x_prec_round(5, Floor);
227 /// assert_eq!(ln.to_string(), "2.38");
228 /// assert_eq!(o, Less);
229 ///
230 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
231 /// .0
232 /// .ln_1_plus_x_prec_round(5, Ceiling);
233 /// assert_eq!(ln.to_string(), "2.50");
234 /// assert_eq!(o, Greater);
235 ///
236 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
237 /// .0
238 /// .ln_1_plus_x_prec_round(5, Nearest);
239 /// assert_eq!(ln.to_string(), "2.38");
240 /// assert_eq!(o, Less);
241 ///
242 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
243 /// .0
244 /// .ln_1_plus_x_prec_round(20, Floor);
245 /// assert_eq!(ln.to_string(), "2.3978920");
246 /// assert_eq!(o, Less);
247 ///
248 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
249 /// .0
250 /// .ln_1_plus_x_prec_round(20, Ceiling);
251 /// assert_eq!(ln.to_string(), "2.3978958");
252 /// assert_eq!(o, Greater);
253 ///
254 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
255 /// .0
256 /// .ln_1_plus_x_prec_round(20, Nearest);
257 /// assert_eq!(ln.to_string(), "2.3978958");
258 /// assert_eq!(o, Greater);
259 /// ```
260 #[inline]
261 pub fn ln_1_plus_x_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
262 assert_ne!(prec, 0);
263 match self {
264 Self(NaN | Infinity { sign: false }) => (float_nan!(), Equal),
265 float_infinity!() => (float_infinity!(), Equal),
266 // ln_1_plus_x(±0) = ±0
267 Self(Zero { .. }) => (self, Equal),
268 _ => ln_1_plus_x_prec_round_normal(&self, prec, rm),
269 }
270 }
271
272 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result to the specified
273 /// precision and with the specified rounding mode. The [`Float`] is taken by reference. An
274 /// [`Ordering`] is also returned, indicating whether the rounded value is less than, equal to,
275 /// or greater than the exact value. Although `NaN`s are not comparable to any [`Float`],
276 /// whenever this function returns a `NaN` it also returns `Equal`.
277 ///
278 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
279 ///
280 /// See [`RoundingMode`] for a description of the possible rounding modes.
281 ///
282 /// $$
283 /// f(x,p,m) = \ln(1+x)+\varepsilon.
284 /// $$
285 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
286 /// 0.
287 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
288 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$.
289 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
290 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$.
291 ///
292 /// If the output has a precision, it is `prec`.
293 ///
294 /// Special cases:
295 /// - $f(\text{NaN},p,m)=\text{NaN}$
296 /// - $f(\infty,p,m)=\infty$
297 /// - $f(-\infty,p,m)=\text{NaN}$
298 /// - $f(\pm0.0,p,m)=\pm0.0$
299 /// - $f(-1,p,m)=-\infty$
300 /// - $f(x,p,m)=\text{NaN}$ for $x<-1$
301 ///
302 /// This function cannot overflow, but it can underflow:
303 /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
304 /// - If $0<f(x,p,m)<2^{-2^{30}}$, and $m$ is `Ceiling`, `Up`, or `Nearest`, $2^{-2^{30}}$ is
305 /// returned instead.
306 ///
307 /// If you know you'll be using `Nearest`, consider using [`Float::ln_1_plus_x_prec_ref`]
308 /// instead. If you know that your target precision is the precision of the input, consider
309 /// using [`Float::ln_1_plus_x_round_ref`] instead. If both of these things are true, consider
310 /// using `(&Float).ln_1_plus_x()` instead.
311 ///
312 /// # Worst-case complexity
313 /// $T(n) = O(n (\log n)^2 \log\log n)$
314 ///
315 /// $M(n) = O(n (\log n)^2)$
316 ///
317 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
318 ///
319 /// # Panics
320 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
321 /// with the given precision. (The result cannot be represented exactly whenever the input is
322 /// finite, nonzero, and greater than $-1$.)
323 ///
324 /// # Examples
325 /// ```
326 /// use malachite_base::rounding_modes::RoundingMode::*;
327 /// use malachite_float::Float;
328 /// use std::cmp::Ordering::*;
329 ///
330 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
331 /// .0
332 /// .ln_1_plus_x_prec_round_ref(5, Floor);
333 /// assert_eq!(ln.to_string(), "2.38");
334 /// assert_eq!(o, Less);
335 ///
336 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
337 /// .0
338 /// .ln_1_plus_x_prec_round_ref(5, Ceiling);
339 /// assert_eq!(ln.to_string(), "2.50");
340 /// assert_eq!(o, Greater);
341 ///
342 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
343 /// .0
344 /// .ln_1_plus_x_prec_round_ref(5, Nearest);
345 /// assert_eq!(ln.to_string(), "2.38");
346 /// assert_eq!(o, Less);
347 ///
348 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
349 /// .0
350 /// .ln_1_plus_x_prec_round_ref(20, Floor);
351 /// assert_eq!(ln.to_string(), "2.3978920");
352 /// assert_eq!(o, Less);
353 ///
354 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
355 /// .0
356 /// .ln_1_plus_x_prec_round_ref(20, Ceiling);
357 /// assert_eq!(ln.to_string(), "2.3978958");
358 /// assert_eq!(o, Greater);
359 ///
360 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
361 /// .0
362 /// .ln_1_plus_x_prec_round_ref(20, Nearest);
363 /// assert_eq!(ln.to_string(), "2.3978958");
364 /// assert_eq!(o, Greater);
365 /// ```
366 #[inline]
367 pub fn ln_1_plus_x_prec_round_ref(&self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
368 assert_ne!(prec, 0);
369 match self {
370 Self(NaN | Infinity { sign: false }) => (float_nan!(), Equal),
371 float_infinity!() => (float_infinity!(), Equal),
372 Self(Zero { sign }) => (Self(Zero { sign: *sign }), Equal),
373 _ => ln_1_plus_x_prec_round_normal(self, prec, rm),
374 }
375 }
376
377 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result to the nearest value of
378 /// the specified precision. The [`Float`] is taken by value. An [`Ordering`] is also returned,
379 /// indicating whether the rounded value is less than, equal to, or greater than the exact
380 /// value. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
381 /// `NaN` it also returns `Equal`.
382 ///
383 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
384 ///
385 /// If the result is equidistant from two [`Float`]s with the specified precision, the [`Float`]
386 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
387 /// the `Nearest` rounding mode.
388 ///
389 /// $$
390 /// f(x,p) = \ln(1+x)+\varepsilon.
391 /// $$
392 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
393 /// 0.
394 /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
395 /// |\ln(1+x)|\rfloor-p}$.
396 ///
397 /// If the output has a precision, it is `prec`.
398 ///
399 /// Special cases:
400 /// - $f(\text{NaN},p)=\text{NaN}$
401 /// - $f(\infty,p)=\infty$
402 /// - $f(-\infty,p)=\text{NaN}$
403 /// - $f(\pm0.0,p)=\pm0.0$
404 /// - $f(-1,p)=-\infty$
405 /// - $f(x,p)=\text{NaN}$ for $x<-1$
406 ///
407 /// This function cannot overflow, but it can underflow: if $0<f(x,p)<2^{-2^{30}}$,
408 /// $2^{-2^{30}}$ is returned instead.
409 ///
410 /// If you want to use a rounding mode other than `Nearest`, consider using
411 /// [`Float::ln_1_plus_x_prec_round`] instead. If you know that your target precision is the
412 /// precision of the input, consider using [`Float::ln_1_plus_x`] instead.
413 ///
414 /// # Worst-case complexity
415 /// $T(n) = O(n (\log n)^2 \log\log n)$
416 ///
417 /// $M(n) = O(n (\log n)^2)$
418 ///
419 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
420 ///
421 /// # Panics
422 /// Panics if `prec` is zero.
423 ///
424 /// # Examples
425 /// ```
426 /// use malachite_base::num::basic::traits::One;
427 /// use malachite_float::Float;
428 /// use std::cmp::Ordering::*;
429 ///
430 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100).0.ln_1_plus_x_prec(5);
431 /// assert_eq!(ln.to_string(), "2.38");
432 /// assert_eq!(o, Less);
433 ///
434 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100).0.ln_1_plus_x_prec(20);
435 /// assert_eq!(ln.to_string(), "2.3978958");
436 /// assert_eq!(o, Greater);
437 ///
438 /// let (ln, o) = Float::ONE.ln_1_plus_x_prec(20);
439 /// assert_eq!(ln.to_string(), "0.69314671");
440 /// assert_eq!(o, Less);
441 /// ```
442 #[inline]
443 pub fn ln_1_plus_x_prec(self, prec: u64) -> (Self, Ordering) {
444 self.ln_1_plus_x_prec_round(prec, Nearest)
445 }
446
447 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result to the nearest value of
448 /// the specified precision. The [`Float`] is taken by reference. An [`Ordering`] is also
449 /// returned, indicating whether the rounded value is less than, equal to, or greater than the
450 /// exact value. Although `NaN`s are not comparable to any [`Float`], whenever this function
451 /// returns a `NaN` it also returns `Equal`.
452 ///
453 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
454 ///
455 /// If the result is equidistant from two [`Float`]s with the specified precision, the [`Float`]
456 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
457 /// the `Nearest` rounding mode.
458 ///
459 /// $$
460 /// f(x,p) = \ln(1+x)+\varepsilon.
461 /// $$
462 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
463 /// 0.
464 /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
465 /// |\ln(1+x)|\rfloor-p}$.
466 ///
467 /// If the output has a precision, it is `prec`.
468 ///
469 /// Special cases:
470 /// - $f(\text{NaN},p)=\text{NaN}$
471 /// - $f(\infty,p)=\infty$
472 /// - $f(-\infty,p)=\text{NaN}$
473 /// - $f(\pm0.0,p)=\pm0.0$
474 /// - $f(-1,p)=-\infty$
475 /// - $f(x,p)=\text{NaN}$ for $x<-1$
476 ///
477 /// This function cannot overflow, but it can underflow: if $0<f(x,p)<2^{-2^{30}}$,
478 /// $2^{-2^{30}}$ is returned instead.
479 ///
480 /// If you want to use a rounding mode other than `Nearest`, consider using
481 /// [`Float::ln_1_plus_x_prec_round_ref`] instead. If you know that your target precision is the
482 /// precision of the input, consider using `(&Float).ln_1_plus_x()` instead.
483 ///
484 /// # Worst-case complexity
485 /// $T(n) = O(n (\log n)^2 \log\log n)$
486 ///
487 /// $M(n) = O(n (\log n)^2)$
488 ///
489 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
490 ///
491 /// # Panics
492 /// Panics if `prec` is zero.
493 ///
494 /// # Examples
495 /// ```
496 /// use malachite_base::num::basic::traits::One;
497 /// use malachite_float::Float;
498 /// use std::cmp::Ordering::*;
499 ///
500 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
501 /// .0
502 /// .ln_1_plus_x_prec_ref(5);
503 /// assert_eq!(ln.to_string(), "2.38");
504 /// assert_eq!(o, Less);
505 ///
506 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
507 /// .0
508 /// .ln_1_plus_x_prec_ref(20);
509 /// assert_eq!(ln.to_string(), "2.3978958");
510 /// assert_eq!(o, Greater);
511 ///
512 /// let (ln, o) = Float::ONE.ln_1_plus_x_prec_ref(20);
513 /// assert_eq!(ln.to_string(), "0.69314671");
514 /// assert_eq!(o, Less);
515 /// ```
516 #[inline]
517 pub fn ln_1_plus_x_prec_ref(&self, prec: u64) -> (Self, Ordering) {
518 self.ln_1_plus_x_prec_round_ref(prec, Nearest)
519 }
520
521 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result with the specified
522 /// rounding mode. The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating
523 /// whether the rounded value is less than, equal to, or greater than the exact value. Although
524 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
525 /// returns `Equal`.
526 ///
527 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
528 ///
529 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
530 /// description of the possible rounding modes.
531 ///
532 /// $$
533 /// f(x,m) = \ln(1+x)+\varepsilon.
534 /// $$
535 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
536 /// 0.
537 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
538 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$, where $p$ is the precision of the input.
539 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
540 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
541 ///
542 /// If the output has a precision, it is the precision of the input.
543 ///
544 /// Special cases:
545 /// - $f(\text{NaN},m)=\text{NaN}$
546 /// - $f(\infty,m)=\infty$
547 /// - $f(-\infty,m)=\text{NaN}$
548 /// - $f(\pm0.0,m)=\pm0.0$
549 /// - $f(-1,m)=-\infty$
550 /// - $f(x,m)=\text{NaN}$ for $x<-1$
551 ///
552 /// This function cannot overflow, but it can underflow:
553 /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
554 /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Ceiling`, `Up`, or `Nearest`, $2^{-2^{30}}$ is
555 /// returned instead.
556 ///
557 /// If you want to specify an output precision, consider using [`Float::ln_1_plus_x_prec_round`]
558 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
559 /// [`Float::ln_1_plus_x`] instead.
560 ///
561 /// # Worst-case complexity
562 /// $T(n) = O(n (\log n)^2 \log\log n)$
563 ///
564 /// $M(n) = O(n (\log n)^2)$
565 ///
566 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
567 ///
568 /// # Panics
569 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
570 /// precision. (The result cannot be represented exactly whenever the input is finite, nonzero,
571 /// and greater than $-1$.)
572 ///
573 /// # Examples
574 /// ```
575 /// use malachite_base::rounding_modes::RoundingMode::*;
576 /// use malachite_float::Float;
577 /// use std::cmp::Ordering::*;
578 ///
579 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
580 /// .0
581 /// .ln_1_plus_x_round(Floor);
582 /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779621");
583 /// assert_eq!(o, Less);
584 ///
585 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
586 /// .0
587 /// .ln_1_plus_x_round(Ceiling);
588 /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
589 /// assert_eq!(o, Greater);
590 ///
591 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
592 /// .0
593 /// .ln_1_plus_x_round(Nearest);
594 /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
595 /// assert_eq!(o, Greater);
596 /// ```
597 #[inline]
598 pub fn ln_1_plus_x_round(self, rm: RoundingMode) -> (Self, Ordering) {
599 let prec = self.significant_bits();
600 self.ln_1_plus_x_prec_round(prec, rm)
601 }
602
603 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], rounding the result with the specified
604 /// rounding mode. The [`Float`] is taken by reference. An [`Ordering`] is also returned,
605 /// indicating whether the rounded value is less than, equal to, or greater than the exact
606 /// value. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
607 /// `NaN` it also returns `Equal`.
608 ///
609 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
610 ///
611 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
612 /// description of the possible rounding modes.
613 ///
614 /// $$
615 /// f(x,m) = \ln(1+x)+\varepsilon.
616 /// $$
617 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
618 /// 0.
619 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
620 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$, where $p$ is the precision of the input.
621 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
622 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
623 ///
624 /// If the output has a precision, it is the precision of the input.
625 ///
626 /// Special cases:
627 /// - $f(\text{NaN},m)=\text{NaN}$
628 /// - $f(\infty,m)=\infty$
629 /// - $f(-\infty,m)=\text{NaN}$
630 /// - $f(\pm0.0,m)=\pm0.0$
631 /// - $f(-1,m)=-\infty$
632 /// - $f(x,m)=\text{NaN}$ for $x<-1$
633 ///
634 /// This function cannot overflow, but it can underflow:
635 /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
636 /// - If $0<f(x,m)<2^{-2^{30}}$, and $m$ is `Ceiling`, `Up`, or `Nearest`, $2^{-2^{30}}$ is
637 /// returned instead.
638 ///
639 /// If you want to specify an output precision, consider using
640 /// [`Float::ln_1_plus_x_prec_round_ref`] instead. If you know you'll be using the `Nearest`
641 /// rounding mode, consider using `(&Float).ln_1_plus_x()` instead.
642 ///
643 /// # Worst-case complexity
644 /// $T(n) = O(n (\log n)^2 \log\log n)$
645 ///
646 /// $M(n) = O(n (\log n)^2)$
647 ///
648 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
649 ///
650 /// # Panics
651 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
652 /// precision. (The result cannot be represented exactly whenever the input is finite, nonzero,
653 /// and greater than $-1$.)
654 ///
655 /// # Examples
656 /// ```
657 /// use malachite_base::rounding_modes::RoundingMode::*;
658 /// use malachite_float::Float;
659 /// use std::cmp::Ordering::*;
660 ///
661 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
662 /// .0
663 /// .ln_1_plus_x_round_ref(Floor);
664 /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779621");
665 /// assert_eq!(o, Less);
666 ///
667 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
668 /// .0
669 /// .ln_1_plus_x_round_ref(Ceiling);
670 /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
671 /// assert_eq!(o, Greater);
672 ///
673 /// let (ln, o) = Float::from_unsigned_prec(10u32, 100)
674 /// .0
675 /// .ln_1_plus_x_round_ref(Nearest);
676 /// assert_eq!(ln.to_string(), "2.3978952727983705440619435779652");
677 /// assert_eq!(o, Greater);
678 /// ```
679 #[inline]
680 pub fn ln_1_plus_x_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
681 self.ln_1_plus_x_prec_round_ref(self.significant_bits(), rm)
682 }
683
684 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place, rounding the result to the
685 /// specified precision and with the specified rounding mode. An [`Ordering`] is returned,
686 /// indicating whether the rounded value is less than, equal to, or greater than the exact
687 /// value. Although `NaN`s are not comparable to any [`Float`], whenever this function sets the
688 /// [`Float`] to `NaN` it also returns `Equal`.
689 ///
690 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
691 ///
692 /// See [`RoundingMode`] for a description of the possible rounding modes.
693 ///
694 /// $$
695 /// x \gets \ln(1+x)+\varepsilon.
696 /// $$
697 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
698 /// 0.
699 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
700 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$.
701 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
702 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$.
703 ///
704 /// If the output has a precision, it is `prec`.
705 ///
706 /// See the [`Float::ln_1_plus_x_prec_round`] documentation for information on special cases,
707 /// overflow, and underflow.
708 ///
709 /// If you know you'll be using `Nearest`, consider using [`Float::ln_1_plus_x_prec_assign`]
710 /// instead. If you know that your target precision is the precision of the input, consider
711 /// using [`Float::ln_1_plus_x_round_assign`] instead. If both of these things are true,
712 /// consider using [`Float::ln_1_plus_x_assign`] instead.
713 ///
714 /// # Worst-case complexity
715 /// $T(n) = O(n (\log n)^2 \log\log n)$
716 ///
717 /// $M(n) = O(n (\log n)^2)$
718 ///
719 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
720 ///
721 /// # Panics
722 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
723 /// with the given precision. (The result cannot be represented exactly whenever the input is
724 /// finite, nonzero, and greater than $-1$.)
725 ///
726 /// # Examples
727 /// ```
728 /// use malachite_base::rounding_modes::RoundingMode::*;
729 /// use malachite_float::Float;
730 /// use std::cmp::Ordering::*;
731 ///
732 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
733 /// assert_eq!(x.ln_1_plus_x_prec_round_assign(5, Floor), Less);
734 /// assert_eq!(x.to_string(), "2.38");
735 ///
736 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
737 /// assert_eq!(x.ln_1_plus_x_prec_round_assign(5, Ceiling), Greater);
738 /// assert_eq!(x.to_string(), "2.50");
739 ///
740 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
741 /// assert_eq!(x.ln_1_plus_x_prec_round_assign(5, Nearest), Less);
742 /// assert_eq!(x.to_string(), "2.38");
743 ///
744 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
745 /// assert_eq!(x.ln_1_plus_x_prec_round_assign(20, Floor), Less);
746 /// assert_eq!(x.to_string(), "2.3978920");
747 ///
748 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
749 /// assert_eq!(x.ln_1_plus_x_prec_round_assign(20, Ceiling), Greater);
750 /// assert_eq!(x.to_string(), "2.3978958");
751 ///
752 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
753 /// assert_eq!(x.ln_1_plus_x_prec_round_assign(20, Nearest), Greater);
754 /// assert_eq!(x.to_string(), "2.3978958");
755 /// ```
756 #[inline]
757 pub fn ln_1_plus_x_prec_round_assign(&mut self, prec: u64, rm: RoundingMode) -> Ordering {
758 let (result, o) = core::mem::take(self).ln_1_plus_x_prec_round(prec, rm);
759 *self = result;
760 o
761 }
762
763 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place, rounding the result to the nearest
764 /// value of the specified precision. An [`Ordering`] is returned, indicating whether the
765 /// rounded value is less than, equal to, or greater than the exact value. Although `NaN`s are
766 /// not comparable to any [`Float`], whenever this function sets the [`Float`] to `NaN` it also
767 /// returns `Equal`.
768 ///
769 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
770 ///
771 /// If the result is equidistant from two [`Float`]s with the specified precision, the [`Float`]
772 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
773 /// the `Nearest` rounding mode.
774 ///
775 /// $$
776 /// x \gets \ln(1+x)+\varepsilon.
777 /// $$
778 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
779 /// 0.
780 /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
781 /// |\ln(1+x)|\rfloor-p}$.
782 ///
783 /// If the output has a precision, it is `prec`.
784 ///
785 /// See the [`Float::ln_1_plus_x_prec`] documentation for information on special cases,
786 /// overflow, and underflow.
787 ///
788 /// If you want to use a rounding mode other than `Nearest`, consider using
789 /// [`Float::ln_1_plus_x_prec_round_assign`] instead. If you know that your target precision is
790 /// the precision of the input, consider using [`Float::ln_1_plus_x_assign`] instead.
791 ///
792 /// # Worst-case complexity
793 /// $T(n) = O(n (\log n)^2 \log\log n)$
794 ///
795 /// $M(n) = O(n (\log n)^2)$
796 ///
797 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
798 ///
799 /// # Panics
800 /// Panics if `prec` is zero.
801 ///
802 /// # Examples
803 /// ```
804 /// use malachite_float::Float;
805 /// use std::cmp::Ordering::*;
806 ///
807 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
808 /// assert_eq!(x.ln_1_plus_x_prec_assign(5), Less);
809 /// assert_eq!(x.to_string(), "2.38");
810 ///
811 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
812 /// assert_eq!(x.ln_1_plus_x_prec_assign(20), Greater);
813 /// assert_eq!(x.to_string(), "2.3978958");
814 /// ```
815 #[inline]
816 pub fn ln_1_plus_x_prec_assign(&mut self, prec: u64) -> Ordering {
817 self.ln_1_plus_x_prec_round_assign(prec, Nearest)
818 }
819
820 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place, rounding the result with the
821 /// specified rounding mode. An [`Ordering`] is returned, indicating whether the rounded value
822 /// is less than, equal to, or greater than the exact value. Although `NaN`s are not comparable
823 /// to any [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns
824 /// `Equal`.
825 ///
826 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
827 ///
828 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
829 /// description of the possible rounding modes.
830 ///
831 /// $$
832 /// x \gets \ln(1+x)+\varepsilon.
833 /// $$
834 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
835 /// 0.
836 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
837 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p+1}$, where $p$ is the precision of the input.
838 /// - If $\ln(1+x)$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
839 /// 2^{\lfloor\log_2 |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
840 ///
841 /// If the output has a precision, it is the precision of the input.
842 ///
843 /// See the [`Float::ln_1_plus_x_round`] documentation for information on special cases,
844 /// overflow, and underflow.
845 ///
846 /// If you want to specify an output precision, consider using
847 /// [`Float::ln_1_plus_x_prec_round_assign`] instead. If you know you'll be using the `Nearest`
848 /// rounding mode, consider using [`Float::ln_1_plus_x_assign`] instead.
849 ///
850 /// # Worst-case complexity
851 /// $T(n) = O(n (\log n)^2 \log\log n)$
852 ///
853 /// $M(n) = O(n (\log n)^2)$
854 ///
855 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
856 ///
857 /// # Panics
858 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
859 /// precision. (The result cannot be represented exactly whenever the input is finite, nonzero,
860 /// and greater than $-1$.)
861 ///
862 /// # Examples
863 /// ```
864 /// use malachite_base::rounding_modes::RoundingMode::*;
865 /// use malachite_float::Float;
866 /// use std::cmp::Ordering::*;
867 ///
868 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
869 /// assert_eq!(x.ln_1_plus_x_round_assign(Floor), Less);
870 /// assert_eq!(x.to_string(), "2.3978952727983705440619435779621");
871 ///
872 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
873 /// assert_eq!(x.ln_1_plus_x_round_assign(Ceiling), Greater);
874 /// assert_eq!(x.to_string(), "2.3978952727983705440619435779652");
875 ///
876 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
877 /// assert_eq!(x.ln_1_plus_x_round_assign(Nearest), Greater);
878 /// assert_eq!(x.to_string(), "2.3978952727983705440619435779652");
879 /// ```
880 #[inline]
881 pub fn ln_1_plus_x_round_assign(&mut self, rm: RoundingMode) -> Ordering {
882 let prec = self.significant_bits();
883 self.ln_1_plus_x_prec_round_assign(prec, rm)
884 }
885}
886
887impl Ln1PlusX for Float {
888 type Output = Self;
889
890 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], taking the [`Float`] by value.
891 ///
892 /// If the output has a precision, it is the precision of the input. If the result is
893 /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
894 /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
895 /// rounding mode.
896 ///
897 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
898 ///
899 /// $$
900 /// f(x) = \ln(1+x)+\varepsilon.
901 /// $$
902 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
903 /// 0.
904 /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
905 /// |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
906 ///
907 /// Special cases:
908 /// - $f(\text{NaN})=\text{NaN}$
909 /// - $f(\infty)=\infty$
910 /// - $f(-\infty)=\text{NaN}$
911 /// - $f(\pm0.0)=\pm0.0$
912 /// - $f(-1)=-\infty$
913 /// - $f(x)=\text{NaN}$ for $x<-1$
914 ///
915 /// This function cannot overflow, but it can underflow: if $0<f(x)<2^{-2^{30}}$, $2^{-2^{30}}$
916 /// is returned instead.
917 ///
918 /// If you want to use a rounding mode other than `Nearest`, consider using
919 /// [`Float::ln_1_plus_x_round`] instead. If you want to specify the output precision, consider
920 /// using [`Float::ln_1_plus_x_prec`]. If you want both of these things, consider using
921 /// [`Float::ln_1_plus_x_prec_round`].
922 ///
923 /// # Worst-case complexity
924 /// $T(n) = O(n (\log n)^2 \log\log n)$
925 ///
926 /// $M(n) = O(n (\log n)^2)$
927 ///
928 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
929 ///
930 /// # Examples
931 /// ```
932 /// use malachite_base::num::arithmetic::traits::Ln1PlusX;
933 /// use malachite_base::num::basic::traits::{
934 /// Infinity, NaN, NegativeInfinity, NegativeOne, One,
935 /// };
936 /// use malachite_float::Float;
937 ///
938 /// assert!(Float::NAN.ln_1_plus_x().is_nan());
939 /// assert_eq!(Float::INFINITY.ln_1_plus_x(), Float::INFINITY);
940 /// assert!(Float::NEGATIVE_INFINITY.ln_1_plus_x().is_nan());
941 /// assert_eq!(Float::ONE.ln_1_plus_x().to_string(), "0.50");
942 /// assert_eq!(
943 /// Float::from_unsigned_prec(10u32, 100)
944 /// .0
945 /// .ln_1_plus_x()
946 /// .to_string(),
947 /// "2.3978952727983705440619435779652"
948 /// );
949 /// assert_eq!(Float::NEGATIVE_ONE.ln_1_plus_x(), Float::NEGATIVE_INFINITY);
950 /// assert!(Float::from_signed_prec(-10, 100).0.ln_1_plus_x().is_nan());
951 /// ```
952 #[inline]
953 fn ln_1_plus_x(self) -> Self {
954 let prec = self.significant_bits();
955 self.ln_1_plus_x_prec(prec).0
956 }
957}
958
959impl Ln1PlusX for &Float {
960 type Output = Float;
961
962 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], taking the [`Float`] by reference.
963 ///
964 /// If the output has a precision, it is the precision of the input. If the result is
965 /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
966 /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
967 /// rounding mode.
968 ///
969 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
970 ///
971 /// $$
972 /// f(x) = \ln(1+x)+\varepsilon.
973 /// $$
974 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
975 /// 0.
976 /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
977 /// |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
978 ///
979 /// Special cases:
980 /// - $f(\text{NaN})=\text{NaN}$
981 /// - $f(\infty)=\infty$
982 /// - $f(-\infty)=\text{NaN}$
983 /// - $f(\pm0.0)=\pm0.0$
984 /// - $f(-1)=-\infty$
985 /// - $f(x)=\text{NaN}$ for $x<-1$
986 ///
987 /// This function cannot overflow, but it can underflow: if $0<f(x)<2^{-2^{30}}$, $2^{-2^{30}}$
988 /// is returned instead.
989 ///
990 /// If you want to use a rounding mode other than `Nearest`, consider using
991 /// [`Float::ln_1_plus_x_round_ref`] instead. If you want to specify the output precision,
992 /// consider using [`Float::ln_1_plus_x_prec_ref`]. If you want both of these things, consider
993 /// using [`Float::ln_1_plus_x_prec_round_ref`].
994 ///
995 /// # Worst-case complexity
996 /// $T(n) = O(n (\log n)^2 \log\log n)$
997 ///
998 /// $M(n) = O(n (\log n)^2)$
999 ///
1000 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
1001 ///
1002 /// # Examples
1003 /// ```
1004 /// use malachite_base::num::arithmetic::traits::Ln1PlusX;
1005 /// use malachite_base::num::basic::traits::{
1006 /// Infinity, NaN, NegativeInfinity, NegativeOne, One,
1007 /// };
1008 /// use malachite_float::Float;
1009 ///
1010 /// assert!((&Float::NAN).ln_1_plus_x().is_nan());
1011 /// assert_eq!((&Float::INFINITY).ln_1_plus_x(), Float::INFINITY);
1012 /// assert!((&Float::NEGATIVE_INFINITY).ln_1_plus_x().is_nan());
1013 /// assert_eq!((&Float::ONE).ln_1_plus_x().to_string(), "0.50");
1014 /// assert_eq!(
1015 /// (&Float::from_unsigned_prec(10u32, 100).0)
1016 /// .ln_1_plus_x()
1017 /// .to_string(),
1018 /// "2.3978952727983705440619435779652"
1019 /// );
1020 /// assert_eq!(
1021 /// (&Float::NEGATIVE_ONE).ln_1_plus_x(),
1022 /// Float::NEGATIVE_INFINITY
1023 /// );
1024 /// assert!((&Float::from_signed_prec(-10, 100).0)
1025 /// .ln_1_plus_x()
1026 /// .is_nan());
1027 /// ```
1028 #[inline]
1029 fn ln_1_plus_x(self) -> Float {
1030 self.ln_1_plus_x_prec_round_ref(self.significant_bits(), Nearest)
1031 .0
1032 }
1033}
1034
1035impl Ln1PlusXAssign for Float {
1036 /// Computes $\ln(1+x)$, where $x$ is a [`Float`], in place.
1037 ///
1038 /// If the output has a precision, it is the precision of the input. If the result is
1039 /// equidistant from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in
1040 /// its binary expansion is chosen. See [`RoundingMode`] for a description of the `Nearest`
1041 /// rounding mode.
1042 ///
1043 /// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, the [`Float`] is set to `NaN`.
1044 ///
1045 /// $$
1046 /// x \gets \ln(1+x)+\varepsilon.
1047 /// $$
1048 /// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be
1049 /// 0.
1050 /// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
1051 /// |\ln(1+x)|\rfloor-p}$, where $p$ is the precision of the input.
1052 ///
1053 /// See the [`Float::ln_1_plus_x`] documentation for information on special cases, overflow, and
1054 /// underflow.
1055 ///
1056 /// If you want to use a rounding mode other than `Nearest`, consider using
1057 /// [`Float::ln_1_plus_x_round_assign`] instead. If you want to specify the output precision,
1058 /// consider using [`Float::ln_1_plus_x_prec_assign`]. If you want both of these things,
1059 /// consider using [`Float::ln_1_plus_x_prec_round_assign`].
1060 ///
1061 /// # Worst-case complexity
1062 /// $T(n) = O(n (\log n)^2 \log\log n)$
1063 ///
1064 /// $M(n) = O(n (\log n)^2)$
1065 ///
1066 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.get_prec()`.
1067 ///
1068 /// # Examples
1069 /// ```
1070 /// use malachite_base::num::arithmetic::traits::Ln1PlusXAssign;
1071 /// use malachite_base::num::basic::traits::{
1072 /// Infinity, NaN, NegativeInfinity, NegativeOne, One,
1073 /// };
1074 /// use malachite_float::Float;
1075 ///
1076 /// let mut x = Float::NAN;
1077 /// x.ln_1_plus_x_assign();
1078 /// assert!(x.is_nan());
1079 ///
1080 /// let mut x = Float::INFINITY;
1081 /// x.ln_1_plus_x_assign();
1082 /// assert_eq!(x, Float::INFINITY);
1083 ///
1084 /// let mut x = Float::NEGATIVE_INFINITY;
1085 /// x.ln_1_plus_x_assign();
1086 /// assert!(x.is_nan());
1087 ///
1088 /// let mut x = Float::ONE;
1089 /// x.ln_1_plus_x_assign();
1090 /// assert_eq!(x.to_string(), "0.50");
1091 ///
1092 /// let mut x = Float::from_unsigned_prec(10u32, 100).0;
1093 /// x.ln_1_plus_x_assign();
1094 /// assert_eq!(x.to_string(), "2.3978952727983705440619435779652");
1095 ///
1096 /// let mut x = Float::NEGATIVE_ONE;
1097 /// x.ln_1_plus_x_assign();
1098 /// assert_eq!(x, Float::NEGATIVE_INFINITY);
1099 ///
1100 /// let mut x = Float::from_signed_prec(-10, 100).0;
1101 /// x.ln_1_plus_x_assign();
1102 /// assert!(x.is_nan());
1103 /// ```
1104 #[inline]
1105 fn ln_1_plus_x_assign(&mut self) {
1106 let prec = self.significant_bits();
1107 self.ln_1_plus_x_prec_round_assign(prec, Nearest);
1108 }
1109}
1110
1111/// Computes the natural logarithm of one plus a primitive float, $\ln(1+x)$. Using this function is
1112/// more accurate than using the primitive float `ln_1p` function (the standard library's `ln_1p` is
1113/// not correctly rounded).
1114///
1115/// $\ln(1+x)$ is undefined for $x<-1$, so whenever $x<-1$, `NaN` is returned.
1116///
1117/// $$
1118/// f(x) = \ln(1+x)+\varepsilon.
1119/// $$
1120/// - If $\ln(1+x)$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1121/// - If $\ln(1+x)$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2
1122/// |\ln(1+x)|\rfloor-p}$, where $p$ is precision of the output (typically 24 if `T` is a [`f32`]
1123/// and 53 if `T` is a [`f64`], but less if the output is subnormal).
1124///
1125/// Special cases:
1126/// - $f(\text{NaN})=\text{NaN}$
1127/// - $f(\infty)=\infty$
1128/// - $f(-\infty)=\text{NaN}$
1129/// - $f(\pm0.0)=\pm0.0$
1130/// - $f(-1.0)=-\infty$
1131/// - $f(x)=\text{NaN}$ for $x<-1$
1132///
1133/// Neither overflow nor underflow is possible.
1134///
1135/// # Worst-case complexity
1136/// Constant time and additional memory.
1137///
1138/// # Examples
1139/// ```
1140/// use malachite_base::num::basic::traits::NegativeInfinity;
1141/// use malachite_base::num::float::NiceFloat;
1142/// use malachite_float::float::arithmetic::ln_1_plus_x::primitive_float_ln_1_plus_x;
1143///
1144/// assert!(primitive_float_ln_1_plus_x(f32::NAN).is_nan());
1145/// assert_eq!(
1146/// NiceFloat(primitive_float_ln_1_plus_x(f32::INFINITY)),
1147/// NiceFloat(f32::INFINITY)
1148/// );
1149/// assert!(primitive_float_ln_1_plus_x(f32::NEGATIVE_INFINITY).is_nan());
1150/// assert_eq!(
1151/// NiceFloat(primitive_float_ln_1_plus_x(-1.0f32)),
1152/// NiceFloat(f32::NEGATIVE_INFINITY)
1153/// );
1154/// assert!(primitive_float_ln_1_plus_x(-2.0f32).is_nan());
1155/// assert_eq!(
1156/// NiceFloat(primitive_float_ln_1_plus_x(1.0f32)),
1157/// NiceFloat(0.6931472)
1158/// );
1159/// assert_eq!(
1160/// NiceFloat(primitive_float_ln_1_plus_x(7.0f32)),
1161/// NiceFloat(2.0794415)
1162/// );
1163/// ```
1164#[inline]
1165#[allow(clippy::type_repetition_in_bounds)]
1166pub fn primitive_float_ln_1_plus_x<T: PrimitiveFloat>(x: T) -> T
1167where
1168 Float: From<T> + PartialOrd<T>,
1169 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1170{
1171 emulate_float_to_float_fn(Float::ln_1_plus_x_prec, x)
1172}