pub trait EstimatedLog2 {
// Required method
fn log2_bounds(&self) -> (f32, f32);
// Provided method
fn log2_est(&self) -> f32 { ... }
}
Expand description
Fast estimation of the binary logarithm of a number
§Panics
Panics if the number is 0
§Examples
use dashu_base::EstimatedLog2;
let lb3 = 1.584962500721156f32;
let (lb3_lb, lb3_ub) = 3u8.log2_bounds();
assert!(lb3_lb <= lb3 && lb3 <= lb3_ub);
assert!((lb3 - lb3_lb) / lb3 < 1. / 256.);
assert!((lb3_ub - lb3) / lb3 <= 1. / 256.);
let lb3_est = 3u8.log2_est();
assert!((lb3 - lb3_est).abs() < 1e-3);
Required Methods§
Sourcefn log2_bounds(&self) -> (f32, f32)
fn log2_bounds(&self) -> (f32, f32)
Estimate the bounds of the binary logarithm.
The result is (lower bound, upper bound)
such that lower bound ≤ log2(self) ≤ upper bound
.
The precision of the bounds must be at least 8 bits (relative error < 2^-8).
With std
disabled, the precision is about 13 bits. With std
enabled, the precision
can be full 24 bits. But the exact precision is not guaranteed and should not be not
relied on.
For negative values, the logarithm is calculated based on its absolute value. If the number is zero, then negative infinity will be returned.
Provided Methods§
Sourcefn log2_est(&self) -> f32
fn log2_est(&self) -> f32
Estimate the value of the binary logarithm. It’s calculated as the average of log2_bounds by default.