@@ -35,7 +35,7 @@ use rand::Rng;
35
35
/// use rand::prelude::*;
36
36
/// use rand_distr::Zipf;
37
37
///
38
- /// let val: f64 = rand::rng().sample(Zipf::new(10, 1.5).unwrap());
38
+ /// let val: f64 = rand::rng().sample(Zipf::new(10.0 , 1.5).unwrap());
39
39
/// println!("{}", val);
40
40
/// ```
41
41
///
@@ -92,16 +92,17 @@ where
92
92
/// Construct a new `Zipf` distribution for a set with `n` elements and a
93
93
/// frequency rank exponent `s`.
94
94
///
95
- /// For large `n`, rounding may occur to fit the number into the float type.
95
+ /// The parameter `n` is typically integral, however we use type
96
+ /// <pre><code>F: [Float]</code></pre> in order to permit very large values
97
+ /// and since our implementation requires a floating-point type.
96
98
#[ inline]
97
- pub fn new ( n : u64 , s : F ) -> Result < Zipf < F > , Error > {
99
+ pub fn new ( n : F , s : F ) -> Result < Zipf < F > , Error > {
98
100
if !( s >= F :: zero ( ) ) {
99
101
return Err ( Error :: STooSmall ) ;
100
102
}
101
- if n < 1 {
103
+ if n < F :: one ( ) {
102
104
return Err ( Error :: NTooSmall ) ;
103
105
}
104
- let n = F :: from ( n) . unwrap ( ) ; // This does not fail.
105
106
let q = if s != F :: one ( ) {
106
107
// Make sure to calculate the division only once.
107
108
F :: one ( ) / ( F :: one ( ) - s)
@@ -173,24 +174,24 @@ mod tests {
173
174
#[ test]
174
175
#[ should_panic]
175
176
fn zipf_s_too_small ( ) {
176
- Zipf :: new ( 10 , -1. ) . unwrap ( ) ;
177
+ Zipf :: new ( 10. , -1. ) . unwrap ( ) ;
177
178
}
178
179
179
180
#[ test]
180
181
#[ should_panic]
181
182
fn zipf_n_too_small ( ) {
182
- Zipf :: new ( 0 , 1. ) . unwrap ( ) ;
183
+ Zipf :: new ( 0. , 1. ) . unwrap ( ) ;
183
184
}
184
185
185
186
#[ test]
186
187
#[ should_panic]
187
188
fn zipf_nan ( ) {
188
- Zipf :: new ( 10 , f64:: NAN ) . unwrap ( ) ;
189
+ Zipf :: new ( 10. , f64:: NAN ) . unwrap ( ) ;
189
190
}
190
191
191
192
#[ test]
192
193
fn zipf_sample ( ) {
193
- let d = Zipf :: new ( 10 , 0.5 ) . unwrap ( ) ;
194
+ let d = Zipf :: new ( 10. , 0.5 ) . unwrap ( ) ;
194
195
let mut rng = crate :: test:: rng ( 2 ) ;
195
196
for _ in 0 ..1000 {
196
197
let r = d. sample ( & mut rng) ;
@@ -200,7 +201,7 @@ mod tests {
200
201
201
202
#[ test]
202
203
fn zipf_sample_s_1 ( ) {
203
- let d = Zipf :: new ( 10 , 1. ) . unwrap ( ) ;
204
+ let d = Zipf :: new ( 10. , 1. ) . unwrap ( ) ;
204
205
let mut rng = crate :: test:: rng ( 2 ) ;
205
206
for _ in 0 ..1000 {
206
207
let r = d. sample ( & mut rng) ;
@@ -210,7 +211,7 @@ mod tests {
210
211
211
212
#[ test]
212
213
fn zipf_sample_s_0 ( ) {
213
- let d = Zipf :: new ( 10 , 0. ) . unwrap ( ) ;
214
+ let d = Zipf :: new ( 10. , 0. ) . unwrap ( ) ;
214
215
let mut rng = crate :: test:: rng ( 2 ) ;
215
216
for _ in 0 ..1000 {
216
217
let r = d. sample ( & mut rng) ;
@@ -221,7 +222,7 @@ mod tests {
221
222
222
223
#[ test]
223
224
fn zipf_sample_large_n ( ) {
224
- let d = Zipf :: new ( u64 :: MAX , 1.5 ) . unwrap ( ) ;
225
+ let d = Zipf :: new ( f64 :: MAX , 1.5 ) . unwrap ( ) ;
225
226
let mut rng = crate :: test:: rng ( 2 ) ;
226
227
for _ in 0 ..1000 {
227
228
let r = d. sample ( & mut rng) ;
@@ -232,12 +233,12 @@ mod tests {
232
233
233
234
#[ test]
234
235
fn zipf_value_stability ( ) {
235
- test_samples ( Zipf :: new ( 10 , 0.5 ) . unwrap ( ) , 0f32 , & [ 10.0 , 2.0 , 6.0 , 7.0 ] ) ;
236
- test_samples ( Zipf :: new ( 10 , 2.0 ) . unwrap ( ) , 0f64 , & [ 1.0 , 2.0 , 3.0 , 2.0 ] ) ;
236
+ test_samples ( Zipf :: new ( 10. , 0.5 ) . unwrap ( ) , 0f32 , & [ 10.0 , 2.0 , 6.0 , 7.0 ] ) ;
237
+ test_samples ( Zipf :: new ( 10. , 2.0 ) . unwrap ( ) , 0f64 , & [ 1.0 , 2.0 , 3.0 , 2.0 ] ) ;
237
238
}
238
239
239
240
#[ test]
240
241
fn zipf_distributions_can_be_compared ( ) {
241
- assert_eq ! ( Zipf :: new( 1 , 2.0 ) , Zipf :: new( 1 , 2.0 ) ) ;
242
+ assert_eq ! ( Zipf :: new( 1.0 , 2.0 ) , Zipf :: new( 1.0 , 2.0 ) ) ;
242
243
}
243
244
}
0 commit comments