Skip to main content

primitive_float_ln

Function primitive_float_ln 

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

Computes the natural logarithm of a primitive float. Using this function is more accurate than using the default log function or the one provided by libm.

The reciprocal logarithm of any nonzero 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(\text{NaN})=\text{NaN}$
  • $f(\infty)=\infty$
  • $f(-\infty)=\text{NaN}$
  • $f(\pm0.0)=-\infty$

Neither overflow nor underflow is possible.

§Worst-case complexity

Constant time and additional memory.

§Examples

use malachite_base::num::basic::traits::NegativeInfinity;
use malachite_base::num::float::NiceFloat;
use malachite_float::arithmetic::ln::primitive_float_ln;

assert!(primitive_float_ln(f32::NAN).is_nan());
assert_eq!(
    NiceFloat(primitive_float_ln(f32::INFINITY)),
    NiceFloat(f32::INFINITY)
);
assert!(primitive_float_ln(f32::NEGATIVE_INFINITY).is_nan());
assert_eq!(NiceFloat(primitive_float_ln(10.0f32)), NiceFloat(2.3025851));
assert!(primitive_float_ln(-10.0f32).is_nan());