pub fn primitive_float_log_base<T>(x: T, base: u64) -> Twhere
Float: From<T> + PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float> + PrimitiveFloat,Expand description
Computes $\log_b x$, the base-$b$ logarithm of a primitive float, where $b$ is a u64 greater
than 1. Using this function is more accurate than computing the logarithm using the standard
library, whose log is not always correctly rounded.
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
Tis af32and 53 ifTis af64, but less if the output is subnormal).
Special cases:
- $f(\text{NaN},b)=\text{NaN}$
- $f(\infty,b)=\infty$
- $f(-\infty,b)=\text{NaN}$
- $f(\pm0.0,b)=-\infty$
- $f(1.0,b)=0.0$
- $f(x,b)=\text{NaN}$ for $x<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;
use malachite_base::num::float::NiceFloat;
use malachite_float::arithmetic::log_base::primitive_float_log_base;
assert!(primitive_float_log_base(f32::NAN, 10).is_nan());
assert_eq!(
NiceFloat(primitive_float_log_base(f32::INFINITY, 10)),
NiceFloat(f32::INFINITY)
);
assert_eq!(
NiceFloat(primitive_float_log_base(0.0f32, 10)),
NiceFloat(f32::NEGATIVE_INFINITY)
);
// log_10(1000) = 3
assert_eq!(
NiceFloat(primitive_float_log_base(1000.0f32, 10)),
NiceFloat(3.0)
);
// log_3(9) = 2
assert_eq!(
NiceFloat(primitive_float_log_base(9.0f32, 3)),
NiceFloat(2.0)
);
// log_10(50)
assert_eq!(
NiceFloat(primitive_float_log_base(50.0f32, 10)),
NiceFloat(1.69897)
);
assert!(primitive_float_log_base(-1.0f32, 10).is_nan());