Skip to content

Commit f029e15

Browse files
committed
Improve documentation of rounding functions
1 parent 79e9f14 commit f029e15

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

src/libcore/num/f32.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,23 @@ impl Float for f32 {
117117
#[inline]
118118
fn neg_zero() -> f32 { -0.0 }
119119

120-
/// Returns `true` if the number is NaN
120+
/// Returns `true` if the number is NaN.
121121
#[inline]
122122
fn is_nan(self) -> bool { self != self }
123123

124-
/// Returns `true` if the number is infinite
124+
/// Returns `true` if the number is infinite.
125125
#[inline]
126126
fn is_infinite(self) -> bool {
127127
self == Float::infinity() || self == Float::neg_infinity()
128128
}
129129

130-
/// Returns `true` if the number is neither infinite or NaN
130+
/// Returns `true` if the number is neither infinite or NaN.
131131
#[inline]
132132
fn is_finite(self) -> bool {
133133
!(self.is_nan() || self.is_infinite())
134134
}
135135

136-
/// Returns `true` if the number is neither zero, infinite, subnormal or NaN
136+
/// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
137137
#[inline]
138138
fn is_normal(self) -> bool {
139139
self.classify() == FPNormal
@@ -195,25 +195,25 @@ impl Float for f32 {
195195
(mantissa as u64, exponent, sign)
196196
}
197197

198-
/// Round half-way cases toward `NEG_INFINITY`
198+
/// Rounds towards minus infinity.
199199
#[inline]
200200
fn floor(self) -> f32 {
201201
unsafe { intrinsics::floorf32(self) }
202202
}
203203

204-
/// Round half-way cases toward `INFINITY`
204+
/// Rounds towards plus infinity.
205205
#[inline]
206206
fn ceil(self) -> f32 {
207207
unsafe { intrinsics::ceilf32(self) }
208208
}
209209

210-
/// Round half-way cases away from `0.0`
210+
/// Rounds to nearest integer. Rounds half-way cases away from zero.
211211
#[inline]
212212
fn round(self) -> f32 {
213213
unsafe { intrinsics::roundf32(self) }
214214
}
215215

216-
/// The integer part of the number (rounds towards `0.0`)
216+
/// Returns the integer part of the number (rounds towards zero).
217217
#[inline]
218218
fn trunc(self) -> f32 {
219219
unsafe { intrinsics::truncf32(self) }
@@ -236,7 +236,7 @@ impl Float for f32 {
236236
unsafe { intrinsics::fmaf32(self, a, b) }
237237
}
238238

239-
/// The reciprocal (multiplicative inverse) of the number
239+
/// Returns the reciprocal (multiplicative inverse) of the number.
240240
#[inline]
241241
fn recip(self) -> f32 { 1.0 / self }
242242

@@ -325,45 +325,45 @@ impl Float for f32 {
325325
#[inline]
326326
fn ln_10() -> f32 { consts::LN_10 }
327327

328-
/// Returns the exponential of the number
328+
/// Returns the exponential of the number.
329329
#[inline]
330330
fn exp(self) -> f32 {
331331
unsafe { intrinsics::expf32(self) }
332332
}
333333

334-
/// Returns 2 raised to the power of the number
334+
/// Returns 2 raised to the power of the number.
335335
#[inline]
336336
fn exp2(self) -> f32 {
337337
unsafe { intrinsics::exp2f32(self) }
338338
}
339339

340-
/// Returns the natural logarithm of the number
340+
/// Returns the natural logarithm of the number.
341341
#[inline]
342342
fn ln(self) -> f32 {
343343
unsafe { intrinsics::logf32(self) }
344344
}
345345

346-
/// Returns the logarithm of the number with respect to an arbitrary base
346+
/// Returns the logarithm of the number with respect to an arbitrary base.
347347
#[inline]
348348
fn log(self, base: f32) -> f32 { self.ln() / base.ln() }
349349

350-
/// Returns the base 2 logarithm of the number
350+
/// Returns the base 2 logarithm of the number.
351351
#[inline]
352352
fn log2(self) -> f32 {
353353
unsafe { intrinsics::log2f32(self) }
354354
}
355355

356-
/// Returns the base 10 logarithm of the number
356+
/// Returns the base 10 logarithm of the number.
357357
#[inline]
358358
fn log10(self) -> f32 {
359359
unsafe { intrinsics::log10f32(self) }
360360
}
361361

362-
/// Converts to degrees, assuming the number is in radians
362+
/// Converts to degrees, assuming the number is in radians.
363363
#[inline]
364364
fn to_degrees(self) -> f32 { self * (180.0f32 / Float::pi()) }
365365

366-
/// Converts to radians, assuming the number is in degrees
366+
/// Converts to radians, assuming the number is in degrees.
367367
#[inline]
368368
fn to_radians(self) -> f32 {
369369
let value: f32 = Float::pi();

src/libcore/num/f64.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,23 @@ impl Float for f64 {
123123
#[inline]
124124
fn neg_zero() -> f64 { -0.0 }
125125

126-
/// Returns `true` if the number is NaN
126+
/// Returns `true` if the number is NaN.
127127
#[inline]
128128
fn is_nan(self) -> bool { self != self }
129129

130-
/// Returns `true` if the number is infinite
130+
/// Returns `true` if the number is infinite.
131131
#[inline]
132132
fn is_infinite(self) -> bool {
133133
self == Float::infinity() || self == Float::neg_infinity()
134134
}
135135

136-
/// Returns `true` if the number is neither infinite or NaN
136+
/// Returns `true` if the number is neither infinite or NaN.
137137
#[inline]
138138
fn is_finite(self) -> bool {
139139
!(self.is_nan() || self.is_infinite())
140140
}
141141

142-
/// Returns `true` if the number is neither zero, infinite, subnormal or NaN
142+
/// Returns `true` if the number is neither zero, infinite, subnormal or NaN.
143143
#[inline]
144144
fn is_normal(self) -> bool {
145145
self.classify() == FPNormal
@@ -201,25 +201,25 @@ impl Float for f64 {
201201
(mantissa, exponent, sign)
202202
}
203203

204-
/// Round half-way cases toward `NEG_INFINITY`
204+
/// Rounds towards minus infinity.
205205
#[inline]
206206
fn floor(self) -> f64 {
207207
unsafe { intrinsics::floorf64(self) }
208208
}
209209

210-
/// Round half-way cases toward `INFINITY`
210+
/// Rounds towards plus infinity.
211211
#[inline]
212212
fn ceil(self) -> f64 {
213213
unsafe { intrinsics::ceilf64(self) }
214214
}
215215

216-
/// Round half-way cases away from `0.0`
216+
/// Rounds to nearest integer. Rounds half-way cases away from zero.
217217
#[inline]
218218
fn round(self) -> f64 {
219219
unsafe { intrinsics::roundf64(self) }
220220
}
221221

222-
/// The integer part of the number (rounds towards `0.0`)
222+
/// Returns the integer part of the number (rounds towards zero).
223223
#[inline]
224224
fn trunc(self) -> f64 {
225225
unsafe { intrinsics::truncf64(self) }
@@ -242,7 +242,7 @@ impl Float for f64 {
242242
unsafe { intrinsics::fmaf64(self, a, b) }
243243
}
244244

245-
/// The reciprocal (multiplicative inverse) of the number
245+
/// Returns the reciprocal (multiplicative inverse) of the number.
246246
#[inline]
247247
fn recip(self) -> f64 { 1.0 / self }
248248

@@ -332,46 +332,45 @@ impl Float for f64 {
332332
#[inline]
333333
fn ln_10() -> f64 { consts::LN_10 }
334334

335-
/// Returns the exponential of the number
335+
/// Returns the exponential of the number.
336336
#[inline]
337337
fn exp(self) -> f64 {
338338
unsafe { intrinsics::expf64(self) }
339339
}
340340

341-
/// Returns 2 raised to the power of the number
341+
/// Returns 2 raised to the power of the number.
342342
#[inline]
343343
fn exp2(self) -> f64 {
344344
unsafe { intrinsics::exp2f64(self) }
345345
}
346346

347-
/// Returns the natural logarithm of the number
347+
/// Returns the natural logarithm of the number.
348348
#[inline]
349349
fn ln(self) -> f64 {
350350
unsafe { intrinsics::logf64(self) }
351351
}
352352

353-
/// Returns the logarithm of the number with respect to an arbitrary base
353+
/// Returns the logarithm of the number with respect to an arbitrary base.
354354
#[inline]
355355
fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
356356

357-
/// Returns the base 2 logarithm of the number
357+
/// Returns the base 2 logarithm of the number.
358358
#[inline]
359359
fn log2(self) -> f64 {
360360
unsafe { intrinsics::log2f64(self) }
361361
}
362362

363-
/// Returns the base 10 logarithm of the number
363+
/// Returns the base 10 logarithm of the number.
364364
#[inline]
365365
fn log10(self) -> f64 {
366366
unsafe { intrinsics::log10f64(self) }
367367
}
368368

369-
370-
/// Converts to degrees, assuming the number is in radians
369+
/// Converts to degrees, assuming the number is in radians.
371370
#[inline]
372371
fn to_degrees(self) -> f64 { self * (180.0f64 / Float::pi()) }
373372

374-
/// Converts to radians, assuming the number is in degrees
373+
/// Converts to radians, assuming the number is in degrees.
375374
#[inline]
376375
fn to_radians(self) -> f64 {
377376
let value: f64 = Float::pi();

src/libnum/rational.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ pub type BigRational = Ratio<BigInt>;
3838

3939
impl<T: Clone + Integer + PartialOrd>
4040
Ratio<T> {
41-
/// Create a ratio representing the integer `t`.
41+
/// Creates a ratio representing the integer `t`.
4242
#[inline]
4343
pub fn from_integer(t: T) -> Ratio<T> {
4444
Ratio::new_raw(t, One::one())
4545
}
4646

47-
/// Create a ratio without checking for `denom == 0` or reducing.
47+
/// Creates a ratio without checking for `denom == 0` or reducing.
4848
#[inline]
4949
pub fn new_raw(numer: T, denom: T) -> Ratio<T> {
5050
Ratio { numer: numer, denom: denom }
@@ -61,7 +61,7 @@ impl<T: Clone + Integer + PartialOrd>
6161
ret
6262
}
6363

64-
/// Convert to an integer.
64+
/// Converts to an integer.
6565
#[inline]
6666
pub fn to_integer(&self) -> T {
6767
self.trunc().numer
@@ -79,7 +79,7 @@ impl<T: Clone + Integer + PartialOrd>
7979
&self.denom
8080
}
8181

82-
/// Return true if the rational number is an integer (denominator is 1).
82+
/// Returns true if the rational number is an integer (denominator is 1).
8383
#[inline]
8484
pub fn is_integer(&self) -> bool {
8585
self.denom == One::one()
@@ -103,19 +103,21 @@ impl<T: Clone + Integer + PartialOrd>
103103
}
104104
}
105105

106-
/// Return a `reduce`d copy of self.
106+
/// Returns a `reduce`d copy of self.
107107
pub fn reduced(&self) -> Ratio<T> {
108108
let mut ret = self.clone();
109109
ret.reduce();
110110
ret
111111
}
112112

113-
/// Return the reciprocal
113+
/// Returns the reciprocal.
114114
#[inline]
115115
pub fn recip(&self) -> Ratio<T> {
116116
Ratio::new_raw(self.denom.clone(), self.numer.clone())
117117
}
118118

119+
/// Rounds towards minus infinity.
120+
#[inline]
119121
pub fn floor(&self) -> Ratio<T> {
120122
if *self < Zero::zero() {
121123
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
@@ -124,6 +126,8 @@ impl<T: Clone + Integer + PartialOrd>
124126
}
125127
}
126128

129+
/// Rounds towards plus infinity.
130+
#[inline]
127131
pub fn ceil(&self) -> Ratio<T> {
128132
if *self < Zero::zero() {
129133
Ratio::from_integer(self.numer / self.denom)
@@ -132,27 +136,34 @@ impl<T: Clone + Integer + PartialOrd>
132136
}
133137
}
134138

139+
/// Rounds to the nearest integer. Rounds half-way cases away from zero.
140+
///
141+
/// Note: This function is currently broken and always rounds away from zero.
135142
#[inline]
136143
pub fn round(&self) -> Ratio<T> {
144+
// FIXME(#15826)
137145
if *self < Zero::zero() {
138146
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
139147
} else {
140148
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
141149
}
142150
}
143151

152+
/// Rounds towards zero.
144153
#[inline]
145154
pub fn trunc(&self) -> Ratio<T> {
146155
Ratio::from_integer(self.numer / self.denom)
147156
}
148157

158+
///Returns the fractional part of a number.
159+
#[inline]
149160
pub fn fract(&self) -> Ratio<T> {
150161
Ratio::new_raw(self.numer % self.denom, self.denom.clone())
151162
}
152163
}
153164

154165
impl Ratio<BigInt> {
155-
/// Converts a float into a rational number
166+
/// Converts a float into a rational number.
156167
pub fn from_float<T: Float>(f: T) -> Option<BigRational> {
157168
if !f.is_finite() {
158169
return None;
@@ -328,7 +339,7 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
328339

329340
impl<T: FromStr + Clone + Integer + PartialOrd>
330341
FromStr for Ratio<T> {
331-
/// Parses `numer/denom` or just `numer`
342+
/// Parses `numer/denom` or just `numer`.
332343
fn from_str(s: &str) -> Option<Ratio<T>> {
333344
let mut split = s.splitn('/', 1);
334345

0 commit comments

Comments
 (0)