Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added stdevp method and alias

  • Loading branch information...
commit f58038b4d4f689eded7647f097544e79327bab5e 1 parent 103b7ab
@lucianot lucianot authored
Showing with 22 additions and 0 deletions.
  1. +22 −0 ext/standard_deviation/standard_deviation.c
View
22 ext/standard_deviation/standard_deviation.c
@@ -22,7 +22,29 @@ static VALUE stdev(VALUE self) {
return rb_float_new(sqrt(variance / (size - 1)));
}
+static VALUE stdevp(VALUE self) {
+ int i, size;
+ double total, mean, variance;
+
+ size = RARRAY_LEN(self);
+ total = variance = 0;
+ VALUE *array = RARRAY_PTR(self);
+
+ for (i = 0; i < size; i++) {
+ total += NUM2DBL(array[i]);
+ }
+
+ mean = total / size;
+
+ for (i = 0; i < size; i++) {
+ variance += pow((NUM2DBL(array[i]) - mean), 2);
+ }
+
+ return rb_float_new(sqrt(variance / size));
+}
+
void Init_standard_deviation() {
rb_define_method(rb_cArray, "stdev", stdev, 0);
rb_define_alias(rb_cArray, "standard_deviation", "stdev");
+ rb_define_method(rb_cArray, "stdevp", stdev, 0);
}
Please sign in to comment.
Something went wrong with that request. Please try again.