Skip to content

Commit 3367a48

Browse files
committed
migrate to num-traits, raise MSRV to 1.36
1 parent 2c2fbd6 commit 3367a48

23 files changed

+237
-369
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ sudo: false
66

77
matrix:
88
include:
9-
- rust: 1.32.0
10-
name: "Linux, 1.32.0"
9+
- rust: 1.36.0
10+
name: "Linux, 1.36.0"
1111
env: ALLOC=0
1212
os: linux
1313

rand_distr/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ travis-ci = { repository = "rust-random/rand" }
2020
appveyor = { repository = "rust-random/rand" }
2121

2222
[dependencies]
23-
rand = { path = "..", version = "0.7" }
23+
rand = { path = "..", version = "0.7", default-features = false }
24+
num-traits = { version = "0.2", default-features = false, features = ["libm"] }
25+
26+
[features]
27+
default = ["std"]
28+
std = ["rand/std", "num-traits/std", "alloc"]
29+
alloc = ["rand/alloc"]
2430

2531
[dev-dependencies]
2632
rand_pcg = { version = "0.2", path = "../rand_pcg" }
33+
# For inline examples
34+
rand = { path = "..", version = "0.7", default-features = false, features = ["std_rng", "std"] }
2735
# Histogram implementation for testing uniformity
2836
average = "0.10.3"

rand_distr/src/binomial.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
use crate::{Distribution, Uniform};
1313
use rand::Rng;
14-
use std::{error, fmt};
14+
use core::fmt;
1515

1616
/// The binomial distribution `Binomial(n, p)`.
1717
///
@@ -53,7 +53,8 @@ impl fmt::Display for Error {
5353
}
5454
}
5555

56-
impl error::Error for Error {}
56+
#[cfg(feature = "std")]
57+
impl std::error::Error for Error {}
5758

