Skip to content

Commit 7ec9a59

Browse files
dhardysicking
authored andcommitted
Float sampling: improve high precision sampling; add mean test
(The mean test is totally inadequate for checking high precision.)
1 parent 8fe80f6 commit 7ec9a59

File tree

1 file changed

+76
-49
lines changed

1 file changed

+76
-49
lines changed

src/distributions/float.rs

+76-49
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Basic floating-point number distributions
1212
13-
use core::mem;
13+
use core::{cmp, mem};
1414
use Rng;
1515
use distributions::{Distribution, Standard};
1616
use distributions::utils::CastFromInt;
@@ -98,22 +98,20 @@ impl<F: HPFloatHelper> HighPrecision<F> {
9898
}
9999

100100
/// Generate a floating point number in the half-open interval `[0, 1)` with a
101-
/// uniform distribution.
101+
/// uniform distribution, with as much precision as the floating-point type
102+
/// can represent, including sub-normals.
102103
///
103-
/// This is different from `Uniform` in that it uses all 32 bits of an RNG for a
104-
/// `f32`, instead of only 23, the number of bits that fit in a floats fraction
105-
/// (or 64 instead of 52 bits for a `f64`).
104+
/// Technically 0 is representable, but the probability of occurrence is
105+
/// remote (1 in 2^149 for `f32` or 1 in 2^1074 for `f64`).
106106
///
107-
/// The smallest interval between values that can be generated is 2^-32
108-
/// (2.3283064e-10) for `f32`, and 2^-64 (5.421010862427522e-20) for `f64`.
109-
/// But this interval increases further away from zero because of limitations of
110-
/// the floating point format. Close to 1.0 the interval is 2^-24 (5.9604645e-8)
111-
/// for `f32`, and 2^-53 (1.1102230246251565) for `f64`. Compare this with
112-
/// `Uniform`, which has a fixed interval of 2^23 and 2^-52 respectively.
113-
///
114-
/// Note: in the future this may change change to request even more bits from
115-
/// the RNG if the value gets very close to 0.0, so it always has as many digits
116-
/// of precision as the float can represent.
107+
/// This is different from `Uniform` in that it uses as many random bits as
108+
/// required to get high precision close to 0. Normally only a single call to
109+
/// the source RNG is required (32 bits for `f32` or 64 bits for `f64`); 1 in
110+
/// 2^9 (`f32`) or 2^12 (`f64`) samples need an extra call; of these 1 in 2^32
111+
/// or 1 in 2^64 require a third call, etc.; i.e. even for `f32` a third call is
112+
/// almost impossible to observe with an unbiased RNG. Due to the extra logic
113+
/// there is some performance overhead relative to `Uniform`; this is more
114+
/// significant for `f32` than for `f64`.
117115
///
118116
/// # Example
119117
/// ```rust
@@ -229,24 +227,10 @@ float_impls! { f64x8, u64x8, f64, u64, 52, 1023 }
229227

230228

