Skip to main content

primitive_float_log_base_rational

Function primitive_float_log_base_rational 

Source
pub fn primitive_float_log_base_rational<T>(x: &Rational, base: u64) -> T
where Float: PartialOrd<T>, for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float> + PrimitiveFloat,
Expand description

Computes $\log_b x$, the base-$b$ logarithm of a Rational, where $b$ is a u64 greater than 1, returning a primitive float result.

If the logarithm is equidistant from two primitive floats, the primitive float with fewer 1s in its binary expansion is chosen. See RoundingMode for a description of the Nearest rounding mode.

The base-$b$ logarithm of any negative number is NaN.

$$ f(x,b) = \log_b x+\varepsilon. $$

  • If $\log_b x$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed to be 0.
  • If $\log_b x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |\log_b x|\rfloor-p}$, where $p$ is precision of the output (typically 24 if T is a f32 and 53 if T is a f64, but less if the output is subnormal).

Special cases:

  • $f(0,b)=-\infty$
  • $f(x,b)=\text{NaN}$ for $x<0$
  • $f(1,b)=0.0$

Neither overflow nor underflow is possible.

§Worst-case complexity

Constant time and additional memory.

§Panics

Panics if base is less than 2.

§Examples

use malachite_base::num::basic::traits::{NegativeInfinity, Zero};
use malachite_base::num::float::NiceFloat;
use malachite_float::arithmetic::log_base::primitive_float_log_base_rational;
use malachite_q::Rational;

assert_eq!(
    NiceFloat(primitive_float_log_base_rational::<f64>(
        &Rational::ZERO,
        10
    )),
    NiceFloat(f64::NEGATIVE_INFINITY)
);
// log_10(1000) = 3
assert_eq!(
    NiceFloat(primitive_float_log_base_rational::<f64>(
        &Rational::from(1000),
        10
    )),
    NiceFloat(3.0)
);
// log_3(1/9) = -2
assert_eq!(
    NiceFloat(primitive_float_log_base_rational::<f64>(
        &Rational::from_unsigneds(1u8, 9),
        3
    )),
    NiceFloat(-2.0)
);
// log_10(1/3)
assert_eq!(
    NiceFloat(primitive_float_log_base_rational::<f64>(
        &Rational::from_unsigneds(1u8, 3),
        10
    )),
    NiceFloat(-0.47712125471966244)
);
assert_eq!(
    NiceFloat(primitive_float_log_base_rational::<f64>(
        &Rational::from(-1000),
        10
    )),
    NiceFloat(f64::NAN)
);