Steve Hebert’s .Math Library

Sponsored by Hebert Software Services, LLC

All functions, operators and value objects are created using an IValue interface that defines a single function: double GetValue(). This structure allows all calls to be made polymorphically and all components are interchangeable – allowing us to represent any form of mathematical function we are interested in duplicating.

 

An operator such as addition is defined as such:

 

CAdd : IValue

{

                CAdd(…){}

                Private IValue _valueLeft;

                Private IValue _valueRight;

 

                Public double GetValue()

                {

                                Return _valueLeft.GetValue() + _valueRight.GetValue();

                }

}

 

 

 

 

 

 

 

Functions are defined (typically) as one value items and look like this:

 

CSin : IValue

{

                CSin(…){}

                Private IValue _value;

                Public double GetValue()

                {

                                Return Math.Sin(_value.GetValue());

                }

}

 

 

 

 

 

 

 

Static values are implemented as objects as well and typically look like this:

CStaticValue : IValue

{

                CStaticValue( double dValue ) {_dValue = dValue; }

                Private double _dValue;

                Public double GetValue()

                {

                                Return _dValue;

                }

}

 

 

 

 

 

 

Variable names are similar to static values, however they reference a shared value pool.  This way if I have a function like ((a^2) * (4*a) + a), the value of ‘a’ is stored only once.  I won’t go over the code itself, but the functionality is similar to the CStaticValue class above.

Given the code values we’ve seen, we can begin to understand how the compiler represents a function.  Let’s take sin((4+7) + 9)+10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The compiler holds onto the root node and calls ‘GetValue()’ to evaluate the equation.  This design allows the same processing for complex equations and simple equations alike.

 

 

 

 

 

 

 

CAdd

 

CSin

(x)

CStaticValue

 

CAdd

 

CSin

(10)

CStatic...

 

CAdd

(9)

CStatic...

 

CAdd

(7)

CStatic...

(4)

CStatic