-
-
Save jasonwatkinspdx/187618 to your computer and use it in GitHub Desktop.
Measurement class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Measurement | |
| attr_accessor :count, :sum, :sum_squares, :min, :max | |
| def initialize | |
| @count, @sum, @sum_squares, @min, @max = 0, 0, 0, nil, nil | |
| end | |
| def record n | |
| @count += 1 | |
| @sum += n | |
| @sum_squares += n**2 | |
| @min ||= n | |
| @max ||= n | |
| @min = n if n < @min | |
| @max = n if n > @max | |
| end | |
| def avg | |
| sum.to_f / count | |
| end | |
| def sdev | |
| ((sum_squares - (sum*avg))/(count - 1))**0.5 | |
| end | |
| def to_s | |
| "avg: %12g sdev: %12g sum: %12g min: %12g max: %12g" % [avg, sdev, @sum, @min, @max] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment