Click here to Skip to main content
11,434,695 members (47,567 online)
Click here to Skip to main content
Add your own
alternative version

A Calculation Engine for .NET

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

    ' Methods
    Public Shared Sub Register(ByVal ce As CalcEngine)
        ce.RegisterFunction("ABS", 1, New CalcEngineFunction(AddressOf MathTrig.Abs))
        ce.RegisterFunction("ACOS", 1, New CalcEngineFunction(AddressOf MathTrig.Acos))
        ce.RegisterFunction("ASIN", 1, New CalcEngineFunction(AddressOf MathTrig.Asin))
        ce.RegisterFunction("ATAN", 1, New CalcEngineFunction(AddressOf MathTrig.Atan))
        ce.RegisterFunction("ATAN2", 2, New CalcEngineFunction(AddressOf MathTrig.Atan2))
        ce.RegisterFunction("CEILING", 1, New CalcEngineFunction(AddressOf MathTrig.Ceiling))
        ce.RegisterFunction("COS", 1, New CalcEngineFunction(AddressOf MathTrig.Cos))
        ce.RegisterFunction("COSH", 1, New CalcEngineFunction(AddressOf MathTrig.Cosh))
        ce.RegisterFunction("EXP", 1, New CalcEngineFunction(AddressOf MathTrig.Exp))
        ce.RegisterFunction("FLOOR", 1, New CalcEngineFunction(AddressOf MathTrig.Floor))
        ce.RegisterFunction("INT", 1, New CalcEngineFunction(AddressOf MathTrig.Int))
        ce.RegisterFunction("LN", 1, New CalcEngineFunction(AddressOf MathTrig.Ln))
        ce.RegisterFunction("LOG", 1, 2, New CalcEngineFunction(AddressOf MathTrig.Log))
        ce.RegisterFunction("LOG10", 1, New CalcEngineFunction(AddressOf MathTrig.Log10))
        ce.RegisterFunction("PI", 0, New CalcEngineFunction(AddressOf MathTrig.Pi))
        ce.RegisterFunction("POWER", 2, New CalcEngineFunction(AddressOf MathTrig.Power))
        ce.RegisterFunction("RAND", 0, New CalcEngineFunction(AddressOf MathTrig.Rand))
        ce.RegisterFunction("RANDBETWEEN", 2, New CalcEngineFunction(AddressOf MathTrig.RandBetween))
        ce.RegisterFunction("SIGN", 1, New CalcEngineFunction(AddressOf MathTrig.Sign))
        ce.RegisterFunction("SIN", 1, New CalcEngineFunction(AddressOf MathTrig.Sin))
        ce.RegisterFunction("SINH", 1, New CalcEngineFunction(AddressOf MathTrig.Sinh))
        ce.RegisterFunction("SQRT", 1, New CalcEngineFunction(AddressOf MathTrig.Sqrt))
        ce.RegisterFunction("SUM", 1, &H7FFFFFFF, New CalcEngineFunction(AddressOf MathTrig.Sum))
        ce.RegisterFunction("TAN", 1, New CalcEngineFunction(AddressOf MathTrig.Tan))
        ce.RegisterFunction("TANH", 1, New CalcEngineFunction(AddressOf MathTrig.Tanh))
        ce.RegisterFunction("TRUNC", 1, New CalcEngineFunction(AddressOf MathTrig.Trunc))
    End Sub

    Private Shared Function Abs(ByVal p As List(Of Expression)) As Object
        Return Math.Abs(CDbl(p.Item(0)))
    End Function

    Private Shared Function Acos(ByVal p As List(Of Expression)) As Object
        Return Math.Acos(CDbl(p.Item(0)))
    End Function

    Private Shared Function Asin(ByVal p As List(Of Expression)) As Object
        Return Math.Asin(CDbl(p.Item(0)))
    End Function

    Private Shared Function Atan(ByVal p As List(Of Expression)) As Object
        Return Math.Atan(CDbl(p.Item(0)))
    End Function

    Private Shared Function Atan2(ByVal p As List(Of Expression)) As Object
        Return Math.Atan2(CDbl(p.Item(0)), CDbl(p.Item(1)))
    End Function

    Private Shared Function Ceiling(ByVal p As List(Of Expression)) As Object
        Return Math.Ceiling(CDbl(p.Item(0)))
    End Function

    Private Shared Function Cos(ByVal p As List(Of Expression)) As Object
        Return Math.Cos(CDbl(p.Item(0)))
    End Function

    Private Shared Function Cosh(ByVal p As List(Of Expression)) As Object
        Return Math.Cosh(CDbl(p.Item(0)))
    End Function

    Private Shared Function Exp(ByVal p As List(Of Expression)) As Object
        Return Math.Exp(CDbl(p.Item(0)))
    End Function

    Private Shared Function Floor(ByVal p As List(Of Expression)) As Object
        Return Math.Floor(CDbl(p.Item(0)))
    End Function

    Private Shared Function Int(ByVal p As List(Of Expression)) As Object
        Return CInt(CDbl(p.Item(0)))
    End Function

    Private Shared Function Ln(ByVal p As List(Of Expression)) As Object
        Return Math.Log(CDbl(p.Item(0)))
    End Function

    Private Shared Function Log(ByVal p As List(Of Expression)) As Object
        Dim lbase As Double = IIf((p.Count > 1), CDbl(p.Item(1)), 10)
        Return Math.Log(CDbl(p.Item(0)), lbase)
    End Function

    Private Shared Function Log10(ByVal p As List(Of Expression)) As Object
        Return Math.Log10(CDbl(p.Item(0)))
    End Function

    Private Shared Function Pi(ByVal p As List(Of Expression)) As Object
        Return 3.1415926535897931
    End Function

    Private Shared Function Power(ByVal p As List(Of Expression)) As Object
        Return Math.Pow(CDbl(p.Item(0)), CDbl(p.Item(1)))
    End Function

    Private Shared Function Rand(ByVal p As List(Of Expression)) As Object
        Return MathTrig._rnd.NextDouble
    End Function

    Private Shared Function RandBetween(ByVal p As List(Of Expression)) As Object
        Return MathTrig._rnd.Next(CInt(CDbl(p.Item(0))), CInt(CDbl(p.Item(1))))
    End Function

    Private Shared Function Sign(ByVal p As List(Of Expression)) As Object
        Return Math.Sign(CDbl(p.Item(0)))
    End Function

    Private Shared Function Sin(ByVal p As List(Of Expression)) As Object
        Return Math.Sin(CDbl(p.Item(0)))
    End Function

    Private Shared Function Sinh(ByVal p As List(Of Expression)) As Object
        Return Math.Sinh(CDbl(p.Item(0)))
    End Function

    Private Shared Function Sqrt(ByVal p As List(Of Expression)) As Object
        Return Math.Sqrt(CDbl(p.Item(0)))
    End Function

    Private Shared Function Sum(ByVal p As List(Of Expression)) As Object
        Dim tally As New Tally
        Dim e As Expression
        For Each e In p
            tally.Add(e)
        Next
        Return tally.Sum
    End Function

    Private Shared Function Tan(ByVal p As List(Of Expression)) As Object
        Return Math.Tan(CDbl(p.Item(0)))
    End Function

    Private Shared Function Tanh(ByVal p As List(Of Expression)) As Object
        Return Math.Tanh(CDbl(p.Item(0)))
    End Function

    Private Shared Function Trunc(ByVal p As List(Of Expression)) As Object
        Return CDbl(CInt(CDbl(p.Item(0))))
    End Function


    ' Fields
    Private Shared _rnd As Random = New Random
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

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