From 1c7dcd09a361e2854f75ec8b7de844487a81258f Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 9 Nov 2014 16:59:28 +1100 Subject: [PATCH 1/5] Deprecate Signed method wrappers --- stats.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/stats.rs b/stats.rs index 8c184cc..ff274c8 100644 --- a/stats.rs +++ b/stats.rs @@ -17,7 +17,6 @@ use std::hash::Hash; use std::io; use std::mem; use std::num::Zero; -use std::num; fn local_cmp(x: T, y: T) -> Ordering { // arbitrarily decide that NaNs are larger than everything. @@ -166,7 +165,6 @@ impl Summary { } impl<'a, T: FloatMath + FromPrimitive> Stats for &'a [T] { - // FIXME #11059 handle NaN, inf and overflow fn sum(self) -> T { let mut partials = vec![]; @@ -176,8 +174,8 @@ impl<'a, T: FloatMath + FromPrimitive> Stats for &'a [T] { // This inner loop applies `hi`/`lo` summation to each // partial so that the list of partial sums remains exact. for i in range(0, partials.len()) { - let mut y = partials[i]; - if num::abs(x) < num::abs(y) { + let mut y: T = partials[i]; + if x.abs() < y.abs() { mem::swap(&mut x, &mut y); } // Rounded `x+y` is stored in `hi` with round-off stored in @@ -249,7 +247,7 @@ impl<'a, T: FloatMath + FromPrimitive> Stats for &'a [T] { fn median_abs_dev(self) -> T { let med = self.median(); - let abs_devs: Vec = self.iter().map(|&v| num::abs(med - v)).collect(); + let abs_devs: Vec = self.iter().map(|&v| (med - v).abs()).collect(); // This constant is derived by smarter statistics brains than me, but it is // consistent with how R and other packages treat the MAD. let number = FromPrimitive::from_f64(1.4826).unwrap(); From 1109ca84a3d84e92aa0a0199f40dd777c497eb8e Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 9 Nov 2014 17:46:33 +1100 Subject: [PATCH 2/5] Move saturating operator methods into Int --- lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lib.rs b/lib.rs index 4d6aefb..ee67864 100644 --- a/lib.rs +++ b/lib.rs @@ -106,7 +106,6 @@ enum NamePadding { PadNone, PadOnLeft, PadOnRight } impl TestDesc { fn padded_name(&self, column_count: uint, align: NamePadding) -> String { - use std::num::Saturating; let mut name = String::from_str(self.name.as_slice()); let fill = column_count.saturating_sub(name.len()); let mut pad = " ".repeat(fill); From 1dd6b641c5cd02bf201f87b8b78f76cade915374 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 10 Nov 2014 09:35:53 +1100 Subject: [PATCH 3/5] Deprecate Zero and One traits --- stats.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/stats.rs b/stats.rs index ff274c8..6be5bca 100644 --- a/stats.rs +++ b/stats.rs @@ -16,7 +16,6 @@ use std::fmt::Show; use std::hash::Hash; use std::io; use std::mem; -use std::num::Zero; fn local_cmp(x: T, y: T) -> Ordering { // arbitrarily decide that NaNs are larger than everything. @@ -144,7 +143,6 @@ pub struct Summary { } impl Summary { - /// Construct a new summary of a sample set. pub fn new(samples: &[T]) -> Summary { Summary { @@ -182,7 +180,7 @@ impl<'a, T: FloatMath + FromPrimitive> Stats for &'a [T] { // `lo`. Together `hi+lo` are exactly equal to `x+y`. let hi = x + y; let lo = y - (hi - x); - if !lo.is_zero() { + if lo != Float::zero() { partials[j] = lo; j += 1; } @@ -195,7 +193,7 @@ impl<'a, T: FloatMath + FromPrimitive> Stats for &'a [T] { partials.truncate(j+1); } } - let zero: T = Zero::zero(); + let zero: T = Float::zero(); partials.iter().fold(zero, |p, q| p + *q) } @@ -220,10 +218,10 @@ impl<'a, T: FloatMath + FromPrimitive> Stats for &'a [T] { fn var(self) -> T { if self.len() < 2 { - Zero::zero() + Float::zero() } else { let mean = self.mean(); - let mut v: T = Zero::zero(); + let mut v: T = Float::zero(); for s in self.iter() { let x = *s - mean; v = v + x*x; @@ -292,7 +290,7 @@ fn percentile_of_sorted(sorted_samples: &[T], if sorted_samples.len() == 1 { return sorted_samples[0]; } - let zero: T = Zero::zero(); + let zero: T = Float::zero(); assert!(zero <= pct); let hundred = FromPrimitive::from_uint(100).unwrap(); assert!(pct <= hundred); @@ -368,14 +366,14 @@ pub fn write_boxplot( let himag = ten.powf(s.max.abs().log10().floor()); // need to consider when the limit is zero - let zero: T = Zero::zero(); - let lo = if lomag.is_zero() { + let zero: T = Float::zero(); + let lo = if lomag == Float::zero() { zero } else { (s.min / lomag).floor() * lomag }; - let hi = if himag.is_zero() { + let hi = if himag == Float::zero() { zero } else { (s.max / himag).ceil() * himag @@ -1044,11 +1042,11 @@ mod tests { } #[test] fn test_sum_f64s() { - assert_eq!([0.5f64, 3.2321f64, 1.5678f64].sum(), 5.2999); + assert_eq!([0.5f64, 3.2321f64, 1.5678f64].sum(0.0), 5.2999); } #[test] fn test_sum_f64_between_ints_that_sum_to_0() { - assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(), 1.2); + assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(0.0), 1.2); } } @@ -1060,7 +1058,7 @@ mod bench { #[bench] pub fn sum_three_items(b: &mut Bencher) { b.iter(|| { - [1e20f64, 1.5f64, -1e20f64].sum(); + [1e20f64, 1.5f64, -1e20f64].sum(0.0); }) } #[bench] @@ -1069,7 +1067,7 @@ mod bench { let v = Vec::from_fn(500, |i| nums[i%5]); b.iter(|| { - v.as_slice().sum(); + v.as_slice().sum(0.0); }) } } From 93bfb8e206a6b59357b1d72bb4f6b87aab4d5ef3 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 10 Nov 2014 16:26:10 +1100 Subject: [PATCH 4/5] Remove lots of numeric traits from the preludes Num, NumCast, Unsigned, Float, Primitive and Int have been removed. --- lib.rs | 1 + stats.rs | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib.rs b/lib.rs index ee67864..a25aff3 100644 --- a/lib.rs +++ b/lib.rs @@ -60,6 +60,7 @@ use std::io::fs::PathExtensions; use std::io::stdio::StdWriter; use std::io::{File, ChanReader, ChanWriter}; use std::io; +use std::num::{Int, FloatMath}; use std::os; use std::string::String; use std::task::TaskBuilder; diff --git a/stats.rs b/stats.rs index 6be5bca..5cec507 100644 --- a/stats.rs +++ b/stats.rs @@ -16,6 +16,7 @@ use std::fmt::Show; use std::hash::Hash; use std::io; use std::mem; +use std::num::{Float, FloatMath}; fn local_cmp(x: T, y: T) -> Ordering { // arbitrarily decide that NaNs are larger than everything. @@ -1042,11 +1043,11 @@ mod tests { } #[test] fn test_sum_f64s() { - assert_eq!([0.5f64, 3.2321f64, 1.5678f64].sum(0.0), 5.2999); + assert_eq!([0.5f64, 3.2321f64, 1.5678f64].sum(), 5.2999); } #[test] fn test_sum_f64_between_ints_that_sum_to_0() { - assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(0.0), 1.2); + assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(), 1.2); } } @@ -1058,7 +1059,7 @@ mod bench { #[bench] pub fn sum_three_items(b: &mut Bencher) { b.iter(|| { - [1e20f64, 1.5f64, -1e20f64].sum(0.0); + [1e20f64, 1.5f64, -1e20f64].sum(); }) } #[bench] @@ -1067,7 +1068,7 @@ mod bench { let v = Vec::from_fn(500, |i| nums[i%5]); b.iter(|| { - v.as_slice().sum(0.0); + v.as_slice().sum(); }) } } From bddde2b44211f76504c95f90726df16437783443 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 13 Nov 2014 00:02:42 +1100 Subject: [PATCH 5/5] Remove Signed trait and add SignedInt trait The methods have been moved into Float and SignedInt --- lib.rs | 2 +- stats.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib.rs b/lib.rs index a25aff3..1178848 100644 --- a/lib.rs +++ b/lib.rs @@ -60,7 +60,7 @@ use std::io::fs::PathExtensions; use std::io::stdio::StdWriter; use std::io::{File, ChanReader, ChanWriter}; use std::io; -use std::num::{Int, FloatMath}; +use std::num::{Float, FloatMath, Int}; use std::os; use std::string::String; use std::task::TaskBuilder; diff --git a/stats.rs b/stats.rs index 5cec507..adf58dc 100644 --- a/stats.rs +++ b/stats.rs @@ -461,6 +461,7 @@ mod tests { macro_rules! assert_approx_eq( ($a:expr, $b:expr) => ({ + use std::num::Float; let (a, b) = (&$a, &$b); assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b);