Click here to Skip to main content
15,901,505 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I hate it when I am too clever for my own good... Pin
Mike Hankey9-Feb-14 7:23
mveMike Hankey9-Feb-14 7:23 
GeneralRe: I hate it when I am too clever for my own good... Pin
Andy Brummer9-Feb-14 14:57
sitebuilderAndy Brummer9-Feb-14 14:57 
JokeRe: I hate it when I am too clever for my own good... Pin
TnTinMn9-Feb-14 17:44
TnTinMn9-Feb-14 17:44 
GeneralRe: I hate it when I am too clever for my own good... Pin
BillWoodruff9-Feb-14 21:35
professionalBillWoodruff9-Feb-14 21:35 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff9-Feb-14 21:43
mveOriginalGriff9-Feb-14 21:43 
GeneralRe: I hate it when I am too clever for my own good... Pin
CBadger9-Feb-14 22:50
professionalCBadger9-Feb-14 22:50 
GeneralRe: I hate it when I am too clever for my own good... Pin
Fueled By Decaff10-Feb-14 0:31
Fueled By Decaff10-Feb-14 0:31 
GeneralRe: I hate it when I am too clever for my own good... Pin
irneb10-Feb-14 0:38
irneb10-Feb-14 0:38 
You could of course go the fully OO route and make your own "number" class. I.e. the way you'd have done it prior to generics. Then add some implied conversion overloads so you don't need to manually type-cast your int/double/decimal/etc. You could then even accommodate other more complicated data types (e.g. to generate moving averages on a candle-stick-chart with 3 values per item: open, avg, close).

BTW, for the data sample I'd go with either a double linked list (LinkedList<t> with max length) or a circular array, not an Array List as you've done in your sample code (that's implemented as a flat array so a remove from index 0 means it shifts all samples down by 1, if you reverse the order then each insert would shift all items up by 1). Seeing as mostly you'd calculate by iterating over each item in the "list" and you'd not want to move all the samples in the array each time you get a new one the linked list should suffice for this purpose. The circular array I'd only use if I know the size sample will not change after initially creating the object and if I need to reference specific items by index.

You could use a Queue<t> type for this, as well. I "think" it's implemented as a linked list anyway.

Here's what I'm thinking:
C#
public class MyNumber {
    private object _value;
    public object Value {
        get { return _value; }
        set {
            if ((value is int) || (value is double) || (value is decimal))
                _value = value;
            else throw new InvalidCastException();
        }
    }

    public MyNumber(object val) {
        Value = val;
    }

    public static implicit operator MyNumber(int val) { return new MyNumber(val); }

    public static implicit operator int(MyNumber val) { return (int)val._value; }

    public static implicit operator MyNumber(double val) { return new MyNumber(val); }

    public static implicit operator double(MyNumber val) { return (double)val._value; }

    public static implicit operator MyNumber(decimal val) { return new MyNumber(val); }

    public static implicit operator decimal(MyNumber val) { return (decimal)val._value; }
}

public class MovingAverage {
    private LinkedList<MyNumber> _samples = new LinkedList<MyNumber>();
    private decimal total = 0;
    public int SampleSize { get; private set; }
    public decimal Value {
        get {
            if (SampleSize < 0) return (_samples.Count > 0) ? total / _samples.Count : 0;
            return (_samples.Count >= SampleSize) ? total / (decimal)SampleSize : 0;
        }
    }

    public MovingAverage(int sampleSize = -1) {
        SampleSize = sampleSize;
    }

    public void Add(MyNumber value)
    {
        _samples.AddLast(value);
        total += value;
        if (SampleSize > 0) {
            while (_samples.Count > SampleSize) {
                total -= _samples.First;
                _samples.RemoveFirst();
            }
        }
    }
}


Edit: Stupid me that type check should have been or's not and's D'Oh! | :doh:

modified 10-Feb-14 7:03am.

GeneralRe: I hate it when I am too clever for my own good... Pin
Gary Wheeler10-Feb-14 0:51
Gary Wheeler10-Feb-14 0:51 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 0:59
mveOriginalGriff10-Feb-14 0:59 
GeneralRe: I hate it when I am too clever for my own good... Pin
Gary Wheeler10-Feb-14 1:15
Gary Wheeler10-Feb-14 1:15 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 1:23
mveOriginalGriff10-Feb-14 1:23 
GeneralRe: I hate it when I am too clever for my own good... Pin
Gary Wheeler10-Feb-14 1:34
Gary Wheeler10-Feb-14 1:34 
GeneralRe: I hate it when I am too clever for my own good... Pin
Simon O'Riordan from UK10-Feb-14 5:09
Simon O'Riordan from UK10-Feb-14 5:09 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 5:32
mveOriginalGriff10-Feb-14 5:32 
GeneralRe: I hate it when I am too clever for my own good... Pin
James Curran10-Feb-14 1:40
James Curran10-Feb-14 1:40 
GeneralRe: I hate it when I am too clever for my own good... Pin
Richard Deeming10-Feb-14 3:48
mveRichard Deeming10-Feb-14 3:48 
GeneralRe: I hate it when I am too clever for my own good... Pin
Simon O'Riordan from UK10-Feb-14 5:07
Simon O'Riordan from UK10-Feb-14 5:07 
GeneralRe: I hate it when I am too clever for my own good... Pin
OriginalGriff10-Feb-14 5:30
mveOriginalGriff10-Feb-14 5:30 
GeneralRe: I hate it when I am too clever for my own good... Pin
RafagaX10-Feb-14 9:40
professionalRafagaX10-Feb-14 9:40 
GeneralRe: I hate it when I am too clever for my own good... Pin
Moreno Airoldi12-Feb-14 5:50
Moreno Airoldi12-Feb-14 5:50 
GeneralRick York share your dissatisfaction Pin
Sanmayce9-Feb-14 4:45
Sanmayce9-Feb-14 4:45 
GeneralRe: Rick York share your dissatisfaction Pin
OriginalGriff9-Feb-14 5:02
mveOriginalGriff9-Feb-14 5:02 
GeneralRe: Rick York share your dissatisfaction Pin
Sanmayce9-Feb-14 5:18
Sanmayce9-Feb-14 5:18 
GeneralRe: Rick York share your dissatisfaction Pin
R. Giskard Reventlov9-Feb-14 5:47
R. Giskard Reventlov9-Feb-14 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.