VBscript operator precedence inconsistency with VB6
Asked Answered
V

1

7

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()
Vocational answered 19/10, 2023 at 12:12 Comment(13)
Not according to the VBScript Language Reference - Operator Precedence. Correct way to get 0 would be "-(5^2) + (5^2)".Amygdalate
You are mixing negation with substraction. -5^2 = -5*-5 = 25Mickimickie
@user692941 Thanks for the reference. I was previously looking at a book which did not mention Negation.Vocational
@Geert Bellekens You are right! I see now that the - sign in -5^2 is interpreted differently between case 5 (subtraction) & case 6(negation).Vocational
I have editted the code block to show the inconsistencies between VBS and VB6.Vocational
The Operator Precedence in Visual Basic is different. In VBScript Negation comes before Exponentiation whereas in VB it's Exponentiation then Negation.Amygdalate
@Amygdalate Yes, that VB(.net) way is the same as I was taught in school. And in VB6 (which I am using in this question) precedence appears to be same as VB.net (I updated the question to include VB6 results for comparison with VBS results).Vocational
As implied in the comments, there is no 'fix' or update for this. Despite their similarities, VB6 is a different language from VBscript (and also from VB.net and VBA). These operator precedence differences are what they are, and are just going to have to be lived with.Shrieve
Apologies thought that was the VB documentation link but it is indeed VB.Net (Microsoft make it increasingly harder to find VB documentation) but I'm sure the same operator precedence applies.Amygdalate
Can you add an example of a real equation that includes exponentiation?Kurtis
@Kurtis Not really sure what you mean - all the 11 cases shown are assignments which mimic equations with exponents.Vocational
But those all have constant values, not variables which I presume real equations would have to include. Could change the context somewhat.Kurtis
@Kurtis I am using MSScriptControl.ScriptControl with VBScript from within VB6 in order to Evaluate a 100% numerical math-expression contained within a VB6 string variable. I am simply checking that an in-string math-expression derived by expansion from another in-string math expression gives the same numerical result when test numerical values are inserted into math parameters. I don't need to consider passing variables to VBScript.Vocational
A
6

It's worth noting that VBScript and Visual Basic are different languages, one is a scripting language while the other is a fully-fledged programming language.

The Visual Basic Operator Precedence is well documented across the various versions of Visual Basic (VB, VBA, VB.Net etc) - See What is the operator precedence order in Visual Basic 6.0? and Operator Precedence in Visual Basic.

When you compare this with the VBScript Operator Precedence you see a major difference between the Arithmetic Operation Precedence Order.

VBScript favours Negation over Exponentiation, while Visual Basic favours Exponentiation over Negation, which explains why you see different results.

The obvious workaround is to use brackets to be explicit about your intent (which you mention in the original question).

Without brackets

'VBScript
-5^2 '-5 * -5 = 25
'Visual Basic
-5^2 '-5 * 5 = -25

With brackets, we can force the intent

'VBScript
-(5^2) '-(5 * 5) = -25
'Visual Basic
-(5^2) '-(5 * 5) = -25
Amygdalate answered 20/10, 2023 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.