5859
impl Binomial {
5960
/// Construct a new `Binomial` with the given shape parameters `n` (number
@@ -72,7 +73,7 @@ impl Binomial {
7273
/// Convert a `f64` to an `i64`, panicing on overflow.
7374
// In the future (Rust 1.34), this might be replaced with `TryFrom`.
7475
fn f64_to_i64(x: f64) -> i64 {
75-
assert!(x < (::std::i64::MAX as f64));
76+
assert!(x < (core::i64::MAX as f64));
7677
x as i64
7778
}
7879

@@ -106,7 +107,7 @@ impl Distribution<u64> for Binomial {
106107
// Ranlib uses 30, and GSL uses 14.
107108
const BINV_THRESHOLD: f64 = 10.;
108109

109-
if (self.n as f64) * p < BINV_THRESHOLD && self.n <= (::std::i32::MAX as u64) {
110+
if (self.n as f64) * p < BINV_THRESHOLD && self.n <= (core::i32::MAX as u64) {
110111
// Use the BINV algorithm.
111112
let s = p / q;
112113
let a = ((self.n + 1) as f64) * s;

rand_distr/src/cauchy.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
//! The Cauchy distribution.
1111
12-
use crate::utils::Float;
12+
use num_traits::{Float, FloatConst};
1313
use crate::{Distribution, Standard};
1414
use rand::Rng;
15-
use std::{error, fmt};
15+
use core::fmt;
1616

1717
/// The Cauchy distribution `Cauchy(median, scale)`.
1818
///
@@ -52,30 +52,31 @@ impl fmt::Display for Error {
5252
}
5353
}
5454

55-
impl error::Error for Error {}
55+
#[cfg(feature = "std")]
56+
impl std::error::Error for Error {}
5657

57-
impl<N: Float> Cauchy<N>
58+
impl<N: Float + FloatConst> Cauchy<N>
5859
where Standard: Distribution<N>
5960
{
6061
/// Construct a new `Cauchy` with the given shape parameters
6162
/// `median` the peak location and `scale` the scale factor.
6263
pub fn new(median: N, scale: N) -> Result<Cauchy<N>, Error> {
63-
if !(scale > N::from(0.0)) {
64+
if !(scale > N::zero()) {
6465
return Err(Error::ScaleTooSmall);
6566
}
6667
Ok(Cauchy { median, scale })
6768
}
6869
}
6970

70-
impl<N: Float> Distribution<N> for Cauchy<N>
71+
impl<N: Float + FloatConst> Distribution<N> for Cauchy<N>
7172
where Standard: Distribution<N>
7273
{
7374
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> N {
7475
// sample from [0, 1)
7576
let x = Standard.sample(rng);
7677
// get standard cauchy random number
7778
// note that π/2 is not exactly representable, even if x=0.5 the result is finite
78-
let comp_dev = (N::pi() * x).tan();
79+
let comp_dev = (N::PI() * x).tan();
7980
// shift and scale according to parameters
8081
self.median + self.scale * comp_dev
8182
}
@@ -108,10 +109,12 @@ mod test {
108109
sum += numbers[i];
109110
}
110111
let median = median(&mut numbers);
111-
println!("Cauchy median: {}", median);
112+
#[cfg(feature = "std")]
113+
std::println!("Cauchy median: {}", median);
112114
assert!((median - 10.0).abs() < 0.4); // not 100% certain, but probable enough
113115
let mean = sum / 1000.0;
114-
println!("Cauchy mean: {}", mean);
116+
#[cfg(feature = "std")]
117+
std::println!("Cauchy mean: {}", mean);
115118
// for a Cauchy distribution the mean should not converge
116119
assert!((mean - 10.0).abs() > 0.4); // not 100% certain, but probable enough
117120
}
@@ -130,7 +133,7 @@ mod test {
130133

131134
#[test]
132135
fn value_stability() {
133-
fn gen_samples<N: Float + core::fmt::Debug>(m: N, s: N, buf: &mut [N])
136+
fn gen_samples<N: Float + FloatConst + core::fmt::Debug>(m: N, s: N, buf: &mut [N])
134137
where Standard: Distribution<N> {
135138
let distr = Cauchy::new(m, s).unwrap();
136139
let mut rng = crate::test::rng(353);

rand_distr/src/dirichlet.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
// except according to those terms.
99

1010
//! The dirichlet distribution.
11-
12-
use crate::utils::Float;
11+
#![cfg(feature = "alloc")]
12+
use num_traits::Float;
1313
use crate::{Distribution, Exp1, Gamma, Open01, StandardNormal};
1414
use rand::Rng;
15-
use std::{error, fmt};
15+
use core::fmt;
16+
use alloc::{vec, vec::Vec};
1617

1718
/// The Dirichlet distribution `Dirichlet(alpha)`.
1819
///
@@ -58,7 +59,8 @@ impl fmt::Display for Error {
5859
}
5960
}
6061

61-
impl error::Error for Error {}
62+
#[cfg(feature = "std")]
63+
impl std::error::Error for Error {}
6264

6365
impl<N: Float> Dirichlet<N>
6466
where
@@ -76,7 +78,7 @@ where
7678
return Err(Error::AlphaTooShort);
7779
}
7880
for &ai in &a {
79-
if !(ai > N::from(0.0)) {
81+
if !(ai > N::zero()) {
8082
return Err(Error::AlphaTooSmall);
8183
}
8284
}
@@ -89,7 +91,7 @@ where
8991
/// Requires `size >= 2`.
9092
#[inline]
9193
pub fn new_with_size(alpha: N, size: usize) -> Result<Dirichlet<N>, Error> {
92-
if !(alpha > N::from(0.0)) {
94+
if !(alpha > N::zero()) {
9395
return Err(Error::AlphaTooSmall);
9496
}
9597
if size < 2 {
@@ -109,17 +111,17 @@ where
109111
{
110112
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec<N> {
111113
let n = self.alpha.len();
112-
let mut samples = vec![N::from(0.0); n];
113-
let mut sum = N::from(0.0);
114+
let mut samples = vec![N::zero(); n];
115+
let mut sum = N::zero();
114116

115117
for (s, &a) in samples.iter_mut().zip(self.alpha.iter()) {
116-
let g = Gamma::new(a, N::from(1.0)).unwrap();
118+
let g = Gamma::new(a, N::one()).unwrap();
117119
*s = g.sample(rng);
118-
sum += *s;
120+
sum = sum + (*s);
119121
}
120-
let invacc = N::from(1.0) / sum;
122+
let invacc = N::one() / sum;
121123
for s in samples.iter_mut() {
122-
*s *= invacc;
124+
*s = (*s)*invacc;
123125
}
124126
samples
125127
}

rand_distr/src/exponential.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
//! The exponential distribution.
1111
12-
use crate::utils::{ziggurat, Float};
12+
use crate::utils::ziggurat;
13+
use num_traits::Float;
1314
use crate::{ziggurat_tables, Distribution};
1415
use rand::Rng;
15-
use std::{error, fmt};
16+
use core::fmt;
1617

1718
/// Samples floating-point numbers according to the exponential distribution,
1819
/// with rate parameter `λ = 1`. This is equivalent to `Exp::new(1.0)` or
@@ -110,7 +111,8 @@ impl fmt::Display for Error {
110111
}
111112
}
112113

113-
impl error::Error for Error {}
114+
#[cfg(feature = "std")]
115+
impl std::error::Error for Error {}
114116

115117
impl<N: Float> Exp<N>
116118
where Exp1: Distribution<N>
@@ -119,11 +121,11 @@ where Exp1: Distribution<N>
119121
/// `lambda`.
120122
#[inline]
121123
pub fn new(lambda: N) -> Result<Exp<N>, Error> {
122-
if !(lambda > N::from(0.0)) {
124+
if !(lambda > N::zero()) {
123125
return Err(Error::LambdaTooSmall);
124126
}
125127
Ok(Exp {
126-
lambda_inverse: N::from(1.0) / lambda,
128+
lambda_inverse: N::one() / lambda,
127129
})
128130
}
129131
}

0 commit comments

Comments
 (0)