Skip to main content

primitive_float_log_base_2_rational

Function primitive_float_log_base_2_rational 

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

Computes the base-2 logarithm of a Rational, 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 logarithm of any negative number is NaN.

$$ f(x) = \log_2{x}+\varepsilon. $$

  • If $\log_2{x}$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed to be 0.
  • If $\log_2{x}$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |\log_2{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)=-\infty$

Neither overflow nor underflow is possible.

§Worst-case complexity

Constant time and additional memory.

§Examples

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

assert_eq!(
    NiceFloat(primitive_float_log_base_2_rational::<f64>(&Rational::ZERO)),
    NiceFloat(f64::NEGATIVE_INFINITY)
);
assert_eq!(
    NiceFloat(primitive_float_log_base_2_rational::<f64>(
        &Rational::from_unsigneds(1u8, 3)
    )),
    NiceFloat(-1.584962500721156)
);
assert_eq!(
    NiceFloat(primitive_float_log_base_2_rational::<f64>(&Rational::from(
        10000
    ))),
    NiceFloat(13.287712379549449)
);
assert_eq!(
    NiceFloat(primitive_float_log_base_2_rational::<f64>(&Rational::from(
        -10000
    ))),
    NiceFloat(f64::NAN)
);