Sha256: ddb37b38bf94ce5267060c8e708abbea6d2b1b872199b48471135a1aff17bed6

Contents?: true

Size: 587 Bytes

Versions: 1

Compression:

Stored size: 587 Bytes

Contents

#include <ruby.h>
#include <math.h>

static VALUE stdev(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 - 1)));
}

void Init_standard_deviation() {
  rb_define_method(rb_cArray, "stdev", stdev, 0);
  rb_define_alias(rb_cArray, "standard_deviation", "stdev");
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
standard_deviation-0.0.1 ext/standard_deviation/standard_deviation.c