Skip to main content

malachite_float/float/arithmetic/
power_of_10_x_minus_1.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5//      Copyright 2001-2025 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::TWICE_WIDTH;
17use crate::float::arithmetic::exp::{exp_overflow, one_neighbor};
18use crate::float::arithmetic::exp_x_minus_1::exp_x_minus_1_rational_near_zero;
19use crate::float::arithmetic::round_near_x::float_round_near_x;
20use crate::{
21    Float, emulate_float_to_float_fn, emulate_rational_to_float_fn, float_infinity, float_nan,
22    float_zero, floor_and_ceiling,
23};
24use core::cmp::Ordering::{self, *};
25use core::cmp::max;
26use malachite_base::fail_on_untested_path;
27use malachite_base::num::arithmetic::traits::{
28    CeilingLogBase2, Pow, PowerOf10XMinus1, PowerOf10XMinus1Assign, Reciprocal,
29};
30use malachite_base::num::basic::floats::PrimitiveFloat;
31use malachite_base::num::basic::integers::PrimitiveInt;
32use malachite_base::num::basic::traits::{NegativeOne, One};
33use malachite_base::num::comparison::traits::PartialOrdAbs;
34use malachite_base::num::conversion::traits::{ExactFrom, IsInteger, RoundingFrom};
35use malachite_base::num::logic::traits::SignificantBits;
36use malachite_base::rounding_modes::RoundingMode::{self, *};
37use malachite_nz::integer::Integer;
38use malachite_nz::natural::arithmetic::float_extras::float_can_round;
39use malachite_nz::platform::Limb;
40use malachite_q::Rational;
41
42const TEN: Rational = Rational::const_from_unsigned(10);
43
44// The outcome of the "small |x|" branch of `mpfr_exp10m1`.
45enum Small {
46    // x * ln(10) enables correct rounding; the contained `Float` is the approximation to round.
47    Round(Float),
48    // The small approximation did not enable correct rounding.
49    NotSmall,
50}
51
52// Decides `x * log2(10) >= bound` exactly, where `x` is a nonzero `Rational` and `bound` an
53// integer. Since log2(10) is irrational and `x` is a nonzero rational, `x * log2(10)` is irrational
54// and hence never equals the integer `bound`, so the widening bracket always resolves. Used to
55// place a `Float` x against the overflow (10^x >= 2^MAX_EXPONENT) and underflow (10^x <
56// 2^MIN_EXPONENT) boundaries of 10^x, which occur at x = MAX_EXPONENT / log2(10) and x =
57// MIN_EXPONENT / log2(10) respectively.
58fn x_log_2_10_ge(x: &Rational, bound: i64) -> bool {
59    let bound = Rational::from(bound);
60    let positive = *x > 0u32;
61    let mut p = TWICE_WIDTH;
62    loop {
63        let (lo, hi) = floor_and_ceiling(Float::log_2_10_prec_round(p, Floor));
64        let lo = Rational::exact_from(lo);
65        let hi = Rational::exact_from(hi);
66        // log2(10) in [lo, hi], so x * log2(10) in [a_lo, a_hi].
67        let (a_lo, a_hi) = if positive {
68            (x * lo, x * hi)
69        } else {
70            (x * hi, x * lo)
71        };
72        if a_lo >= bound {
73            return true;
74        }
75        if a_hi < bound {
76            return false;
77        }
78        p <<= 1;
79    }
80}
81
82// In case x is small in absolute value, 10^x - 1 ~ x * ln(10). If this is enough to deduce correct
83// rounding, return the approximation that will be rounded to get the result; otherwise signal that
84// the small case does not apply. (Unlike 2^x - 1, no underflow can occur, since ln(10) > 1, so x *
85// ln(10) is always at least as large as x in magnitude and hence representable.)
86//
87// This is mpfr_exp10m1_small from exp10m1.c, MPFR 4.3.0.
88fn power_of_10_x_minus_1_small(x: &Float, prec: u64, working_prec: u64, rm: RoundingMode) -> Small {
89    let ex = i64::from(x.get_exponent().unwrap());
90    // For |x| < 0.25, we have |10^x - 1 - x * ln(10)| < 4 * x^2. Otherwise the approximation is not
91    // accurate enough.
92    if ex > -2 {
93        return Small::NotSmall;
94    }
95    // t = ln(10) * x * (1 + theta)^2 with |theta| <= 2^(-working_prec).
96    let t = Float::ln_10_prec(working_prec)
97        .0
98        .mul_prec_val_ref(x, working_prec)
99        .0;
100    let exp_t = i64::from(t.get_exponent().unwrap());
101    // |t - x * ln(10)| < 3 * 2^(EXP(t) - working_prec), and |4 * x^2| < 2^e * 2^(EXP(t) -
102    // working_prec), so |t - (10^x - 1)| < 2^e * 2^(EXP(t) - working_prec).
103    let e = (ex << 1) + 2 + i64::exact_from(working_prec) - exp_t;
104    let e = if e <= 1 { 2 + i64::from(e == 1) } else { e + 1 };
105    if float_can_round(
106        t.significand_ref().unwrap(),
107        working_prec - u64::exact_from(e),
108        prec,
109        rm,
110    ) {
111        Small::Round(t)
112    } else {
113        Small::NotSmall
114    }
115}
116
117// This is mpfr_exp10m1 from exp10m1.c, MPFR 4.3.0, where the input is finite and nonzero.
118fn power_of_10_x_minus_1_prec_round_normal(
119    x: &Float,
120    prec: u64,
121    rm: RoundingMode,
122) -> (Float, Ordering) {
123    // Huge negative x: if |x| > 2 + (prec - 1) / 3 then 3 |x| >= prec + 3, so 10^x < 8^x <=
124    // 2^(-prec-3) <= (1/2) ulp(-1); thus 10^x - 1 rounds to -1 (for Floor, Up, and Nearest) or to
125    // nextabove(-1) (for Ceiling and Down).
126    if x.is_sign_negative() && x.gt_abs(&(2 + (prec - 1) / 3)) {
127        return match rm {
128            Ceiling | Down => (-one_neighbor(prec, false), Greater),
129            Floor | Up | Nearest => (Float::negative_one_prec(prec), Less),
130            Exact => panic!("Inexact power_of_10_x_minus_1"),
131        };
132    }
133    // Deeply negative x: 10^x lies below the smallest positive Float (x * log2(10) < MIN_EXPONENT),
134    // so the loop's `power_of_10` would return 0 or the minimum positive value with unbounded
135    // relative error, and the result could never be certified. Since the huge-negative shortcut
136    // above did not fire, |x| <= 2 + (prec - 1) / 3, so the bits of 10^x land within the output's
137    // prec-bit window and must be computed for real. This is only reachable at enormous prec: a
138    // sub-`MIN_EXPONENT` 10^x needs |x| > -MIN_EXPONENT / log2(10) > -MIN_EXPONENT / 4, and the
139    // shortcut leaves only |x| <= 2 + (prec - 1) / 3. The cheap `prec` gate below skips the (up to
140    // 128 MB) `Rational::exact_from(x)` in the common case, where the branch cannot fire.
141    if x.is_sign_negative()
142        && 2 + (prec - 1) / 3 >= const { Float::MAX_EXPONENT_U64 >> 2 }
143        && !x_log_2_10_ge(&Rational::exact_from(x), Float::MIN_EXPONENT_I64)
144    {
145        return power_of_10_x_minus_1_deep_negative(x, prec, rm);
146    }
147    // Compute the precision of the intermediary variable: the optimal number of bits, see
148    // algorithms.tex.
149    let mut working_prec = prec + prec.ceiling_log_base_2() + 6;
150    let mut increment = Limb::WIDTH;
151    loop {
152        // 10^x may overflow.
153        let (mut t, o1) = Float::power_of_10_of_float_prec_ref(x, working_prec);
154        if t.is_infinite() {
155            // 10^x overflowed at the working precision. For prec < MAX_EXPONENT this decides the
156            // result: 10^x >= (1 - 2^-working_prec) * 2^MAX_EXPONENT, so 10^x - 1 exceeds the
157            // largest prec-bit value and rounds exactly as an overflow does. At prec >=
158            // MAX_EXPONENT the values just below 2^MAX_EXPONENT are representable, and the
159            // intermediate overflow no longer implies one in the result:
160            // - x * log2(10) >= MAX_EXPONENT: a true overflow. (Unlike base 2, 10^x is never
161            //   exactly 2^MAX_EXPONENT, so there is no representable boundary value to
162            //   materialize.)
163            // - x * log2(10) < MAX_EXPONENT: 10^x < 2^MAX_EXPONENT strictly, so the overflow is an
164            //   artifact of rounding 10^x up at the working precision; growing it far enough (past
165            //   the distance from x * log2(10) to MAX_EXPONENT) makes 10^x finite, and the loop
166            //   proceeds normally.
167            if prec < Float::MAX_EXPONENT_U64
168                || x_log_2_10_ge(&Rational::exact_from(x), Float::MAX_EXPONENT_I64)
169            {
170                return exp_overflow(prec, rm);
171            }
172            working_prec += increment;
173            increment = working_prec >> 1;
174            continue;
175        }
176        // 10^x cannot underflow here: that would require x * log2(10) < MIN_EXPONENT, but then the
177        // deep-negative case above would already have returned. Integer x: 10^x is exact, so the
178        // result is simply round(10^x - 1).
179        if o1 == Equal {
180            return t.sub_prec_round(Float::ONE, prec, rm);
181        }
182        // 10^x is inexact, so x is not an integer and 10^x - 1 is transcendental: never exact.
183        assert_ne!(rm, Exact, "Inexact power_of_10_x_minus_1");
184        let exp_te = i64::from(t.get_exponent().unwrap());
185        t.sub_prec_assign(Float::ONE, working_prec); // 10^x - 1
186        if t != 0u32 {
187            let exp_t = i64::from(t.get_exponent().unwrap());
188            // The error estimate (cf. exp10m1.c): err = max(EXP(10^x) - EXP(10^x - 1), 0) + 1.
189            let err = u64::exact_from(max(exp_te - exp_t, 0) + 1);
190            if float_can_round(t.significand_ref().unwrap(), working_prec - err, prec, rm) {
191                return Float::from_float_prec_round(t, prec, rm);
192            }
193        }
194        // For small |x|, 10^x - 1 ~ x * ln(10); this may enable correct rounding when the
195        // cancellation in 10^x - 1 above does not. We must retry it at each Ziv step, since the
196        // multiplication x * ln(10) might not give correct rounding at the first loop.
197        match power_of_10_x_minus_1_small(x, prec, working_prec, rm) {
198            Small::Round(t) => return Float::from_float_prec_round(t, prec, rm),
199            Small::NotSmall => {}
200        }
201        // Increase the precision.
202        working_prec += increment;
203        increment = working_prec >> 1;
204    }
205}
206
207// Computes 10^x - 1 for a Float x with x * log2(10) < MIN_EXPONENT (so that 10^x is smaller than
208// the smallest positive Float) and |x| <= 2 + (prec - 1) / 3 (larger |x| is handled by the caller's
209// -1-rounding shortcut). The result is -1 + 10^x, and since |x| is bounded the bits of 10^x land
210// within the output's prec-bit window, even though 10^x itself is not representable. Split x = -s +
211// f with integer s >= 1 and f in [0, 1): 10^x = 10^f / 10^s, where 10^f is a normal Float in [1,
212// 10); the division by 10^s and the subtraction of 1 are exact over `Rational`s, whose size stays
213// O(prec) because 10^s has about 3.32 * s <= prec bits. For integer x the result is an exact
214// rational; otherwise 10^f is bracketed and the bracket is tightened Ziv-style. The initial working
215// precision is small: the leading ~3.32 * s bits of the result are a run of ones, so only about
216// prec - 3.32 * s bits of 10^f are needed.
217fn power_of_10_x_minus_1_deep_negative(
218    x: &Float,
219    prec: u64,
220    rm: RoundingMode,
221) -> (Float, Ordering) {
222    let xr = Rational::exact_from(x);
223    let neg_n = -Integer::rounding_from(&xr, Floor).0; // = |floor(x)| = s, since x < 0
224    let shift = u64::exact_from(&neg_n);
225    let ten_pow_s = TEN.pow(shift); // 10^s
226    let frac = xr + Rational::from(neg_n); // x - floor(x), in [0, 1)
227    if frac == 0u32 {
228        // x is an integer, so -1 + 10^x = -1 + 1 / 10^s is an exact rational.
229        return Float::from_rational_prec_round(ten_pow_s.reciprocal() - Rational::ONE, prec, rm);
230    }
231    // x is not an integer, so 10^x - 1 is irrational: never exact.
232    assert_ne!(rm, Exact, "Inexact power_of_10_x_minus_1");
233    // The fractional part of the Float x is a dyadic rational, exactly representable with as many
234    // bits as its numerator has.
235    let frac_prec = frac.numerator_ref().significant_bits();
236    let frac = Float::from_rational_prec_round(frac, frac_prec, Exact).0;
237    let mut working_prec = prec.saturating_sub(3 * shift) + Limb::WIDTH;
238    let mut increment = Limb::WIDTH;
239    loop {
240        // 10^frac is in [1, 10) and irrational, so the Floor rounding is strict: the true value
241        // lies strictly between u_lo and u_hi.
242        let u = Float::power_of_10_of_float_prec_round_ref(&frac, working_prec, Floor);
243        let (u_lo, u_hi) = floor_and_ceiling(u);
244        let (f_lo, mut o_lo) = Float::from_rational_prec_round(
245            Rational::exact_from(u_lo) / &ten_pow_s - Rational::ONE,
246            prec,
247            rm,
248        );
249        let (f_hi, mut o_hi) = Float::from_rational_prec_round(
250            Rational::exact_from(u_hi) / &ten_pow_s - Rational::ONE,
251            prec,
252            rm,
253        );
254        // A bound that is exactly representable at `prec` rounds with `Equal`, but the true value
255        // lies strictly between the bounds, so the other bound's ordering is the true one; treat
256        // the exact bound as agreeing with it.
257        if o_lo == Equal {
258            fail_on_untested_path(
259                "deep_negative o_lo == Equal: u_lo / 10^s has a 5^s factor in its denominator (the \
260                 working-precision mantissa is smaller than 5^s in the deep regime), so it is \
261                 non-dyadic and never rounds exactly",
262            );
263            o_lo = o_hi;
264        }
265        if o_hi == Equal {
266            fail_on_untested_path(
267                "deep_negative o_hi == Equal: as o_lo == Equal -- the bracket end is non-dyadic, \
268                 so it never rounds exactly",
269            );
270            o_hi = o_lo;
271        }
272        if o_lo == o_hi && f_lo == f_hi {
273            return (f_lo, o_lo);
274        }
275        working_prec += increment;
276        increment = working_prec >> 1;
277    }
278}
279
280// Computes 10^x - 1 for a nonzero `Rational` `x` with x * log2(10) < MIN_EXPONENT (so 10^x is too
281// small to bracket between `Float`s). Since 10^x - 1 = expm1(x ln(10)), bracketing ln(10) between
282// two `Rational`s (from a single directed ln(10) computation) and applying
283// `exp_x_minus_1_rational_near_zero` to each product brackets the result. expm1 is increasing, and
284// x ln(10) is increasing in ln(10) for x > 0 and decreasing for x < 0, so the bracket ends order
285// accordingly.
286fn power_of_10_x_minus_1_rational_near_zero(
287    x: &Rational,
288    prec: u64,
289    rm: RoundingMode,
290) -> (Float, Ordering) {
291    let positive = *x > 0u32;
292    let mut working_prec = prec + Limb::WIDTH;
293    let mut increment = Limb::WIDTH;
294    loop {
295        // ln_10_lo <= ln(10) <= ln_10_hi, as exact Rationals, from a single ln(10) computation.
296        let (ln_10_lo, ln_10_hi) = floor_and_ceiling(Float::ln_10_prec_round(working_prec, Floor));
297        let ln_10_lo = Rational::exact_from(ln_10_lo);
298        let ln_10_hi = Rational::exact_from(ln_10_hi);
299        let (a_lo, a_hi) = if positive {
300            (x * ln_10_lo, x * ln_10_hi)
301        } else {
302            (x * ln_10_hi, x * ln_10_lo)
303        };
304        let (f_lo, o_lo) = exp_x_minus_1_rational_near_zero(&a_lo, prec, rm);
305        let (f_hi, o_hi) = exp_x_minus_1_rational_near_zero(&a_hi, prec, rm);
306        if o_lo == o_hi && f_lo == f_hi {
307            return (f_lo, o_lo);
308        }
309        working_prec += increment;
310        increment = working_prec >> 1;
311    }
312}
313
314// Computes 10^x - 1 for a nonzero `Rational` `x`, rounded to precision `prec` with rounding mode
315// `rm`. (10^0 - 1 = 0 is handled by the caller.) Unlike expm1, the result is exactly representable
316// for some inputs: a nonnegative integer x makes 10^x - 1 an exact integer, and a negative integer
317// x makes it an exact (non-dyadic) rational, so those are computed directly (the Ziv squeeze below
318// could never certify one); every other rational x gives an irrational result.
319fn power_of_10_x_minus_1_rational_helper(
320    x: &Rational,
321    prec: u64,
322    rm: RoundingMode,
323) -> (Float, Ordering) {
324    if x.is_integer() {
325        let n = Integer::exact_from(x);
326        return if n > 0 {
327            // 10^n - 1 overflows when 10^n - 1 >= 2^MAX_EXPONENT, i.e. n * log2(10) >=
328            // MAX_EXPONENT.
329            if x_log_2_10_ge(x, Float::MAX_EXPONENT_I64) {
330                exp_overflow(prec, rm)
331            } else {
332                // 10^n - 1 is an exact integer with at most MAX_EXPONENT bits; round it directly.
333                let value = TEN.pow(u64::exact_from(&n)) - Rational::ONE;
334                Float::from_rational_prec_round(value, prec, rm)
335            }
336        } else {
337            // n < 0: 10^n - 1 = (1 - 10^|n|) / 10^|n| lies in (-1, 0) and is never exactly
338            // representable (its denominator 10^|n| has a factor of 5^|n|).
339            assert_ne!(rm, Exact, "Inexact power_of_10_x_minus_1");
340            // If 10^n >= 2^(-prec-2), then |n| log2(10) = O(prec), so materializing 10^|n| stays
341            // cheap. Otherwise 10^n < 2^(-prec-2) (i.e. n * log2(10) < -prec-2), so the result is
342            // within (1/4) ulp(-1) of -1: round directly from -1 with err = prec + 2, which
343            // satisfies `float_round_near_x`'s |result + 1| = 10^n < 2^(1 - err) and err > prec +
344            // 1.
345            if x_log_2_10_ge(x, -i64::exact_from(prec) - 2) {
346                let s = u64::exact_from(&-&n);
347                let value = TEN.pow(s).reciprocal() - Rational::ONE;
348                Float::from_rational_prec_round(value, prec, rm)
349            } else {
350                float_round_near_x(&Float::NEGATIVE_ONE, prec + 2, false, prec, rm).unwrap()
351            }
352        };
353    }
354    // The result for a non-integer rational is irrational, hence never exactly representable.
355    assert_ne!(rm, Exact, "Inexact power_of_10_x_minus_1");
356    let positive = *x > 0u32;
357    let exp_x = x.floor_log_base_2_abs() + 1; // the MPFR-style exponent of x
358    // x is too small to be represented as a normal Float (|x| < 2^MIN_EXPONENT). The squeeze below
359    // cannot bracket it (its Float bounds would be 0), so use the ln(10)-bracketing helper, which
360    // reduces 10^x - 1 to expm1(x ln(10)) for the tiny (near-zero) argument.
361    if exp_x <= Float::MIN_EXPONENT_I64 {
362        return power_of_10_x_minus_1_rational_near_zero(x, prec, rm);
363    }
364    // |x| is too large to be a finite Float. For x > 0, 10^x - 1 overflows; for x < 0 it tends to
365    // -1. Smaller x that still overflow or round to -1 are handled by the Float function inside the
366    // squeeze below.
367    if exp_x >= Float::MAX_EXPONENT_I64 {
368        if positive {
369            return exp_overflow(prec, rm);
370        }
371        // 10^x is far below ulp(-1) at any precision, so 10^x - 1 rounds to -1 or its toward-zero
372        // neighbor.
373        if let Some(result) = float_round_near_x(
374            &Float::NEGATIVE_ONE,
375            Float::MAX_EXPONENT_U64,
376            false,
377            prec,
378            rm,
379        ) {
380            return result;
381        }
382        // `prec` is enormous (>= MAX_EXPONENT), so `float_round_near_x` cannot resolve the
383        // rounding; but 10^x is still far below ulp(-1), so -1 rounds the same way.
384        return match rm {
385            Ceiling | Down => (-one_neighbor(prec, false), Greater), // -1 + ulp (toward zero)
386            _ => (-Float::one_prec(prec), Less),                     // -1
387        };
388    }
389    // General case: bracket x between the Floats x_lo <= x <= x_hi, apply 10^x - 1 to both, and
390    // increase the working precision until the two bounds round to the same result. 10^x - 1 is
391    // monotonic, so once the bounds agree the exact result (which lies between them) rounds the
392    // same way. A bound that lands on an integer produces an `Equal` ordering, which cannot match
393    // the other (irrational) bound's; the loop then simply tightens the bracket until neither bound
394    // is an integer.
395    let mut working_prec = prec + 10;
396    let mut increment = Limb::WIDTH;
397    loop {
398        let (x_lo, x_o) = Float::from_rational_prec_round_ref(x, working_prec, Floor);
399        if x_o == Equal {
400            // x is exactly representable at `working_prec`, so the result is simply that of the
401            // Float function.
402            return x_lo.power_of_10_x_minus_1_prec_round(prec, rm);
403        }
404        let (x_lo, x_hi) = floor_and_ceiling((x_lo, x_o));
405        let (e_lo, o_lo) = x_lo.power_of_10_x_minus_1_prec_round_ref(prec, rm);
406        let (e_hi, o_hi) = x_hi.power_of_10_x_minus_1_prec_round_ref(prec, rm);
407        if o_lo == o_hi && e_lo == e_hi {
408            return (e_lo, o_lo);
409        }
410        working_prec += increment;
411        increment = working_prec >> 1;
412    }
413}
414
415impl Float {
416    /// Computes $10^x-1$, rounding the result to the specified precision and with the specified
417    /// rounding mode. The [`Float`] is taken by value.
418    #[inline]
419    pub fn power_of_10_x_minus_1_prec_round(self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
420        self.power_of_10_x_minus_1_prec_round_ref(prec, rm)
421    }
422
423    /// Computes $10^x-1$, rounding the result to the specified precision and with the specified
424    /// rounding mode. The [`Float`] is taken by reference.
425    pub fn power_of_10_x_minus_1_prec_round_ref(
426        &self,
427        prec: u64,
428        rm: RoundingMode,
429    ) -> (Self, Ordering) {
430        assert_ne!(prec, 0);
431        match self {
432            Self(NaN) => (float_nan!(), Equal),
433            float_infinity!() => (float_infinity!(), Equal),
434            // 10^(-inf) - 1 = -1
435            Self(Infinity { sign: false }) => (Self::from_signed_prec(-1i32, prec).0, Equal),
436            // 10^(±0) - 1 = ±0
437            Self(Zero { .. }) => (self.clone(), Equal),
438            _ => power_of_10_x_minus_1_prec_round_normal(self, prec, rm),
439        }
440    }
441
442    /// Computes $10^x-1$, rounding the result to the nearest value of the specified precision. The
443    /// [`Float`] is taken by value.
444    #[inline]
445    pub fn power_of_10_x_minus_1_prec(self, prec: u64) -> (Self, Ordering) {
446        self.power_of_10_x_minus_1_prec_round(prec, Nearest)
447    }
448
449    /// Computes $10^x-1$, rounding the result to the nearest value of the specified precision. The
450    /// [`Float`] is taken by reference.
451    #[inline]
452    pub fn power_of_10_x_minus_1_prec_ref(&self, prec: u64) -> (Self, Ordering) {
453        self.power_of_10_x_minus_1_prec_round_ref(prec, Nearest)
454    }
455
456    /// Computes $10^x-1$, rounding the result with the specified rounding mode. The precision of
457    /// the output is the precision of the input. The [`Float`] is taken by value.
458    #[inline]
459    pub fn power_of_10_x_minus_1_round(self, rm: RoundingMode) -> (Self, Ordering) {
460        let prec = self.significant_bits();
461        self.power_of_10_x_minus_1_prec_round(prec, rm)
462    }
463
464    /// Computes $10^x-1$, rounding the result with the specified rounding mode. The precision of
465    /// the output is the precision of the input. The [`Float`] is taken by reference.
466    #[inline]
467    pub fn power_of_10_x_minus_1_round_ref(&self, rm: RoundingMode) -> (Self, Ordering) {
468        self.power_of_10_x_minus_1_prec_round_ref(self.significant_bits(), rm)
469    }
470
471    /// Computes $10^x-1$ in place, rounding the result to the specified precision and with the
472    /// specified rounding mode.
473    #[inline]
474    pub fn power_of_10_x_minus_1_prec_round_assign(
475        &mut self,
476        prec: u64,
477        rm: RoundingMode,
478    ) -> Ordering {
479        let (result, o) = core::mem::take(self).power_of_10_x_minus_1_prec_round(prec, rm);
480        *self = result;
481        o
482    }
483
484    /// Computes $10^x-1$ in place, rounding the result to the nearest value of the specified
485    /// precision.
486    #[inline]
487    pub fn power_of_10_x_minus_1_prec_assign(&mut self, prec: u64) -> Ordering {
488        self.power_of_10_x_minus_1_prec_round_assign(prec, Nearest)
489    }
490
491    /// Computes $10^x-1$ in place, rounding the result with the specified rounding mode. The
492    /// precision of the output is the precision of the input.
493    #[inline]
494    pub fn power_of_10_x_minus_1_round_assign(&mut self, rm: RoundingMode) -> Ordering {
495        let prec = self.significant_bits();
496        self.power_of_10_x_minus_1_prec_round_assign(prec, rm)
497    }
498
499    #[allow(clippy::needless_pass_by_value)]
500    /// Computes $10^x-1$, where $x$ is a [`Rational`], rounding the result to the specified
501    /// precision and with the specified rounding mode and returning the result as a [`Float`]. The
502    /// [`Rational`] is taken by value. An [`Ordering`] is also returned, indicating whether the
503    /// rounded value is less than, equal to, or greater than the exact value.
504    ///
505    /// See [`RoundingMode`] for a description of the possible rounding modes.
506    ///
507    /// $$
508    /// f(x,p,m) = 10^x-1+\varepsilon.
509    /// $$
510    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |10^x-1|\rfloor-p+1}$.
511    /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |10^x-1|\rfloor-p}$.
512    ///
513    /// These bounds do not apply when the result overflows or underflows; see below.
514    ///
515    /// The output has precision `prec`.
516    ///
517    /// Special cases:
518    /// - $f(0,p,m)=0$.
519    ///
520    /// Overflow and underflow:
521    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
522    ///   returned instead.
523    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
524    ///   returned instead.
525    /// - If $0<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
526    /// - If $0<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
527    ///   instead.
528    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
529    /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
530    ///   instead.
531    /// - If $-2^{-2^{30}}<f(x,p,m)<0$ and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
532    /// - If $-2^{-2^{30}}<f(x,p,m)<0$ and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
533    ///   instead.
534    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$ and $m$ is `Nearest`, $-0.0$ is returned instead.
535    /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$ and $m$ is `Nearest`, $-2^{-2^{30}}$ is returned
536    ///   instead.
537    ///
538    /// Unlike $10^x$, $10^x-1$ never underflows to zero for large negative $x$: it instead tends to
539    /// $-1$.
540    ///
541    /// If you know you'll be using `Nearest`, consider using
542    /// [`Float::power_of_10_x_minus_1_rational_prec`] instead.
543    ///
544    /// # Worst-case complexity
545    /// $T(n) = O(n^{3/2} \log n \log\log n)$
546    ///
547    /// $M(n) = O(n \log n)$
548    ///
549    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
550    ///
551    /// # Panics
552    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
553    /// with the given precision (which is the case unless $x$ is a nonnegative integer).
554    ///
555    /// # Examples
556    /// ```
557    /// use malachite_base::rounding_modes::RoundingMode::*;
558    /// use malachite_float::Float;
559    /// use malachite_q::Rational;
560    /// use std::cmp::Ordering::*;
561    ///
562    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round(
563    ///     Rational::from_unsigneds(3u8, 5),
564    ///     5,
565    ///     Floor,
566    /// );
567    /// assert_eq!(e.to_string(), "2.88");
568    /// assert_eq!(o, Less);
569    ///
570    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round(
571    ///     Rational::from_unsigneds(3u8, 5),
572    ///     5,
573    ///     Ceiling,
574    /// );
575    /// assert_eq!(e.to_string(), "3.00");
576    /// assert_eq!(o, Greater);
577    ///
578    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round(
579    ///     Rational::from_unsigneds(3u8, 5),
580    ///     20,
581    ///     Floor,
582    /// );
583    /// assert_eq!(e.to_string(), "2.9810715");
584    /// assert_eq!(o, Less);
585    ///
586    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round(
587    ///     Rational::from_unsigneds(3u8, 5),
588    ///     20,
589    ///     Ceiling,
590    /// );
591    /// assert_eq!(e.to_string(), "2.9810753");
592    /// assert_eq!(o, Greater);
593    /// ```
594    #[inline]
595    pub fn power_of_10_x_minus_1_rational_prec_round(
596        x: Rational,
597        prec: u64,
598        rm: RoundingMode,
599    ) -> (Self, Ordering) {
600        Self::power_of_10_x_minus_1_rational_prec_round_ref(&x, prec, rm)
601    }
602
603    /// Computes $10^x-1$, where $x$ is a [`Rational`], rounding the result to the specified
604    /// precision and with the specified rounding mode and returning the result as a [`Float`]. The
605    /// [`Rational`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
606    /// rounded value is less than, equal to, or greater than the exact value.
607    ///
608    /// See [`RoundingMode`] for a description of the possible rounding modes.
609    ///
610    /// $$
611    /// f(x,p,m) = 10^x-1+\varepsilon.
612    /// $$
613    /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 |10^x-1|\rfloor-p+1}$.
614    /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 |10^x-1|\rfloor-p}$.
615    ///
616    /// These bounds do not apply when the result overflows or underflows; see below.
617    ///
618    /// The output has precision `prec`.
619    ///
620    /// Special cases:
621    /// - $f(0,p,m)=0$.
622    ///
623    /// Overflow and underflow:
624    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
625    ///   returned instead.
626    /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
627    ///   returned instead.
628    /// - If $0<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
629    /// - If $0<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
630    ///   instead.
631    /// - If $0<f(x,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
632    /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
633    ///   instead.
634    /// - If $-2^{-2^{30}}<f(x,p,m)<0$ and $m$ is `Ceiling` or `Down`, $-0.0$ is returned instead.
635    /// - If $-2^{-2^{30}}<f(x,p,m)<0$ and $m$ is `Floor` or `Up`, $-2^{-2^{30}}$ is returned
636    ///   instead.
637    /// - If $-2^{-2^{30}-1}\leq f(x,p,m)<0$ and $m$ is `Nearest`, $-0.0$ is returned instead.
638    /// - If $-2^{-2^{30}}<f(x,p,m)<-2^{-2^{30}-1}$ and $m$ is `Nearest`, $-2^{-2^{30}}$ is returned
639    ///   instead.
640    ///
641    /// Unlike $10^x$, $10^x-1$ never underflows to zero for large negative $x$: it instead tends to
642    /// $-1$.
643    ///
644    /// If you know you'll be using `Nearest`, consider using
645    /// [`Float::power_of_10_x_minus_1_rational_prec_ref`] instead.
646    ///
647    /// # Worst-case complexity
648    /// $T(n) = O(n^{3/2} \log n \log\log n)$
649    ///
650    /// $M(n) = O(n \log n)$
651    ///
652    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
653    ///
654    /// # Panics
655    /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
656    /// with the given precision (which is the case unless $x$ is a nonnegative integer).
657    ///
658    /// # Examples
659    /// ```
660    /// use malachite_base::rounding_modes::RoundingMode::*;
661    /// use malachite_float::Float;
662    /// use malachite_q::Rational;
663    /// use std::cmp::Ordering::*;
664    ///
665    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round_ref(
666    ///     &Rational::from_unsigneds(3u8, 5),
667    ///     5,
668    ///     Floor,
669    /// );
670    /// assert_eq!(e.to_string(), "2.88");
671    /// assert_eq!(o, Less);
672    ///
673    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round_ref(
674    ///     &Rational::from_unsigneds(3u8, 5),
675    ///     5,
676    ///     Ceiling,
677    /// );
678    /// assert_eq!(e.to_string(), "3.00");
679    /// assert_eq!(o, Greater);
680    ///
681    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round_ref(
682    ///     &Rational::from_unsigneds(3u8, 5),
683    ///     20,
684    ///     Floor,
685    /// );
686    /// assert_eq!(e.to_string(), "2.9810715");
687    /// assert_eq!(o, Less);
688    ///
689    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_round_ref(
690    ///     &Rational::from_unsigneds(3u8, 5),
691    ///     20,
692    ///     Ceiling,
693    /// );
694    /// assert_eq!(e.to_string(), "2.9810753");
695    /// assert_eq!(o, Greater);
696    /// ```
697    #[inline]
698    pub fn power_of_10_x_minus_1_rational_prec_round_ref(
699        x: &Rational,
700        prec: u64,
701        rm: RoundingMode,
702    ) -> (Self, Ordering) {
703        assert_ne!(prec, 0);
704        if *x == 0u32 {
705            // 10^0 - 1 = 0, exactly.
706            return (float_zero!(), Equal);
707        }
708        power_of_10_x_minus_1_rational_helper(x, prec, rm)
709    }
710
711    /// Computes $10^x-1$, where $x$ is a [`Rational`], rounding the result to the nearest value of
712    /// the specified precision and returning the result as a [`Float`]. The [`Rational`] is taken
713    /// by value. An [`Ordering`] is also returned, indicating whether the rounded value is less
714    /// than, equal to, or greater than the exact value.
715    ///
716    /// If the value is equidistant from two [`Float`]s with the specified precision, the [`Float`]
717    /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
718    /// the `Nearest` rounding mode.
719    ///
720    /// $$
721    /// f(x,p) = 10^x-1+\varepsilon,
722    /// $$
723    /// where $|\varepsilon| \leq 2^{\lfloor\log_2 |10^x-1|\rfloor-p}$ (unless the result overflows
724    /// or underflows; see below).
725    ///
726    /// The output has precision `prec`.
727    ///
728    /// Special cases:
729    /// - $f(0,p)=0$.
730    ///
731    /// Overflow and underflow:
732    /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
733    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
734    /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
735    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
736    /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
737    ///
738    /// Unlike $10^x$, $10^x-1$ never underflows to zero for large negative $x$: it instead tends to
739    /// $-1$.
740    ///
741    /// If you want to use a rounding mode other than `Nearest`, consider using
742    /// [`Float::power_of_10_x_minus_1_rational_prec_round`] instead.
743    ///
744    /// # Worst-case complexity
745    /// $T(n) = O(n^{3/2} \log n \log\log n)$
746    ///
747    /// $M(n) = O(n \log n)$
748    ///
749    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
750    ///
751    /// # Panics
752    /// Panics if `prec` is zero.
753    ///
754    /// # Examples
755    /// ```
756    /// use malachite_float::Float;
757    /// use malachite_q::Rational;
758    /// use std::cmp::Ordering::*;
759    ///
760    /// let (e, o) =
761    ///     Float::power_of_10_x_minus_1_rational_prec(Rational::from_unsigneds(3u8, 5), 5);
762    /// assert_eq!(e.to_string(), "3.00");
763    /// assert_eq!(o, Greater);
764    ///
765    /// let (e, o) =
766    ///     Float::power_of_10_x_minus_1_rational_prec(Rational::from_unsigneds(3u8, 5), 20);
767    /// assert_eq!(e.to_string(), "2.9810715");
768    /// assert_eq!(o, Less);
769    ///
770    /// let (e, o) =
771    ///     Float::power_of_10_x_minus_1_rational_prec(Rational::from_signeds(-3i8, 5), 10);
772    /// assert_eq!(e.to_string(), "-0.74902");
773    /// assert_eq!(o, Less);
774    ///
775    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec(Rational::from(0), 10);
776    /// assert_eq!(e.to_string(), "0.0");
777    /// assert_eq!(o, Equal);
778    /// ```
779    #[inline]
780    pub fn power_of_10_x_minus_1_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
781        Self::power_of_10_x_minus_1_rational_prec_round(x, prec, Nearest)
782    }
783
784    /// Computes $10^x-1$, where $x$ is a [`Rational`], rounding the result to the nearest value of
785    /// the specified precision and returning the result as a [`Float`]. The [`Rational`] is taken
786    /// by reference. An [`Ordering`] is also returned, indicating whether the rounded value is less
787    /// than, equal to, or greater than the exact value.
788    ///
789    /// If the value is equidistant from two [`Float`]s with the specified precision, the [`Float`]
790    /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
791    /// the `Nearest` rounding mode.
792    ///
793    /// $$
794    /// f(x,p) = 10^x-1+\varepsilon,
795    /// $$
796    /// where $|\varepsilon| \leq 2^{\lfloor\log_2 |10^x-1|\rfloor-p}$ (unless the result overflows
797    /// or underflows; see below).
798    ///
799    /// The output has precision `prec`.
800    ///
801    /// Special cases:
802    /// - $f(0,p)=0$.
803    ///
804    /// Overflow and underflow:
805    /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
806    /// - If $0<f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
807    /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
808    /// - If $-2^{-2^{30}-1}\leq f(x,p)<0$, $-0.0$ is returned instead.
809    /// - If $-2^{-2^{30}}<f(x,p)<-2^{-2^{30}-1}$, $-2^{-2^{30}}$ is returned instead.
810    ///
811    /// Unlike $10^x$, $10^x-1$ never underflows to zero for large negative $x$: it instead tends to
812    /// $-1$.
813    ///
814    /// If you want to use a rounding mode other than `Nearest`, consider using
815    /// [`Float::power_of_10_x_minus_1_rational_prec_round_ref`] instead.
816    ///
817    /// # Worst-case complexity
818    /// $T(n) = O(n^{3/2} \log n \log\log n)$
819    ///
820    /// $M(n) = O(n \log n)$
821    ///
822    /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
823    ///
824    /// # Panics
825    /// Panics if `prec` is zero.
826    ///
827    /// # Examples
828    /// ```
829    /// use malachite_float::Float;
830    /// use malachite_q::Rational;
831    /// use std::cmp::Ordering::*;
832    ///
833    /// let (e, o) =
834    ///     Float::power_of_10_x_minus_1_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 5);
835    /// assert_eq!(e.to_string(), "3.00");
836    /// assert_eq!(o, Greater);
837    ///
838    /// let (e, o) =
839    ///     Float::power_of_10_x_minus_1_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 20);
840    /// assert_eq!(e.to_string(), "2.9810715");
841    /// assert_eq!(o, Less);
842    ///
843    /// let (e, o) =
844    ///     Float::power_of_10_x_minus_1_rational_prec_ref(&Rational::from_signeds(-3i8, 5), 10);
845    /// assert_eq!(e.to_string(), "-0.74902");
846    /// assert_eq!(o, Less);
847    ///
848    /// let (e, o) = Float::power_of_10_x_minus_1_rational_prec_ref(&Rational::from(0), 10);
849    /// assert_eq!(e.to_string(), "0.0");
850    /// assert_eq!(o, Equal);
851    /// ```
852    #[inline]
853    pub fn power_of_10_x_minus_1_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
854        Self::power_of_10_x_minus_1_rational_prec_round_ref(x, prec, Nearest)
855    }
856}
857
858impl PowerOf10XMinus1 for Float {
859    type Output = Self;
860
861    /// Computes $10^x-1$, where $x$ is a [`Float`], taking the [`Float`] by value.
862    #[inline]
863    fn power_of_10_x_minus_1(self) -> Self {
864        let prec = self.significant_bits();
865        self.power_of_10_x_minus_1_prec(prec).0
866    }
867}
868
869impl PowerOf10XMinus1 for &Float {
870    type Output = Float;
871
872    /// Computes $10^x-1$, where $x$ is a [`Float`], taking the [`Float`] by reference.
873    #[inline]
874    fn power_of_10_x_minus_1(self) -> Float {
875        self.power_of_10_x_minus_1_prec_round_ref(self.significant_bits(), Nearest)
876            .0
877    }
878}
879
880impl PowerOf10XMinus1Assign for Float {
881    /// Computes $10^x-1$, where $x$ is a [`Float`], in place.
882    #[inline]
883    fn power_of_10_x_minus_1_assign(&mut self) {
884        let prec = self.significant_bits();
885        self.power_of_10_x_minus_1_prec_round_assign(prec, Nearest);
886    }
887}
888
889/// Computes $10^x-1$ for a primitive float. The result is correctly rounded. Using this function is
890/// more accurate than computing `10.0.powf(x) - 1.0` with the primitive float functions: that
891/// subtraction loses all precision when $x$ is small (where $10^x-1\approx x\ln 10$ but $10^x$
892/// rounds to 1), and the standard library's `powf` is not correctly rounded to begin with.
893///
894/// $$
895/// f(x) = 10^x-1+\varepsilon.
896/// $$
897/// - If $10^x-1$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
898/// - If $10^x-1$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |10^x-1|\rfloor-p}$,
899///   where $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
900///   [`f64`], but less if the output is subnormal).
901///
902/// Special cases:
903/// - $f(\text{NaN})=\text{NaN}$
904/// - $f(\infty)=\infty$
905/// - $f(-\infty)=-1$
906/// - $f(\pm0.0)=\pm0.0$
907///
908/// # Worst-case complexity
909/// Constant time and additional memory.
910///
911/// # Examples
912/// ```
913/// use malachite_base::num::basic::traits::NegativeInfinity;
914/// use malachite_base::num::float::NiceFloat;
915/// use malachite_float::float::arithmetic::power_of_10_x_minus_1::*;
916///
917/// assert!(primitive_float_power_of_10_x_minus_1(f32::NAN).is_nan());
918/// assert_eq!(
919///     NiceFloat(primitive_float_power_of_10_x_minus_1(f32::INFINITY)),
920///     NiceFloat(f32::INFINITY)
921/// );
922/// assert_eq!(
923///     NiceFloat(primitive_float_power_of_10_x_minus_1(
924///         f32::NEGATIVE_INFINITY
925///     )),
926///     NiceFloat(-1.0)
927/// );
928/// assert_eq!(
929///     NiceFloat(primitive_float_power_of_10_x_minus_1(3.0f32)),
930///     NiceFloat(999.0)
931/// );
932/// ```
933#[inline]
934#[allow(clippy::type_repetition_in_bounds)]
935pub fn primitive_float_power_of_10_x_minus_1<T: PrimitiveFloat>(x: T) -> T
936where
937    Float: From<T> + PartialOrd<T>,
938    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
939{
940    emulate_float_to_float_fn(Float::power_of_10_x_minus_1_prec, x)
941}
942
943/// Computes $10^x-1$, where $x$ is a [`Rational`], returning the result as a primitive float. The
944/// result is correctly rounded.
945///
946/// $$
947/// f(x) = 10^x-1+\varepsilon.
948/// $$
949/// - If $10^x-1$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
950/// - If $10^x-1$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |10^x-1|\rfloor-p}$,
951///   where $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
952///   [`f64`], but less if the output is subnormal).
953///
954/// Special cases:
955/// - $f(0)=0$
956///
957/// # Worst-case complexity
958/// Constant time and additional memory.
959///
960/// # Examples
961/// ```
962/// use malachite_base::num::float::NiceFloat;
963/// use malachite_float::float::arithmetic::power_of_10_x_minus_1::*;
964/// use malachite_q::Rational;
965///
966/// assert_eq!(
967///     NiceFloat(primitive_float_power_of_10_x_minus_1_rational::<f32>(
968///         &Rational::from(3u32)
969///     )),
970///     NiceFloat(999.0)
971/// );
972/// assert_eq!(
973///     NiceFloat(primitive_float_power_of_10_x_minus_1_rational::<f32>(
974///         &Rational::from_signeds(-1i32, 2)
975///     )),
976///     NiceFloat(-0.6837722)
977/// );
978/// ```
979#[inline]
980#[allow(clippy::type_repetition_in_bounds)]
981pub fn primitive_float_power_of_10_x_minus_1_rational<T: PrimitiveFloat>(x: &Rational) -> T
982where
983    Float: PartialOrd<T>,
984    for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
985{
986    emulate_rational_to_float_fn(Float::power_of_10_x_minus_1_rational_prec_ref, x)
987}