I am obsessed with code performance, and calculating the performance of long-running algorithms entails – a lot of the times – calculating averages.
To make calculating averages easier (that’s the kind of lazy person I am) I’ve written a few lines of code. Specifically, I’ve written an immutable data-structure in C# that can be used to keep track of an average, without having to calculate the average from the beginning every time a new value is added. The math is ridiculously easy, so I went get into that. The reason I like this data-structure is that it’s very easy to use. Here’s what you have to do to calculate a new average:
If you check the property a.Value, or cast “a” to a decimal, double, or float, you’ll find that it is now equal to 5.5.
Also, averages can be added. Running the following code will result to “c” being equal to 2.5. Which is right, as