File tree 4 files changed +15
-3
lines changed
4 files changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -372,9 +372,10 @@ pub trait Int
372
372
#[ unstable( feature = "core" ,
373
373
reason = "pending integer conventions" ) ]
374
374
#[ inline]
375
- fn pow ( self , mut exp : uint ) -> Self {
375
+ fn pow ( self , mut exp : u32 ) -> Self {
376
376
let mut base = self ;
377
377
let mut acc: Self = Int :: one ( ) ;
378
+
378
379
while exp > 0 {
379
380
if ( exp & 1 ) == 1 {
380
381
acc = acc * base;
Original file line number Diff line number Diff line change @@ -201,6 +201,17 @@ mod tests {
201
201
assert_eq!( FromStrRadix :: from_str_radix( "Z" , 35 ) . ok( ) , None :: <$T>) ;
202
202
assert_eq!( FromStrRadix :: from_str_radix( "-9" , 2 ) . ok( ) , None :: <$T>) ;
203
203
}
204
+
205
+ #[ test]
206
+ fn test_pow( ) {
207
+ let mut r = 2 as $T;
208
+
209
+ assert_eq!( r. pow( 2u32 ) , 4 as $T) ;
210
+ assert_eq!( r. pow( 0u32 ) , 1 as $T) ;
211
+ r = -2 as $T;
212
+ assert_eq!( r. pow( 2u32 ) , 4 as $T) ;
213
+ assert_eq!( r. pow( 3u32 ) , -8 as $T) ;
214
+ }
204
215
}
205
216
206
217
) }
Original file line number Diff line number Diff line change @@ -1830,6 +1830,6 @@ mod bench {
1830
1830
#[ bench]
1831
1831
fn bench_pow_function ( b : & mut Bencher ) {
1832
1832
let v = ( 0 ..1024 ) . collect :: < Vec < _ > > ( ) ;
1833
- b. iter ( || { v. iter ( ) . fold ( 0 , |old, new| old. pow ( * new) ) ; } ) ;
1833
+ b. iter ( || { v. iter ( ) . fold ( 0 , |old, new| old. pow ( * new as u32 ) ) ; } ) ;
1834
1834
}
1835
1835
}
Original file line number Diff line number Diff line change @@ -109,7 +109,7 @@ fn main() {
109
109
110
110
let messages = range_step ( min_depth, max_depth + 1 , 2 ) . map ( |depth| {
111
111
use std:: num:: Int ;
112
- let iterations = 2 . pow ( ( max_depth - depth + min_depth) as usize ) ;
112
+ let iterations = 2 . pow ( ( max_depth - depth + min_depth) as u32 ) ;
113
113
thread:: scoped ( move || inner ( depth, iterations) )
114
114
} ) . collect :: < Vec < _ > > ( ) ;
115
115
You can’t perform that action at this time.
0 commit comments