Skip to content

Commit fe47202

Browse files
committed
Merge the Round trait into the Float trait
Move the rounding functions into the `std::num::Float` trait and then remove `std::num::Round`. This continues the flattening of the numeric traits tracked in #10387. The aim is to make `std::num` very simple and tied to the built in types, leaving the definition of more complex numeric towers to third-party libraries. [breaking-change]
1 parent b75683c commit fe47202

File tree

6 files changed

+106
-118
lines changed

6 files changed

+106
-118
lines changed

src/libnum/rational.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use Integer;
1515
use std::cmp;
1616
use std::fmt;
1717
use std::from_str::FromStr;
18-
use std::num::{Zero,One,ToStrRadix,FromStrRadix,Round};
18+
use std::num::{Zero, One, ToStrRadix, FromStrRadix};
1919
use bigint::{BigInt, BigUint, Sign, Plus, Minus};
2020

2121
/// Represents the ratio between 2 numbers.
@@ -113,6 +113,40 @@ impl<T: Clone + Integer + Ord>
113113
pub fn recip(&self) -> Ratio<T> {
114114
Ratio::new_raw(self.denom.clone(), self.numer.clone())
115115
}
116+
117+
pub fn floor(&self) -> Ratio<T> {
118+
if *self < Zero::zero() {
119+
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
120+
} else {
121+
Ratio::from_integer(self.numer / self.denom)
122+
}
123+
}
124+
125+
pub fn ceil(&self) -> Ratio<T> {
126+
if *self < Zero::zero() {
127+
Ratio::from_integer(self.numer / self.denom)
128+
} else {
129+
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
130+
}
131+
}
132+
133+
#[inline]
134+
pub fn round(&self) -> Ratio<T> {
135+
if *self < Zero::zero() {
136+
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
137+
} else {
138+
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
139+
}
140+
}
141+
142+
#[inline]
143+
pub fn trunc(&self) -> Ratio<T> {
144+
Ratio::from_integer(self.numer / self.denom)
145+
}
146+
147+
pub fn fract(&self) -> Ratio<T> {
148+
Ratio::new_raw(self.numer % self.denom, self.denom.clone())
149+
}
116150
}
117151

