Click here to Skip to main content
12,249,797 members (57,010 online)
Click here to Skip to main content

Tagged as

Stats

211.8K views
9K downloads
400 bookmarked
Posted

A Calculation Engine for .NET

, 1 Sep 2013 CPOL
A calculation engine that is small, fast, and extensible.
Friend Class Tally

    ' Fields
    Private _cnt As Double
    Private _max As Double
    Private _min As Double
    Private _numbersOnly As Boolean
    Private _sum As Double
    Private _sum2 As Double

    ' Methods
    Public Sub New()
    End Sub

    Public Sub New(ByVal numbersOnly As Boolean)
        Me._numbersOnly = numbersOnly
    End Sub

    Public Sub Add(ByVal e As Expression)
        Dim ienum As IEnumerable = TryCast(e, IEnumerable)
        If (Not ienum Is Nothing) Then
            Dim value As Object
            For Each value In ienum
                Me.AddValue(value)
            Next
        Else
            Me.AddValue(e.Evaluate)
        End If
    End Sub

    Private Sub AddValue(ByVal value As Object)
        If Not Me._numbersOnly Then
            If ((value Is Nothing) OrElse TypeOf value Is String) Then
                value = 0
            End If
            If TypeOf value Is Boolean Then
                value = IIf(CBool(value), 1, 0)
            End If
        End If
        If TypeOf value Is Integer Then
            value = CInt(value)
        End If
        If TypeOf value Is Double Then
            Dim dbl As Double = CDbl(value)
            Me._sum = (Me._sum + dbl)
            Me._sum2 = (Me._sum2 + (dbl * dbl))
            Me._cnt += 1
            If ((Me._cnt = 1) OrElse (dbl < Me._min)) Then
                Me._min = dbl
            End If
            If ((Me._cnt = 1) OrElse (dbl > Me._max)) Then
                Me._max = dbl
            End If
        End If
    End Sub

    Public Function Average() As Double
        Return (Me._sum / Me._cnt)
    End Function

    Public Function Count() As Double
        Return Me._cnt
    End Function

    Public Function Max() As Double
        Return Me._max
    End Function

    Public Function Min() As Double
        Return Me._min
    End Function

    Public Function Range() As Double
        Return (Me._max - Me._min)
    End Function

    Public Function Std() As Double
        Dim avg As Double = Me.Average
        Return IIf((Me._cnt <= 1), 0, Math.Sqrt(((((Me._sum2 / Me._cnt) - (avg * avg)) * Me._cnt) / (Me._cnt - 1))))
    End Function

    Public Function StdP() As Double
        Dim avg As Double = Me.Average
        Return IIf((Me._cnt <= 1), 0, Math.Sqrt(((Me._sum2 / Me._cnt) - (avg * avg))))
    End Function

    Public Function Sum() As Double
        Return Me._sum
    End Function

    Public Function Var() As Double
        Dim avg As Double = Me.Average
        Return IIf((Me._cnt <= 1), 0, ((((Me._sum2 / Me._cnt) - (avg * avg)) * Me._cnt) / (Me._cnt - 1)))
    End Function

    Public Function VarP() As Double
        Dim avg As Double = Me.Average
        Return IIf((Me._cnt <= 1), 0, ((Me._sum2 / Me._cnt) - (avg * avg)))
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bernardo Castilho
Chief Technology Officer ComponentOne
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 1 Sep 2013
Article Copyright 2011 by Bernardo Castilho
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid