I am running an old VB6.0 program in interpretive mode (i.e. not as a .exe) on Win-7 32-bit.
The program accesses VBScript via an MSScriptControl.ScriptControl (as illustrated in the code block below) in function calc
which here is called by the test function tc
which itself can be called from another sub or function or from the immediate pane by typing tc <expression>
.
Here <expression>
is a numeric math expression like 2+3^2*(9-3.5)^3
.
When the input expression includes a pattern like -a^b
the results are sometimes surprising to me. (Here a,b
are placeholders for real numbers - I do not expect calc
to handle expressions containing variable names.)
From the rules of operator precedence, based on VB6 behavior I would expect -a^b --> -(a^b)
but often -a^b --> +(a^b)
. Spaces inside the expression have no discernible effect, as expected.
In the different Cases
shown below I label consistent results by "ok" and inconsistent results by "?".
I could address the problem by bracketing every case of a^b
thus (a^b)
. But, given I am working with long complicated equations, this will (i) be tedious to implement and (ii) impair the readability of the code.
Is this behavior known about? Is there a "behind-the-scenes" fix (e.g. a software patch or version upgrade)?
vb6
Public Function calc(ipEqtn$)
Set objScript = CreateObject("MSScriptControl.ScriptControl")
objScript.Language = "VBScript"
calc = objScript.eval(ipEqtn$)
End Function '... calc
Public Function tc(n)
Select Case (n)
Case (1): eqtn$ = "-(5 ^ 2) ": VB6 = -(5 ^ 2) ' --> -25,-25 ok
Case (2): eqtn$ = "(- 5 ^ 2) ": VB6 = (-5 ^ 2) ' --> 25,-25 ?
Case (3): eqtn$ = "(-5^2) ": VB6 = (-5 ^ 2) ' --> 25,-25 ?
Case (4): eqtn$ = "- 5 ^ 2 ": VB6 = -5 ^ 2 ' --> 25,-25 ?
Case (5): eqtn$ = "( 5^2 -5^2 ) ": VB6 = (5 ^ 2 - 5 ^ 2) ' --> 0,0 ok
Case (6): eqtn$ = "( -5^2 + 5^2 ) ": VB6 = (-5 ^ 2 + 5 ^ 2) ' --> 50,0 ?
Case (7): eqtn$ = " 5^2 -5^2 ": VB6 = 5 ^ 2 - 5 ^ 2 ' --> 0,0 ok
Case (8): eqtn$ = " -5^2 + 5^2 ": VB6 = -5 ^ 2 + 5 ^ 2 ' --> 50,0 ?
Case (9): eqtn$ = " + 5^2 -5^2 ": VB6 = 5 ^ 2 - 5 ^ 2 ' --> 0,0
Case (10): eqtn$ = " + 5^2 + -5^2 ": VB6 = 5 ^ 2 + -5 ^ 2 ' -->50,0
Case (11): eqtn$ = " + 5^2 + -1*5^2 ": VB6 = 5 ^ 2 + -1 * 5 ^ 2 ' --> 0,0
End Select
vbs = calc(eqtn$)
Debug.Print vbs, VB6
End Function '... tc()
"-(5^2) + (5^2)"
. – Amygdalate