@@ -70,6 +70,9 @@ pub type BigRational = Ratio<BigInt>;
70
70
/// These method are `const` for Rust 1.31 and later.
71
71
impl < T > Ratio < T > {
72
72
/// Creates a `Ratio` without checking for `denom == 0` or reducing.
73
+ ///
74
+ /// **There are several methods that will panic if used on a `Ratio` with
75
+ /// `denom == 0`.**
73
76
#[ inline]
74
77
pub const fn new_raw ( numer : T , denom : T ) -> Ratio < T > {
75
78
Ratio { numer, denom }
@@ -89,7 +92,9 @@ impl<T> Ratio<T> {
89
92
}
90
93
91
94
impl < T : Clone + Integer > Ratio < T > {
92
- /// Creates a new `Ratio`. Fails if `denom` is zero.
95
+ /// Creates a new `Ratio`.
96
+ ///
97
+ /// **Panics if `denom` is zero.**
93
98
#[ inline]
94
99
pub fn new ( numer : T , denom : T ) -> Ratio < T > {
95
100
let mut ret = Ratio :: new_raw ( numer, denom) ;
@@ -115,7 +120,9 @@ impl<T: Clone + Integer> Ratio<T> {
115
120
self . denom . is_one ( )
116
121
}
117
122
118
- /// Puts self into lowest terms, with denom > 0.
123
+ /// Puts self into lowest terms, with `denom` > 0.
124
+ ///
125
+ /// **Panics if `denom` is zero.**
119
126
fn reduce ( & mut self ) {
120
127
if self . denom . is_zero ( ) {
121
128
panic ! ( "denominator == 0" ) ;
@@ -150,6 +157,8 @@ impl<T: Clone + Integer> Ratio<T> {
150
157
///
151
158
/// In general, it is not necessary to use this method, as the only
152
159
/// method of procuring a non-reduced fraction is through `new_raw`.
160
+ ///
161
+ /// **Panics if `denom` is zero.**
153
162
pub fn reduced ( & self ) -> Ratio < T > {
154
163
let mut ret = self . clone ( ) ;
155
164
ret. reduce ( ) ;
@@ -158,7 +167,7 @@ impl<T: Clone + Integer> Ratio<T> {
158
167
159
168
/// Returns the reciprocal.
160
169
///
161
- /// Fails if the `Ratio` is zero.
170
+ /// **Panics if the `Ratio` is zero.**
162
171
#[ inline]
163
172
pub fn recip ( & self ) -> Ratio < T > {
164
173
self . clone ( ) . into_recip ( )
@@ -468,7 +477,7 @@ mod opassign {
468
477
self . numer *= other. denom / gcd_bd. clone ( ) ;
469
478
self . denom /= gcd_bd;
470
479
self . denom *= other. numer / gcd_ac;
471
- self . reduce ( ) ; //TODO: remove this line. see #8.
480
+ self . reduce ( ) ; // TODO: remove this line. see #8.
472
481
}
473
482
}
474
483
@@ -481,7 +490,7 @@ mod opassign {
481
490
self . numer *= other. numer / gcd_bc. clone ( ) ;
482
491
self . denom /= gcd_bc;
483
492
self . denom *= other. denom / gcd_ad;
484
- self . reduce ( ) ; //TODO: remove this line. see #8.
493
+ self . reduce ( ) ; // TODO: remove this line. see #8.
485
494
}
486
495
}
487
496
@@ -528,7 +537,7 @@ mod opassign {
528
537
let gcd = self . numer . gcd ( & other) ;
529
538
self . numer /= gcd. clone ( ) ;
530
539
self . denom *= other / gcd;
531
- self . reduce ( ) ; //TODO: remove this line. see #8.
540
+ self . reduce ( ) ; // TODO: remove this line. see #8.
532
541
}
533
542
}
534
543
@@ -537,7 +546,7 @@ mod opassign {
537
546
let gcd = self . denom . gcd ( & other) ;
538
547
self . denom /= gcd. clone ( ) ;
539
548
self . numer *= other / gcd;
540
- self . reduce ( ) ; //TODO: remove this line. see #8.
549
+ self . reduce ( ) ; // TODO: remove this line. see #8.
541
550
}
542
551
}
543
552
@@ -1016,7 +1025,7 @@ macro_rules! impl_formatting {
1016
1025
format!( concat!( $fmt_str, "/" , $fmt_str) , self . numer, self . denom)
1017
1026
}
1018
1027
} ;
1019
- //TODO: replace with strip_prefix, when stabalized
1028
+ // TODO: replace with strip_prefix, when stabalized
1020
1029
let ( pre_pad, non_negative) = {
1021
1030
if pre_pad. starts_with( "-" ) {
1022
1031
( & pre_pad[ 1 ..] , false )
@@ -1946,7 +1955,7 @@ mod test {
1946
1955
assert_fmt_eq ! ( format_args!( "{:-b}" , _1_2) , "1/10" ) ;
1947
1956
assert_fmt_eq ! ( format_args!( "{:b}" , _0) , "0" ) ;
1948
1957
assert_fmt_eq ! ( format_args!( "{:#b}" , _1_2) , "0b1/0b10" ) ;
1949
- //no std does not support padding
1958
+ // no std does not support padding
1950
1959
#[ cfg( feature = "std" ) ]
1951
1960
assert_eq ! ( & format!( "{:010b}" , _1_2) , "0000001/10" ) ;
1952
1961
#[ cfg( feature = "std" ) ]
@@ -2378,7 +2387,7 @@ mod test {
2378
2387
T : Integer + Bounded + Clone + Debug + NumAssign ,
2379
2388
{
2380
2389
let two = T :: one ( ) + T :: one ( ) ;
2381
- //value near to maximum, but divisible by two
2390
+ // value near to maximum, but divisible by two
2382
2391
let max_div2 = T :: max_value ( ) / two. clone ( ) * two. clone ( ) ;
2383
2392
let _1_max: Ratio < T > = Ratio :: new ( T :: one ( ) , max_div2) ;
2384
2393
let _1_two: Ratio < T > = Ratio :: new ( T :: one ( ) , two) ;
0 commit comments