Skip to main content

primitive_float_ln_rational

Function primitive_float_ln_rational 

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

Computes the natural 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) = \ln{x}+\varepsilon. $$

  • If $\ln{x}$ is infinite, zero, or NaN, $\varepsilon$ may be ignored or assumed to be 0.
  • If $\ln{x}$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |\ln{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::ln::primitive_float_ln_rational;
use malachite_q::Rational;

assert_eq!(
    NiceFloat(primitive_float_ln_rational::<f64>(&Rational::ZERO)),
    NiceFloat(f64::NEGATIVE_INFINITY)
);
assert_eq!(
    NiceFloat(primitive_float_ln_rational::<f64>(
        &Rational::from_unsigneds(1u8, 3)
    )),
    NiceFloat(-1.0986122886681098)
);
assert_eq!(
    NiceFloat(primitive_float_ln_rational::<f64>(&Rational::from(10000))),
    NiceFloat(9.210340371976184)
);
assert_eq!(
    NiceFloat(primitive_float_ln_rational::<f64>(&Rational::from(-10000))),
    NiceFloat(f64::NAN)
);