118152
impl Ratio<BigInt> {
@@ -238,45 +272,6 @@ impl<T: Clone + Integer + Ord>
238272
impl<T: Clone + Integer + Ord>
239273
Num for Ratio<T> {}
240274

241-
/* Utils */
242-
impl<T: Clone + Integer + Ord>
243-
Round for Ratio<T> {
244-
245-
fn floor(&self) -> Ratio<T> {
246-
if *self < Zero::zero() {
247-
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
248-
} else {
249-
Ratio::from_integer(self.numer / self.denom)
250-
}
251-
}
252-
253-
fn ceil(&self) -> Ratio<T> {
254-
if *self < Zero::zero() {
255-
Ratio::from_integer(self.numer / self.denom)
256-
} else {
257-
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
258-
}
259-
}
260-
261-
#[inline]
262-
fn round(&self) -> Ratio<T> {
263-
if *self < Zero::zero() {
264-
Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
265-
} else {
266-
Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
267-
}
268-
}
269-
270-
#[inline]
271-
fn trunc(&self) -> Ratio<T> {
272-
Ratio::from_integer(self.numer / self.denom)
273-
}
274-
275-
fn fract(&self) -> Ratio<T> {
276-
Ratio::new_raw(self.numer % self.denom, self.denom.clone())
277-
}
278-
}
279-
280275
/* String conversions */
281276
impl<T: fmt::Show> fmt::Show for Ratio<T> {
282277
/// Renders as `numer/denom`.

src/libstd/num/f32.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -239,33 +239,6 @@ impl Signed for f32 {
239239
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
240240
}
241241

242-
impl Round for f32 {
243-
/// Round half-way cases toward `NEG_INFINITY`
244-
#[inline]
245-
fn floor(&self) -> f32 { unsafe{intrinsics::floorf32(*self)} }
246-
247-
/// Round half-way cases toward `INFINITY`
248-
#[inline]
249-
fn ceil(&self) -> f32 { unsafe{intrinsics::ceilf32(*self)} }
250-
251-
/// Round half-way cases away from `0.0`
252-
#[inline]
253-
fn round(&self) -> f32 { unsafe{intrinsics::roundf32(*self)} }
254-
255-
/// The integer part of the number (rounds towards `0.0`)
256-
#[inline]
257-
fn trunc(&self) -> f32 { unsafe{intrinsics::truncf32(*self)} }
258-
259-
/// The fractional part of the number, satisfying:
260-
///
261-
/// ```rust
262-
/// let x = 1.65f32;
263-
/// assert!(x == x.trunc() + x.fract())
264-
/// ```
265-
#[inline]
266-
fn fract(&self) -> f32 { *self - self.trunc() }
267-
}
268-
269242
impl Bounded for f32 {
270243
#[inline]
271244
fn min_value() -> f32 { 1.17549435e-38 }
@@ -414,6 +387,31 @@ impl Float for f32 {
414387
(mantissa as u64, exponent, sign)
415388
}
416389

390+
/// Round half-way cases toward `NEG_INFINITY`
391+
#[inline]
392+
fn floor(&self) -> f32 { unsafe{intrinsics::floorf32(*self)} }
393+
394+
/// Round half-way cases toward `INFINITY`
395+
#[inline]
396+
fn ceil(&self) -> f32 { unsafe{intrinsics::ceilf32(*self)} }
397+
398+
/// Round half-way cases away from `0.0`
399+
#[inline]
400+
fn round(&self) -> f32 { unsafe{intrinsics::roundf32(*self)} }
401+
402+
/// The integer part of the number (rounds towards `0.0`)
403+
#[inline]
404+
fn trunc(&self) -> f32 { unsafe{intrinsics::truncf32(*self)} }
405+
406+
/// The fractional part of the number, satisfying:
407+
///
408+
/// ```rust
409+
/// let x = 1.65f32;
410+
/// assert!(x == x.trunc() + x.fract())
411+
/// ```
412+
#[inline]
413+
fn fract(&self) -> f32 { *self - self.trunc() }
414+
417415
/// Archimedes' constant
418416
#[inline]
419417
fn pi() -> f32 { 3.14159265358979323846264338327950288 }

src/libstd/num/f64.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -247,33 +247,6 @@ impl Signed for f64 {
247247
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == NEG_INFINITY }
248248
}
249249

250-
impl Round for f64 {
251-
/// Round half-way cases toward `NEG_INFINITY`
252-
#[inline]
253-
fn floor(&self) -> f64 { unsafe{intrinsics::floorf64(*self)} }
254-
255-
/// Round half-way cases toward `INFINITY`
256-
#[inline]
257-
fn ceil(&self) -> f64 { unsafe{intrinsics::ceilf64(*self)} }
258-
259-
/// Round half-way cases away from `0.0`
260-
#[inline]
261-
fn round(&self) -> f64 { unsafe{intrinsics::roundf64(*self)} }
262-
263-
/// The integer part of the number (rounds towards `0.0`)
264-
#[inline]
265-
fn trunc(&self) -> f64 { unsafe{intrinsics::truncf64(*self)} }
266-
267-
/// The fractional part of the number, satisfying:
268-
///
269-
/// ```rust
270-
/// let x = 1.65f64;
271-
/// assert!(x == x.trunc() + x.fract())
272-
/// ```
273-
#[inline]
274-
fn fract(&self) -> f64 { *self - self.trunc() }
275-
}
276-
277250
impl Bounded for f64 {
278251
#[inline]
279252
fn min_value() -> f64 { 2.2250738585072014e-308 }
@@ -420,6 +393,31 @@ impl Float for f64 {
420393
(mantissa, exponent, sign)
421394
}
422395

396+
/// Round half-way cases toward `NEG_INFINITY`
397+
#[inline]
398+
fn floor(&self) -> f64 { unsafe{intrinsics::floorf64(*self)} }
399+
400+
/// Round half-way cases toward `INFINITY`
401+
#[inline]
402+
fn ceil(&self) -> f64 { unsafe{intrinsics::ceilf64(*self)} }
403+
404+
/// Round half-way cases away from `0.0`
405+
#[inline]
406+
fn round(&self) -> f64 { unsafe{intrinsics::roundf64(*self)} }
407+
408+
/// The integer part of the number (rounds towards `0.0`)
409+
#[inline]
410+
fn trunc(&self) -> f64 { unsafe{intrinsics::truncf64(*self)} }
411+
412+
/// The fractional part of the number, satisfying:
413+
///
414+
/// ```rust
415+
/// let x = 1.65f64;
416+
/// assert!(x == x.trunc() + x.fract())
417+
/// ```
418+
#[inline]
419+
fn fract(&self) -> f64 { *self - self.trunc() }
420+
423421
/// Archimedes' constant
424422
#[inline]
425423
fn pi() -> f64 { 3.14159265358979323846264338327950288 }

src/libstd/num/mod.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,6 @@ pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
162162
/// A trait for values which cannot be negative
163163
pub trait Unsigned: Num {}
164164

165-
/// A collection of rounding operations.
166-
pub trait Round {
167-
/// Return the largest integer less than or equal to a number.
168-
fn floor(&self) -> Self;
169-
170-
/// Return the smallest integer greater than or equal to a number.
171-
fn ceil(&self) -> Self;
172-
173-
/// Return the nearest integer to a number. Round half-way cases away from
174-
/// `0.0`.
175-
fn round(&self) -> Self;
176-
177-
/// Return the integer part of a number.
178-
fn trunc(&self) -> Self;
179-
180-
/// Return the fractional part of a number.
181-
fn fract(&self) -> Self;
182-
}
183-
184165
/// Raises a value to the power of exp, using exponentiation by squaring.
185166
///
186167
/// # Example
@@ -347,7 +328,7 @@ pub enum FPCategory {
347328
//
348329
// FIXME(#8888): Several of these functions have a parameter named
349330
// `unused_self`. Removing it requires #8888 to be fixed.
350-
pub trait Float: Signed + Round + Primitive {
331+
pub trait Float: Signed + Primitive {
351332
/// Returns the maximum of the two numbers.
352333
fn max(self, other: Self) -> Self;
353334
/// Returns the minimum of the two numbers.
@@ -431,6 +412,22 @@ pub trait Float: Signed + Round + Primitive {
431412
/// Returns the mantissa, exponent and sign as integers, respectively.
432413
fn integer_decode(&self) -> (u64, i16, i8);
433414

415+
/// Return the largest integer less than or equal to a number.
416+
fn floor(&self) -> Self;
417+
418+
/// Return the smallest integer greater than or equal to a number.
419+
fn ceil(&self) -> Self;
420+
421+
/// Return the nearest integer to a number. Round half-way cases away from
422+
/// `0.0`.
423+
fn round(&self) -> Self;
424+
425+
/// Return the integer part of a number.
426+
fn trunc(&self) -> Self;
427+
428+
/// Return the fractional part of a number.
429+
fn fract(&self) -> Self;
430+
434431
/// Archimedes' constant.
435432
fn pi() -> Self;
436433

src/libstd/num/strconv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use clone::Clone;
1515
use container::Container;
1616
use iter::Iterator;
1717
use num::{NumCast, Zero, One, cast, Int};
18-
use num::{Round, Float, FPNaN, FPInfinite, ToPrimitive};
18+
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
1919
use num;
2020
use ops::{Add, Sub, Mul, Div, Rem, Neg};
2121
use option::{None, Option, Some};
@@ -258,7 +258,7 @@ pub fn int_to_str_bytes_common<T: Int>(num: T, radix: uint, sign: SignFormat, f:
258258
* - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict
259259
* between digit and exponent sign `'p'`.
260260
*/
261-
pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
261+
pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+
262262
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
263263
num: T, radix: uint, negative_zero: bool,
264264
sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_upper: bool
@@ -491,7 +491,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+Round+
491491
* `to_str_bytes_common()`, for details see there.
492492
*/
493493
#[inline]
494-
pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+
494+
pub fn float_to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+
495495
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
496496
num: T, radix: uint, negative_zero: bool,
497497
sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_capital: bool

src/libstd/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use iter::{FromIterator, Extendable};
4545
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
4646
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
4747
pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
48-
pub use num::{Signed, Unsigned, Round};
48+
pub use num::{Signed, Unsigned};
4949
pub use num::{Primitive, Int, Float, ToPrimitive, FromPrimitive};
5050
pub use path::{GenericPath, Path, PosixPath, WindowsPath};
5151
pub use ptr::RawPtr;

0 commit comments

Comments
 (0)