Skip to content

Commit

Permalink
Dont use dynamic in moving average
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipAlg committed May 17, 2024
1 parent 69b64c5 commit 8325925
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions AGXUnity/Utils/MovingAverage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace AGXUnity.Utils
Expand All @@ -9,26 +10,25 @@ public class MovingAverage<T>
{
private readonly Queue<T> m_samples;
private readonly int m_frameSize;
private T m_accumulatedValue;
private double m_accumulatedValue;

public int Size => m_frameSize;

public MovingAverage(int size)
public MovingAverage( int size )
{
m_frameSize = size;
m_samples = new Queue<T>(size);
m_samples = new Queue<T>( size );
}

public T Value => (dynamic)m_accumulatedValue / (float)m_samples.Count;
public double Value => (double)m_accumulatedValue / m_samples.Count;

public void Add(dynamic entry)
public void Add( T entry )
{
m_accumulatedValue += entry;
m_samples.Enqueue(entry);
m_accumulatedValue += Convert.ToDouble( entry );
m_samples.Enqueue( entry );

if (m_samples.Count > m_frameSize)
{
dynamic last = m_samples.Dequeue();
if ( m_samples.Count > m_frameSize ) {
double last = Convert.ToDouble(m_samples.Dequeue());
m_accumulatedValue -= last;
}
}
Expand Down

0 comments on commit 8325925

Please sign in to comment.