Skip to content

Commit 48c2418

Browse files
committed
Restore Round trait and move appropriate methods out of Real
1 parent dcd49cc commit 48c2418

File tree

6 files changed

+307
-67
lines changed

6 files changed

+307
-67
lines changed

src/libcore/core.rc

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub use iter::{ExtendedMutableIter};
105105

106106
pub use num::{Num, NumCast};
107107
pub use num::{Signed, Unsigned, Integer};
108-
pub use num::{Fractional, Real, RealExt};
108+
pub use num::{Round, Fractional, Real, RealExt};
109109
pub use ptr::Ptr;
110110
pub use to_str::ToStr;
111111
pub use clone::Clone;

src/libcore/num/f32.rs

+98-16
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ impl Signed for f32 {
327327
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
328328
}
329329

330+
impl Round for f32 {
331+
/// Round half-way cases toward `neg_infinity`
332+
#[inline(always)]
333+
fn floor(&self) -> f32 { floor(*self) }
334+
335+
/// Round half-way cases toward `infinity`
336+
#[inline(always)]
337+
fn ceil(&self) -> f32 { ceil(*self) }
338+
339+
/// Round half-way cases away from `0.0`
340+
#[inline(always)]
341+
fn round(&self) -> f32 { round(*self) }
342+
343+
/// The integer part of the number (rounds towards `0.0`)
344+
#[inline(always)]
345+
fn trunc(&self) -> f32 { trunc(*self) }
346+
347+
///
348+
/// The fractional part of the number, satisfying:
349+
///
350+
/// ~~~
351+
/// assert!(x == trunc(x) + fract(x))
352+
/// ~~~
353+
///
354+
#[inline(always)]
355+
fn fract(&self) -> f32 { *self - self.trunc() }
356+
}
357+
330358
impl Fractional for f32 {
331359
/// The reciprocal (multiplicative inverse) of the number
332360
#[inline(always)]
@@ -402,22 +430,6 @@ impl Real for f32 {
402430
#[inline(always)]
403431
fn log_10() -> f32 { 2.30258509299404568401799145468436421 }
404432

405-
#[inline(always)]
406-
fn floor(&self) -> f32 { floor(*self) }
407-
408-
#[inline(always)]
409-
fn ceil(&self) -> f32 { ceil(*self) }
410-
411-
#[inline(always)]
412-
fn round(&self) -> f32 { round(*self) }
413-
414-
#[inline(always)]
415-
fn trunc(&self) -> f32 { trunc(*self) }
416-
417-
/// The fractional part of the number, calculated using: `n - floor(n)`
418-
#[inline(always)]
419-
fn fract(&self) -> f32 { *self - self.floor() }
420-
421433
#[inline(always)]
422434
fn pow(&self, n: f32) -> f32 { pow(*self, n) }
423435

@@ -736,6 +748,76 @@ mod tests {
736748
num::test_num(10f32, 2f32);
737749
}
738750

751+
#[test]
752+
fn test_floor() {
753+
assert_fuzzy_eq!(1.0f32.floor(), 1.0f32);
754+
assert_fuzzy_eq!(1.3f32.floor(), 1.0f32);
755+
assert_fuzzy_eq!(1.5f32.floor(), 1.0f32);
756+
assert_fuzzy_eq!(1.7f32.floor(), 1.0f32);
757+
assert_fuzzy_eq!(0.0f32.floor(), 0.0f32);
758+
assert_fuzzy_eq!((-0.0f32).floor(), -0.0f32);
759+
assert_fuzzy_eq!((-1.0f32).floor(), -1.0f32);
760+
assert_fuzzy_eq!((-1.3f32).floor(), -2.0f32);
761+
assert_fuzzy_eq!((-1.5f32).floor(), -2.0f32);
762+
assert_fuzzy_eq!((-1.7f32).floor(), -2.0f32);
763+
}
764+
765+
#[test]
766+
fn test_ceil() {
767+
assert_fuzzy_eq!(1.0f32.ceil(), 1.0f32);
768+
assert_fuzzy_eq!(1.3f32.ceil(), 2.0f32);
769+
assert_fuzzy_eq!(1.5f32.ceil(), 2.0f32);
770+
assert_fuzzy_eq!(1.7f32.ceil(), 2.0f32);
771+
assert_fuzzy_eq!(0.0f32.ceil(), 0.0f32);
772+
assert_fuzzy_eq!((-0.0f32).ceil(), -0.0f32);
773+
assert_fuzzy_eq!((-1.0f32).ceil(), -1.0f32);
774+
assert_fuzzy_eq!((-1.3f32).ceil(), -1.0f32);
775+
assert_fuzzy_eq!((-1.5f32).ceil(), -1.0f32);
776+
assert_fuzzy_eq!((-1.7f32).ceil(), -1.0f32);
777+
}
778+
779+
#[test]
780+
fn test_round() {
781+
assert_fuzzy_eq!(1.0f32.round(), 1.0f32);
782+
assert_fuzzy_eq!(1.3f32.round(), 1.0f32);
783+
assert_fuzzy_eq!(1.5f32.round(), 2.0f32);
784+
assert_fuzzy_eq!(1.7f32.round(), 2.0f32);
785+
assert_fuzzy_eq!(0.0f32.round(), 0.0f32);
786+
assert_fuzzy_eq!((-0.0f32).round(), -0.0f32);
787+
assert_fuzzy_eq!((-1.0f32).round(), -1.0f32);
788+
assert_fuzzy_eq!((-1.3f32).round(), -1.0f32);
789+
assert_fuzzy_eq!((-1.5f32).round(), -2.0f32);
790+
assert_fuzzy_eq!((-1.7f32).round(), -2.0f32);
791+
}
792+
793+
#[test]
794+
fn test_trunc() {
795+
assert_fuzzy_eq!(1.0f32.trunc(), 1.0f32);
796+
assert_fuzzy_eq!(1.3f32.trunc(), 1.0f32);
797+
assert_fuzzy_eq!(1.5f32.trunc(), 1.0f32);
798+
assert_fuzzy_eq!(1.7f32.trunc(), 1.0f32);
799+
assert_fuzzy_eq!(0.0f32.trunc(), 0.0f32);
800+
assert_fuzzy_eq!((-0.0f32).trunc(), -0.0f32);
801+
assert_fuzzy_eq!((-1.0f32).trunc(), -1.0f32);
802+
assert_fuzzy_eq!((-1.3f32).trunc(), -1.0f32);
803+
assert_fuzzy_eq!((-1.5f32).trunc(), -1.0f32);
804+
assert_fuzzy_eq!((-1.7f32).trunc(), -1.0f32);
805+
}
806+
807+
#[test]
808+
fn test_fract() {
809+
assert_fuzzy_eq!(1.0f32.fract(), 0.0f32);
810+
assert_fuzzy_eq!(1.3f32.fract(), 0.3f32);
811+
assert_fuzzy_eq!(1.5f32.fract(), 0.5f32);
812+
assert_fuzzy_eq!(1.7f32.fract(), 0.7f32);
813+
assert_fuzzy_eq!(0.0f32.fract(), 0.0f32);
814+
assert_fuzzy_eq!((-0.0f32).fract(), -0.0f32);
815+
assert_fuzzy_eq!((-1.0f32).fract(), -0.0f32);
816+
assert_fuzzy_eq!((-1.3f32).fract(), -0.3f32);
817+
assert_fuzzy_eq!((-1.5f32).fract(), -0.5f32);
818+
assert_fuzzy_eq!((-1.7f32).fract(), -0.7f32);
819+
}
820+
739821
#[test]
740822
fn test_real_consts() {
741823
assert_fuzzy_eq!(Real::two_pi::<f32>(), 2f32 * Real::pi::<f32>());

src/libcore/num/f64.rs

+100-17
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,34 @@ impl Signed for f64 {
337337
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
338338
}
339339

340+
impl Round for f64 {
341+
/// Round half-way cases toward `neg_infinity`
342+
#[inline(always)]
343+
fn floor(&self) -> f64 { floor(*self) }
344+
345+
/// Round half-way cases toward `infinity`
346+
#[inline(always)]
347+
fn ceil(&self) -> f64 { ceil(*self) }
348+
349+
/// Round half-way cases away from `0.0`
350+
#[inline(always)]
351+
fn round(&self) -> f64 { round(*self) }
352+
353+
/// The integer part of the number (rounds towards `0.0`)
354+
#[inline(always)]
355+
fn trunc(&self) -> f64 { trunc(*self) }
356+
357+
///
358+
/// The fractional part of the number, satisfying:
359+
///
360+
/// ~~~
361+
/// assert!(x == trunc(x) + fract(x))
362+
/// ~~~
363+
///
364+
#[inline(always)]
365+
fn fract(&self) -> f64 { *self - self.trunc() }
366+
}
367+
340368
impl Fractional for f64 {
341369
/// The reciprocal (multiplicative inverse) of the number
342370
#[inline(always)]
@@ -412,22 +440,6 @@ impl Real for f64 {
412440
#[inline(always)]
413441
fn log_10() -> f64 { 2.30258509299404568401799145468436421 }
414442

415-
#[inline(always)]
416-
fn floor(&self) -> f64 { floor(*self) }
417-
418-
#[inline(always)]
419-
fn ceil(&self) -> f64 { ceil(*self) }
420-
421-
#[inline(always)]
422-
fn round(&self) -> f64 { round(*self) }
423-
424-
#[inline(always)]
425-
fn trunc(&self) -> f64 { trunc(*self) }
426-
427-
/// The fractional part of the number, calculated using: `n - floor(n)`
428-
#[inline(always)]
429-
fn fract(&self) -> f64 { *self - self.floor() }
430-
431443
#[inline(always)]
432444
fn pow(&self, n: f64) -> f64 { pow(*self, n) }
433445

@@ -766,7 +778,8 @@ mod tests {
766778
($a:expr, $b:expr) => ({
767779
let a = $a, b = $b;
768780
if !((a - b).abs() < 1.0e-6) {
769-
fail!(fmt!("The values were not approximately equal. Found: %? and %?", a, b));
781+
fail!(fmt!("The values were not approximately equal. \
782+
Found: %? and expected %?", a, b));
770783
}
771784
})
772785
)
@@ -776,6 +789,76 @@ mod tests {
776789
num::test_num(10f64, 2f64);
777790
}
778791

792+
#[test]
793+
fn test_floor() {
794+
assert_fuzzy_eq!(1.0f64.floor(), 1.0f64);
795+
assert_fuzzy_eq!(1.3f64.floor(), 1.0f64);
796+
assert_fuzzy_eq!(1.5f64.floor(), 1.0f64);
797+
assert_fuzzy_eq!(1.7f64.floor(), 1.0f64);
798+
assert_fuzzy_eq!(0.0f64.floor(), 0.0f64);
799+
assert_fuzzy_eq!((-0.0f64).floor(), -0.0f64);
800+
assert_fuzzy_eq!((-1.0f64).floor(), -1.0f64);
801+
assert_fuzzy_eq!((-1.3f64).floor(), -2.0f64);
802+
assert_fuzzy_eq!((-1.5f64).floor(), -2.0f64);
803+
assert_fuzzy_eq!((-1.7f64).floor(), -2.0f64);
804+
}
805+
806+
#[test]
807+
fn test_ceil() {
808+
assert_fuzzy_eq!(1.0f64.ceil(), 1.0f64);
809+
assert_fuzzy_eq!(1.3f64.ceil(), 2.0f64);
810+
assert_fuzzy_eq!(1.5f64.ceil(), 2.0f64);
811+
assert_fuzzy_eq!(1.7f64.ceil(), 2.0f64);
812+
assert_fuzzy_eq!(0.0f64.ceil(), 0.0f64);
813+
assert_fuzzy_eq!((-0.0f64).ceil(), -0.0f64);
814+
assert_fuzzy_eq!((-1.0f64).ceil(), -1.0f64);
815+
assert_fuzzy_eq!((-1.3f64).ceil(), -1.0f64);
816+
assert_fuzzy_eq!((-1.5f64).ceil(), -1.0f64);
817+
assert_fuzzy_eq!((-1.7f64).ceil(), -1.0f64);
818+
}
819+
820+
#[test]
821+
fn test_round() {
822+
assert_fuzzy_eq!(1.0f64.round(), 1.0f64);
823+
assert_fuzzy_eq!(1.3f64.round(), 1.0f64);
824+
assert_fuzzy_eq!(1.5f64.round(), 2.0f64);
825+
assert_fuzzy_eq!(1.7f64.round(), 2.0f64);
826+
assert_fuzzy_eq!(0.0f64.round(), 0.0f64);
827+
assert_fuzzy_eq!((-0.0f64).round(), -0.0f64);
828+
assert_fuzzy_eq!((-1.0f64).round(), -1.0f64);
829+
assert_fuzzy_eq!((-1.3f64).round(), -1.0f64);
830+
assert_fuzzy_eq!((-1.5f64).round(), -2.0f64);
831+
assert_fuzzy_eq!((-1.7f64).round(), -2.0f64);
832+
}
833+
834+
#[test]
835+
fn test_trunc() {
836+
assert_fuzzy_eq!(1.0f64.trunc(), 1.0f64);
837+
assert_fuzzy_eq!(1.3f64.trunc(), 1.0f64);
838+
assert_fuzzy_eq!(1.5f64.trunc(), 1.0f64);
839+
assert_fuzzy_eq!(1.7f64.trunc(), 1.0f64);
840+
assert_fuzzy_eq!(0.0f64.trunc(), 0.0f64);
841+
assert_fuzzy_eq!((-0.0f64).trunc(), -0.0f64);
842+
assert_fuzzy_eq!((-1.0f64).trunc(), -1.0f64);
843+
assert_fuzzy_eq!((-1.3f64).trunc(), -1.0f64);
844+
assert_fuzzy_eq!((-1.5f64).trunc(), -1.0f64);
845+
assert_fuzzy_eq!((-1.7f64).trunc(), -1.0f64);
846+
}
847+
848+
#[test]
849+
fn test_fract() {
850+
assert_fuzzy_eq!(1.0f64.fract(), 0.0f64);
851+
assert_fuzzy_eq!(1.3f64.fract(), 0.3f64);
852+
assert_fuzzy_eq!(1.5f64.fract(), 0.5f64);
853+
assert_fuzzy_eq!(1.7f64.fract(), 0.7f64);
854+
assert_fuzzy_eq!(0.0f64.fract(), 0.0f64);
855+
assert_fuzzy_eq!((-0.0f64).fract(), -0.0f64);
856+
assert_fuzzy_eq!((-1.0f64).fract(), -0.0f64);
857+
assert_fuzzy_eq!((-1.3f64).fract(), -0.3f64);
858+
assert_fuzzy_eq!((-1.5f64).fract(), -0.5f64);
859+
assert_fuzzy_eq!((-1.7f64).fract(), -0.7f64);
860+
}
861+
779862
#[test]
780863
fn test_real_consts() {
781864
assert_fuzzy_eq!(Real::two_pi::<f64>(), 2.0 * Real::pi::<f64>());

0 commit comments

Comments
 (0)