Skip to main content

primitive_float_log_base_10

Function primitive_float_log_base_10 

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

Computes $\log_{10} x$, the base-10 logarithm of a primitive float. Using this function is more accurate than using the primitive float log10 function (the standard library’s log10 is not always correctly rounded).

The base-10 logarithm of any negative number is NaN.

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

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

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::log_base_10::primitive_float_log_base_10;

assert!(primitive_float_log_base_10(f32::NAN).is_nan());
assert_eq!(
    NiceFloat(primitive_float_log_base_10(f32::INFINITY)),
    NiceFloat(f32::INFINITY)
);
assert_eq!(
    NiceFloat(primitive_float_log_base_10(0.0f32)),
    NiceFloat(f32::NEGATIVE_INFINITY)
);
// log_10(1000) = 3
assert_eq!(
    NiceFloat(primitive_float_log_base_10(1000.0f32)),
    NiceFloat(3.0)
);
// log_10(50)
assert_eq!(
    NiceFloat(primitive_float_log_base_10(50.0f32)),
    NiceFloat(1.69897)
);
assert!(primitive_float_log_base_10(-1.0f32).is_nan());