231229
macro_rules! high_precision_float_impls {
232-
($ty:ty, $uty:ty, $ity:ty, $fraction_bits:expr, $exponent_bits:expr) => {
230+
($ty:ty, $uty:ty, $ity:ty, $fraction_bits:expr, $exponent_bits:expr, $exponent_bias:expr) => {
233231
impl Distribution<$ty> for HighPrecision01 {
234232
/// Generate a floating point number in the half-open interval
235-
/// `[0, 1)` with a uniform distribution.
236-
///
237-
/// This is different from `Uniform` in that it uses all 32 bits
238-
/// of an RNG for a `f32`, instead of only 23, the number of bits
239-
/// that fit in a floats fraction (or 64 instead of 52 bits for a
240-
/// `f64`).
241-
///
242-
/// # Example
243-
/// ```rust
244-
/// use rand::{NewRng, SmallRng, Rng};
245-
/// use rand::distributions::HighPrecision01;
246-
///
247-
/// let val: f32 = SmallRng::new().sample(HighPrecision01);
248-
/// println!("f32 from [0,1): {}", val);
249-
/// ```
233+
/// `[0, 1)` with a uniform distribution. See [`HighPrecision01`].
250234
///
251235
/// # Algorithm
252236
/// (Note: this description used values that apply to `f32` to
@@ -255,34 +239,50 @@ macro_rules! high_precision_float_impls {
255239
/// The trick to generate a uniform distribution over [0,1) is to
256240
/// set the exponent to the -log2 of the remaining random bits. A
257241
/// simpler alternative to -log2 is to count the number of trailing
258-
/// zero's of the random bits.
242+
/// zeros in the random bits. In the case where all bits are zero,
243+
/// we simply generate a new random number and add the number of
244+
/// trailing zeros to the previous count (up to maximum exponent).
259245
///
260246
/// Each exponent is responsible for a piece of the distribution
261-
/// between [0,1). The exponent -1 fills the part [0.5,1). -2 fills
262-
/// [0.25,0.5). The lowest exponent we can get is -10. So a problem
263-
/// with this method is that we can not fill the part between zero
264-
/// and the part from -10. The solution is to treat numbers with an
265-
/// exponent of -10 as if they have -9 as exponent, and substract
266-
/// 2^-9 (implemented in the `fallback` function).
247+
/// between [0,1). We take the above exponent, add 1 and negate;
248+
/// thus with probability 1/2 we have exponent -1 which fills the
249+
/// range [0.5,1); with probability 1/4 we have exponent -2 which
250+
/// fills the range [0.25,0.5), etc. If the exponent reaches the
251+
/// minimum allowed, the floating-point format drops the implied
252+
/// fraction bit, thus allowing numbers down to 0 to be sampled.
253+
///
254+
/// [`HighPrecision01`]: struct.HighPrecision01.html
267255
#[inline]
268256
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {
257+
// Unusual case. Separate function to allow inlining of rest.
269258
#[inline(never)]
270-
fn fallback(fraction: $uty) -> $ty {
271-
let float_size = (mem::size_of::<$ty>() * 8) as i32;
272-
let min_exponent = $fraction_bits as i32 - float_size;
273-
let adjust = // 2^MIN_EXPONENT
274-
(0 as $uty).into_float_with_exponent(min_exponent);
275-
fraction.into_float_with_exponent(min_exponent) - adjust
259+
fn fallback<R: Rng + ?Sized>(mut exp: i32, fraction: $uty, rng: &mut R) -> $ty {
260+
// Performance impact of code here is negligible.
261+
let bits = rng.gen::<$uty>();
262+
exp += bits.trailing_zeros() as i32;
263+
// If RNG were guaranteed unbiased we could skip the
264+
// check against exp; unfortunately it may be.
265+
// Worst case ("zeros" RNG) has recursion depth 16.
266+
if bits == 0 && exp < $exponent_bias {
267+
return fallback(exp, fraction, rng);
268+
}
269+
exp = cmp::min(exp, $exponent_bias);
270+
fraction.into_float_with_exponent(-exp)
276271
}
277272

278273
let fraction_mask = (1 << $fraction_bits) - 1;
279274
let value: $uty = rng.gen();
280275

281276
let fraction = value & fraction_mask;
282277
let remaining = value >> $fraction_bits;
283-
// If `remaing ==0` we end up in the lowest exponent, which
284-
// needs special treatment.
285-
if remaining == 0 { return fallback(fraction) }
278+
if remaining == 0 {
279+
// exp is compile-time constant so this reduces to a function call:
280+
let size_bits = (mem::size_of::<$ty>() * 8) as i32;
281+
let exp = (size_bits - $fraction_bits as i32) + 1;
282+
return fallback(exp, fraction, rng);
283+
}
284+
285+
// Usual case: exponent from -1 to -9 (f32) or -12 (f64)
286286
let exp = remaining.trailing_zeros() as i32 + 1;
287287
fraction.into_float_with_exponent(-exp)
288288
}
@@ -444,8 +444,8 @@ macro_rules! high_precision_float_impls {
444444
}
445445
}
446446

447-
high_precision_float_impls! { f32, u32, i32, 23, 8 }
448-
high_precision_float_impls! { f64, u64, i64, 52, 11 }
447+
high_precision_float_impls! { f32, u32, i32, 23, 8, 127 }
448+
high_precision_float_impls! { f64, u64, i64, 52, 11, 1023 }
449449

450450

451451
#[cfg(test)]
@@ -729,4 +729,31 @@ mod tests {
729729
assert_eq!(ones.sample::<f32, _>(HighPrecision01), 0.99999994);
730730
assert_eq!(ones.sample::<f64, _>(HighPrecision01), 0.9999999999999999);
731731
}
732+
733+
#[cfg(feature="std")] mod mean {
734+
use Rng;
735+
use distributions::{Standard, HighPrecision01};
736+
737+
macro_rules! test_mean {
738+
($name:ident, $ty:ty, $distr:expr) => {
739+
#[test]
740+
fn $name() {
741+
// TODO: no need to &mut here:
742+
let mut r = ::test::rng(602);
743+
let mut total: $ty = 0.0;
744+
const N: u32 = 1_000_000;
745+
for _ in 0..N {
746+
total += r.sample::<$ty, _>($distr);
747+
}
748+
let avg = total / (N as $ty);
749+
//println!("average over {} samples: {}", N, avg);
750+
assert!(0.499 < avg && avg < 0.501);
751+
}
752+
} }
753+
754+
test_mean!(test_mean_f32, f32, Standard);
755+
test_mean!(test_mean_f64, f64, Standard);
756+
test_mean!(test_mean_high_f32, f32, HighPrecision01);
757+
test_mean!(test_mean_high_f64, f64, HighPrecision01);
758+
}
732759
}

0 commit comments

Comments
 (0)