Skip to content

modelingevolution/numeric

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ModelingEvolution.Numeric

NuGet

Generic numeric utilities for .NET 10.0 using INumber<T> generic math.

Install

dotnet add package ModelingEvolution.Numeric

Types

MovingAverage<T>

High-performance moving average calculator with O(1) add and average operations. Uses a circular buffer with running sum - zero allocations after construction.

var avg = new MovingAverage<double>(windowSize: 5);

// Add values
avg.Add(10.0);
avg.Add(20.0);
avg.Add(30.0);

Console.WriteLine(avg.Average); // 20.0
Console.WriteLine(avg.Count);   // 3
Console.WriteLine(avg.Capacity); // 5

// Operator support
avg += 40.0;
avg += 50.0;

// Window slides - oldest value drops out
avg += 60.0;
Console.WriteLine(avg.Average); // (20+30+40+50+60)/5 = 40

// Implicit conversion to T
double current = avg; // 40.0

// Reset
avg.Clear();

Works with any INumber<T> type: double, float, decimal, int, etc.

Real-world: FPS counter

var frameAvg = new MovingAverage<double>(60); // last 60 frames

void OnFrame(double deltaTime)
{
    frameAvg += deltaTime;
    double fps = 1.0 / (double)frameAvg;
    Console.WriteLine($"FPS: {fps:F1}");
}

Build

dotnet build Sources/ModelingEvolution.Numeric.sln
dotnet test Sources/ModelingEvolution.Numeric.Tests/

License

MIT

About

Generic numeric utilities for .NET - MovingAverage, and more

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors