Skip to content

Commit 78a35f1

Browse files
bors[bot]zetokcuviper
authored
Merge #84
84: Better document panic behaviour for `Ratio`s with zero in denominator r=cuviper a=zetok Co-authored-by: Zetok Zalbavar <zexavexxe@gmail.com> Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents 2ad63fb + 06c3a74 commit 78a35f1

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub type BigRational = Ratio<BigInt>;
7070
/// These method are `const` for Rust 1.31 and later.
7171
impl<T> Ratio<T> {
7272
/// 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`.**
7376
#[inline]
7477
pub const fn new_raw(numer: T, denom: T) -> Ratio<T> {
7578
Ratio { numer, denom }
@@ -89,7 +92,9 @@ impl<T> Ratio<T> {
8992
}
9093

9194
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.**
9398
#[inline]
9499
pub fn new(numer: T, denom: T) -> Ratio<T> {
95100
let mut ret = Ratio::new_raw(numer, denom);
@@ -115,7 +120,9 @@ impl<T: Clone + Integer> Ratio<T> {
115120
self.denom.is_one()
116121
}
117122

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.**
119126
fn reduce(&mut self) {
120127
if self.denom.is_zero() {
121128
panic!("denominator == 0");
@@ -150,6 +157,8 @@ impl<T: Clone + Integer> Ratio<T> {
150157
///
151158
/// In general, it is not necessary to use this method, as the only
152159
/// method of procuring a non-reduced fraction is through `new_raw`.
160+
///
161+
/// **Panics if `denom` is zero.**
153162
pub fn reduced(&self) -> Ratio<T> {
154163
let mut ret = self.clone();
155164
ret.reduce();
@@ -158,7 +167,7 @@ impl<T: Clone + Integer> Ratio<T> {
158167

159168
/// Returns the reciprocal.
160169
///
161-
/// Fails if the `Ratio` is zero.
170+
/// **Panics if the `Ratio` is zero.**
162171
#[inline]
163172
pub fn recip(&self) -> Ratio<T> {
164173
self.clone().into_recip()
@@ -468,7 +477,7 @@ mod opassign {
468477
self.numer *= other.denom / gcd_bd.clone();
469478
self.denom /= gcd_bd;
470479
self.denom *= other.numer / gcd_ac;
471-
self.reduce(); //TODO: remove this line. see #8.
480+
self.reduce(); // TODO: remove this line. see #8.
472481
}
473482
}
474483

@@ -481,7 +490,7 @@ mod opassign {
481490
self.numer *= other.numer / gcd_bc.clone();
482491
self.denom /= gcd_bc;
483492
self.denom *= other.denom / gcd_ad;
484-
self.reduce(); //TODO: remove this line. see #8.
493+
self.reduce(); // TODO: remove this line. see #8.
485494
}
486495
}
487496

@@ -528,7 +537,7 @@ mod opassign {
528537
let gcd = self.numer.gcd(&other);
529538
self.numer /= gcd.clone();
530539
self.denom *= other / gcd;
531-
self.reduce(); //TODO: remove this line. see #8.
540+
self.reduce(); // TODO: remove this line. see #8.
532541
}
533542
}
534543

@@ -537,7 +546,7 @@ mod opassign {
537546
let gcd = self.denom.gcd(&other);
538547
self.denom /= gcd.clone();
539548
self.numer *= other / gcd;
540-
self.reduce(); //TODO: remove this line. see #8.
549+
self.reduce(); // TODO: remove this line. see #8.
541550
}
542551
}
543552

@@ -1016,7 +1025,7 @@ macro_rules! impl_formatting {
10161025
format!(concat!($fmt_str, "/", $fmt_str), self.numer, self.denom)
10171026
}
10181027
};
1019-
//TODO: replace with strip_prefix, when stabalized
1028+
// TODO: replace with strip_prefix, when stabalized
10201029
let (pre_pad, non_negative) = {
10211030
if pre_pad.starts_with("-") {
10221031
(&pre_pad[1..], false)
@@ -1946,7 +1955,7 @@ mod test {
19461955
assert_fmt_eq!(format_args!("{:-b}", _1_2), "1/10");
19471956
assert_fmt_eq!(format_args!("{:b}", _0), "0");
19481957
assert_fmt_eq!(format_args!("{:#b}", _1_2), "0b1/0b10");
1949-
//no std does not support padding
1958+
// no std does not support padding
19501959
#[cfg(feature = "std")]
19511960
assert_eq!(&format!("{:010b}", _1_2), "0000001/10");
19521961
#[cfg(feature = "std")]
@@ -2378,7 +2387,7 @@ mod test {
23782387
T: Integer + Bounded + Clone + Debug + NumAssign,
23792388
{
23802389
let two = T::one() + T::one();
2381-
//value near to maximum, but divisible by two
2390+
// value near to maximum, but divisible by two
23822391
let max_div2 = T::max_value() / two.clone() * two.clone();
23832392
let _1_max: Ratio<T> = Ratio::new(T::one(), max_div2);
23842393
let _1_two: Ratio<T> = Ratio::new(T::one(), two);

0 commit comments

Comments
 (0)