Skip to content

Commit 515cf18

Browse files
authored
Merge pull request #477 from sicking/limit_inexact_float
Make Uniform<f32/f64> and gen_range<f32/f64> honor limits strictly. Resolves #476
2 parents c032204 + b6d7d4c commit 515cf18

File tree

4 files changed

+427
-123
lines changed

4 files changed

+427
-123
lines changed

benches/distributions.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,33 @@ gen_range_int!(gen_range_i64, i64, 3i64, 123_456_789_123);
150150
#[cfg(feature = "i128_support")]
151151
gen_range_int!(gen_range_i128, i128, -12345678901234i128, 123_456_789_123_456_789);
152152

153+
// construct and sample from a floating-point range
154+
macro_rules! gen_range_float {
155+
($fnn:ident, $ty:ident, $low:expr, $high:expr) => {
156+
#[bench]
157+
fn $fnn(b: &mut Bencher) {
158+
let mut rng = XorShiftRng::from_entropy();
159+
160+
b.iter(|| {
161+
let mut high = $high;
162+
let mut low = $low;
163+
let mut accum: $ty = 0.0;
164+
for _ in 0..::RAND_BENCH_N {
165+
accum += rng.gen_range(low, high);
166+
// force recalculation of range each time
167+
low += 0.9;
168+
high += 1.1;
169+
}
170+
accum
171+
});
172+
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
173+
}
174+
}
175+
}
176+
177+
gen_range_float!(gen_range_f32, f32, -20000.0f32, 100000.0);
178+
gen_range_float!(gen_range_f64, f64, 123.456f64, 7890.12);
179+
153180
#[bench]
154181
fn dist_iter(b: &mut Bencher) {
155182
let mut rng = XorShiftRng::from_entropy();

src/distributions/float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use core::mem;
1414
use Rng;
1515
use distributions::{Distribution, Standard};
16-
use distributions::utils::CastFromInt;
16+
use distributions::utils::FloatSIMDUtils;
1717
#[cfg(feature="simd_support")]
1818
use core::simd::*;
1919

0 commit comments

Comments
 (0)