Project

General

Profile

Feature #2322 ยป add_stdev.diff

Patch to add standard deviation feature - danielc192 (Daniel Cohen), 11/02/2009 05:29 AM

View differences:

math.c 2009-11-01 15:23:53.000000000 -0500
}
}
/*
* call-seq:
* Math.stdev(array) => double
*
* Computes the standard deviation of the elements in an <i>array</i>
* If they are all numbers.
*
*
*/
static VALUE
math_stdev(VALUE obj, VALUE ary)
{
long i;
double sum = 0.0;
double mean = 0.0;
double numer = 0.0;
for (i=0; i<RARRAY_LEN(ary); i++) {
if( TYPE(RARRAY_PTR(ary)[i]) == T_STRING) {
rb_raise(rb_eTypeError, "Element is not a number!");
} else {
sum = sum + NUM2DBL(RARRAY_PTR(ary)[i]);
}
}
mean = sum / RARRAY_LEN(ary);
for (i=0; i<RARRAY_LEN(ary); i++) {
if( TYPE(RARRAY_PTR(ary)[i]) == T_STRING) {
rb_raise(rb_eTypeError, "Element is not a number!");
} else {
numer = numer + pow((NUM2DBL(RARRAY_PTR(ary)[i]) - mean), 2);
}
}
return DBL2NUM(sqrt(numer / RARRAY_LEN(ary)));
}
/*
* call-seq:
    (1-1/1)