Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 48c2418

Browse files
committedApr 25, 2013
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 98 additions & 16 deletions
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

Lines changed: 100 additions & 17 deletions
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>());

‎src/libcore/num/float.rs

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,34 @@ impl num::One for float {
403403
fn one() -> float { 1.0 }
404404
}
405405
406+
impl Round for float {
407+
/// Round half-way cases toward `neg_infinity`
408+
#[inline(always)]
409+
fn floor(&self) -> float { floor(*self as f64) as float }
410+
411+
/// Round half-way cases toward `infinity`
412+
#[inline(always)]
413+
fn ceil(&self) -> float { ceil(*self as f64) as float }
414+
415+
/// Round half-way cases away from `0.0`
416+
#[inline(always)]
417+
fn round(&self) -> float { round(*self as f64) as float }
418+
419+
/// The integer part of the number (rounds towards `0.0`)
420+
#[inline(always)]
421+
fn trunc(&self) -> float { trunc(*self as f64) as float }
422+
423+
///
424+
/// The fractional part of the number, satisfying:
425+
///
426+
/// ~~~
427+
/// assert!(x == trunc(x) + fract(x))
428+
/// ~~~
429+
///
430+
#[inline(always)]
431+
fn fract(&self) -> float { *self - self.trunc() }
432+
}
433+
406434
impl Fractional for float {
407435
/// The reciprocal (multiplicative inverse) of the number
408436
#[inline(always)]
@@ -478,22 +506,6 @@ impl Real for float {
478506
#[inline(always)]
479507
fn log_10() -> float { 2.30258509299404568401799145468436421 }
480508
481-
#[inline(always)]
482-
fn floor(&self) -> float { floor(*self as f64) as float }
483-
484-
#[inline(always)]
485-
fn ceil(&self) -> float { ceil(*self as f64) as float }
486-
487-
#[inline(always)]
488-
fn round(&self) -> float { round(*self as f64) as float }
489-
490-
#[inline(always)]
491-
fn trunc(&self) -> float { trunc(*self as f64) as float }
492-
493-
/// The fractional part of the number, calculated using: `n - floor(n)`
494-
#[inline(always)]
495-
fn fract(&self) -> float { *self - self.floor() }
496-
497509
#[inline(always)]
498510
fn pow(&self, n: float) -> float { pow(*self as f64, n as f64) as float }
499511
@@ -694,6 +706,76 @@ mod tests {
694706
num::test_num(10f, 2f);
695707
}
696708
709+
#[test]
710+
fn test_floor() {
711+
assert_fuzzy_eq!(1.0f.floor(), 1.0f);
712+
assert_fuzzy_eq!(1.3f.floor(), 1.0f);
713+
assert_fuzzy_eq!(1.5f.floor(), 1.0f);
714+
assert_fuzzy_eq!(1.7f.floor(), 1.0f);
715+
assert_fuzzy_eq!(0.0f.floor(), 0.0f);
716+
assert_fuzzy_eq!((-0.0f).floor(), -0.0f);
717+
assert_fuzzy_eq!((-1.0f).floor(), -1.0f);
718+
assert_fuzzy_eq!((-1.3f).floor(), -2.0f);
719+
assert_fuzzy_eq!((-1.5f).floor(), -2.0f);
720+
assert_fuzzy_eq!((-1.7f).floor(), -2.0f);
721+
}
722+
723+
#[test]
724+
fn test_ceil() {
725+
assert_fuzzy_eq!(1.0f.ceil(), 1.0f);
726+
assert_fuzzy_eq!(1.3f.ceil(), 2.0f);
727+
assert_fuzzy_eq!(1.5f.ceil(), 2.0f);
728+
assert_fuzzy_eq!(1.7f.ceil(), 2.0f);
729+
assert_fuzzy_eq!(0.0f.ceil(), 0.0f);
730+
assert_fuzzy_eq!((-0.0f).ceil(), -0.0f);
731+
assert_fuzzy_eq!((-1.0f).ceil(), -1.0f);
732+
assert_fuzzy_eq!((-1.3f).ceil(), -1.0f);
733+
assert_fuzzy_eq!((-1.5f).ceil(), -1.0f);
734+
assert_fuzzy_eq!((-1.7f).ceil(), -1.0f);
735+
}
736+
737+
#[test]
738+
fn test_round() {
739+
assert_fuzzy_eq!(1.0f.round(), 1.0f);
740+
assert_fuzzy_eq!(1.3f.round(), 1.0f);
741+
assert_fuzzy_eq!(1.5f.round(), 2.0f);
742+
assert_fuzzy_eq!(1.7f.round(), 2.0f);
743+
assert_fuzzy_eq!(0.0f.round(), 0.0f);
744+
assert_fuzzy_eq!((-0.0f).round(), -0.0f);
745+
assert_fuzzy_eq!((-1.0f).round(), -1.0f);
746+
assert_fuzzy_eq!((-1.3f).round(), -1.0f);
747+
assert_fuzzy_eq!((-1.5f).round(), -2.0f);
748+
assert_fuzzy_eq!((-1.7f).round(), -2.0f);
749+
}
750+
751+
#[test]
752+
fn test_trunc() {
753+
assert_fuzzy_eq!(1.0f.trunc(), 1.0f);
754+
assert_fuzzy_eq!(1.3f.trunc(), 1.0f);
755+
assert_fuzzy_eq!(1.5f.trunc(), 1.0f);
756+
assert_fuzzy_eq!(1.7f.trunc(), 1.0f);
757+
assert_fuzzy_eq!(0.0f.trunc(), 0.0f);
758+
assert_fuzzy_eq!((-0.0f).trunc(), -0.0f);
759+
assert_fuzzy_eq!((-1.0f).trunc(), -1.0f);
760+
assert_fuzzy_eq!((-1.3f).trunc(), -1.0f);
761+
assert_fuzzy_eq!((-1.5f).trunc(), -1.0f);
762+
assert_fuzzy_eq!((-1.7f).trunc(), -1.0f);
763+
}
764+
765+
#[test]
766+
fn test_fract() {
767+
assert_fuzzy_eq!(1.0f.fract(), 0.0f);
768+
assert_fuzzy_eq!(1.3f.fract(), 0.3f);
769+
assert_fuzzy_eq!(1.5f.fract(), 0.5f);
770+
assert_fuzzy_eq!(1.7f.fract(), 0.7f);
771+
assert_fuzzy_eq!(0.0f.fract(), 0.0f);
772+
assert_fuzzy_eq!((-0.0f).fract(), -0.0f);
773+
assert_fuzzy_eq!((-1.0f).fract(), -0.0f);
774+
assert_fuzzy_eq!((-1.3f).fract(), -0.3f);
775+
assert_fuzzy_eq!((-1.5f).fract(), -0.5f);
776+
assert_fuzzy_eq!((-1.7f).fract(), -0.7f);
777+
}
778+
697779
#[test]
698780
fn test_real_consts() {
699781
assert_fuzzy_eq!(Real::two_pi::<float>(), 2f * Real::pi::<float>());
@@ -893,15 +975,6 @@ mod tests {
893975
assert_eq!(to_str_digits(infinity, 10u), ~"inf");
894976
assert_eq!(to_str_digits(-infinity, 10u), ~"-inf");
895977
}
896-
897-
#[test]
898-
pub fn test_round() {
899-
assert_eq!(round(5.8), 6.0);
900-
assert_eq!(round(5.2), 5.0);
901-
assert_eq!(round(3.0), 3.0);
902-
assert_eq!(round(2.5), 3.0);
903-
assert_eq!(round(-3.5), -4.0);
904-
}
905978
}
906979

907980
//

‎src/libcore/num/num.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,17 @@ pub trait Integer: Num
7777
fn is_odd(&self) -> bool;
7878
}
7979

80+
pub trait Round {
81+
fn floor(&self) -> Self;
82+
fn ceil(&self) -> Self;
83+
fn round(&self) -> Self;
84+
fn trunc(&self) -> Self;
85+
fn fract(&self) -> Self;
86+
}
87+
8088
pub trait Fractional: Num
8189
+ Ord
90+
+ Round
8291
+ Quot<Self,Self> {
8392
fn recip(&self) -> Self;
8493
}
@@ -108,13 +117,6 @@ pub trait Real: Signed
108117
fn log_2() -> Self;
109118
fn log_10() -> Self;
110119

111-
// Rounding operations
112-
fn floor(&self) -> Self;
113-
fn ceil(&self) -> Self;
114-
fn round(&self) -> Self;
115-
fn trunc(&self) -> Self;
116-
fn fract(&self) -> Self;
117-
118120
// Exponential functions
119121
fn pow(&self, n: Self) -> Self;
120122
fn exp(&self) -> Self;

‎src/libcore/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use iter::{CopyableIter, CopyableOrderedIter, CopyableNonstrictIter};
3939
pub use iter::{Times, ExtendedMutableIter};
4040
pub use num::{Num, NumCast};
4141
pub use num::{Signed, Unsigned, Integer};
42-
pub use num::{Fractional, Real, RealExt};
42+
pub use num::{Round, Fractional, Real, RealExt};
4343
pub use path::GenericPath;
4444
pub use path::Path;
4545
pub use path::PosixPath;

0 commit comments

Comments
 (0)
Please sign in to comment.