> curiousgeorge...@hotmail.com wrote:
> > what VBA function takes a numeric expression in string form,
[quoted text clipped - 3 lines]
> application.evaluate(s)
> should work ok.
But not "as if I entered the expression directly" in VBA. Not
surprisingly, application.Evaluate interprets the expression as Excel
does -- including its heuristics.
For example, application.Evaluate("12.22 - 0.02 - 12.2") returns
exactly zero, which is Excel's adjustment when the subtraction result
is "close" to zero. application.Evaluate("(12.22 - 0.02 - 12.2)")
returns about 1.7764E-15, but that is different from the VBA result of
about 1.3496E-15. As Jerry Lewis explained, this is because Excel
stores intermediate results into 64-bit doubles whereas VBA uses the
intermediate results in the 80-bit FPU registers. See the VBA example
below.
Having said as much, I confess that I don't know of a better answer.
Is there one?
Dim x As Double, y As Double, z As Double
Dim a As Double, b As Double, c As Double
x = Application.Evaluate("12.22 - 0.02 - 12.2")
y = Application.Evaluate("(12.22 - 0.02 - 12.2)")
z = 12.22 - 0.02 - 12.2
Debug.Print "-----"
Debug.Print IIf(x = 0, True, False)
Debug.Print IIf(y = z, True, False)
Debug.Print y
Debug.Print z
a = 12.22
b = a - 0.02
c = b - 12.2
Debug.Print IIf(y = c, True, False)
Debug.Print c
Dave Peterson - 31 Dec 2007 14:38 GMT
If the string you're evaluating is simple, maybe you could parse it to its
component parts and do the arithmetic yourself using VBA's functions.
But parsing a generic string/formula sounds pretty daunting to me.
> > curiousgeorge...@hotmail.com wrote:
> > > what VBA function takes a numeric expression in string form,
[quoted text clipped - 35 lines]
> Debug.Print IIf(y = c, True, False)
> Debug.Print c

Signature
Dave Peterson
curiousgeorge408@hotmail.com - 31 Dec 2007 15:23 GMT
> If the string you're evaluating is simple, maybe you could parse it to its
> component parts and do the arithmetic yourself using VBA's functions.
> But parsing a generic string/formula sounds pretty daunting to me.
Parse __and__ generate Intel-compatible assembly code!! I believe
that would be the only way of ensuring exactly the same results that
VBA produces, including side-effects of using the FPU registers for
intermediate results. (Unless, of course, you are also proposing that
I emulate the 80-bit arithmetic as well <wink>.)
And yes, I would need a generic expression parser/code-generator
because the expressions are complex and unpredictable.
Enough said! I take your response to mean: there is no such thing as
"someFunction" in VBA that will evaluate a numeric expression in
string form exactly as if I had entered it into VBA directly.
Not the answer I wanted. But thanks for clearing that up for me.
Equiangular - 31 Dec 2007 16:22 GMT
How about this?
It's quite powerful.
http://digilander.libero.it/foxes/mathparser/MathExpressionsParser.htm
>> If the string you're evaluating is simple, maybe you could parse it to its
>> component parts and do the arithmetic yourself using VBA's functions.
[quoted text clipped - 14 lines]
>
> Not the answer I wanted. But thanks for clearing that up for me.
Dave Peterson - 31 Dec 2007 17:23 GMT
I don't know anything that would work the way you want.
> > If the string you're evaluating is simple, maybe you could parse it to its
> > component parts and do the arithmetic yourself using VBA's functions.
[quoted text clipped - 14 lines]
>
> Not the answer I wanted. But thanks for clearing that up for me.

Signature
Dave Peterson