Skip to content

Commit 09dd014

Browse files
committed
impl Distribution<f32> for StandardNormal
1 parent d998742 commit 09dd014

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

rand_distr/src/gamma.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl Distribution<f64> for GammaSmallShape {
162162
impl Distribution<f64> for GammaLargeShape {
163163
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
164164
loop {
165-
let x = rng.sample(StandardNormal);
165+
let x: f64 = rng.sample(StandardNormal);
166166
let v_cbrt = 1.0 + self.c * x;
167167
if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0
168168
continue
@@ -238,7 +238,7 @@ impl Distribution<f64> for ChiSquared {
238238
match self.repr {
239239
DoFExactlyOne => {
240240
// k == 1 => N(0,1)^2
241-
let norm = rng.sample(StandardNormal);
241+
let norm: f64 = rng.sample(StandardNormal);
242242
norm * norm
243243
}
244244
DoFAnythingElse(ref g) => g.sample(rng)
@@ -332,7 +332,7 @@ impl StudentT {
332332
}
333333
impl Distribution<f64> for StudentT {
334334
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
335-
let norm = rng.sample(StandardNormal);
335+
let norm: f64 = rng.sample(StandardNormal);
336336
norm * (self.dof / self.chi.sample(rng)).sqrt()
337337
}
338338
}

rand_distr/src/normal.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ use crate::utils::ziggurat;
3737
#[derive(Clone, Copy, Debug)]
3838
pub struct StandardNormal;
3939

40+
impl Distribution<f32> for StandardNormal {
41+
#[inline]
42+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f32 {
43+
// TODO: use optimal 32-bit implementation
44+
let x: f64 = self.sample(rng);
45+
x as f32
46+
}
47+
}
48+
4049
impl Distribution<f64> for StandardNormal {
4150
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
4251
#[inline]
@@ -121,7 +130,7 @@ impl Normal {
121130
}
122131
impl Distribution<f64> for Normal {
123132
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
124-
let n = rng.sample(StandardNormal);
133+
let n: f64 = rng.sample(StandardNormal);
125134
self.mean + self.std_dev * n
126135
}
127136
}

0 commit comments

Comments
 (0)