99
1010//! The Cauchy distribution.
1111
12- use crate :: utils :: Float ;
12+ use num_traits :: { Float , FloatConst } ;
1313use crate :: { Distribution , Standard } ;
1414use 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 >
5859where 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 >
7172where 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 ) ;
0 commit comments