QB64 Phoenix Edition
Math Evaluator - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Prolific Programmers (https://staging.qb64phoenix.com/forumdisplay.php?fid=26)
+---- Forum: SMcNeill (https://staging.qb64phoenix.com/forumdisplay.php?fid=29)
+---- Thread: Math Evaluator (/showthread.php?tid=1266)



Math Evaluator - SMcNeill - 12-11-2022

I was going to point someone to my math evaluator in a different post, to showcase our math order of operations, and after searching the forums, I couldn't find it.  GASP!!

I guess this little routine was over at the old forums and was just one that I forgot to move over, when things went belly up and burnt down.  My apologies.

Enjoy guys, and feel free to make use of the code within in any of your projects that you might want -- it's a pretty comprehensive math evaluation routine.  Pass it a string full of math stuff, get back the answer to it.   It's really that simple. 

Code: (Select All)
ReDim Shared OName(0) As String 'Operation Name
ReDim Shared PL(0) As Integer 'Priority Level
Dim Shared QuickReturn As Integer
Set_OrderOfOperations 'This will also make certain our directories are valid, and if not make them.

Do
    Input math$
    Print Evaluate_Expression(math$)
Loop


'Steve Subs/Functins for _MATH support with CONST
Function Evaluate_Expression$ (e$)
    t$ = e$ 'So we preserve our original data, we parse a temp copy of it

    b = InStr(UCase$(e$), "EQL") 'take out assignment before the preparser sees it
    If b Then t$ = Mid$(e$, b + 3): var$ = UCase$(LTrim$(RTrim$(Mid$(e$, 1, b - 1))))

    QuickReturn = 0
    PreParse t$

    If QuickReturn Then Evaluate_Expression$ = t$: Exit Function

    If Left$(t$, 5) = "ERROR" Then Evaluate_Expression$ = t$: Exit Function

    'Deal with brackets first
    exp$ = "(" + t$ + ")" 'Starting and finishing brackets for our parse routine.

    Do
        Eval_E = InStr(exp$, ")")
        If Eval_E > 0 Then
            c = 0
            Do Until Eval_E - c <= 0
                c = c + 1
                If Eval_E Then
                    If Mid$(exp$, Eval_E - c, 1) = "(" Then Exit Do
                End If
            Loop
            s = Eval_E - c + 1
            If s < 1 Then Print "ERROR -- BAD () Count": End
            eval$ = " " + Mid$(exp$, s, Eval_E - s) + " " 'pad with a space before and after so the parser can pick up the values properly.
            ParseExpression eval$

            eval$ = LTrim$(RTrim$(eval$))
            If Left$(eval$, 5) = "ERROR" Then Evaluate_Expression$ = eval$: Exit Function
            exp$ = DWD(Left$(exp$, s - 2) + eval$ + Mid$(exp$, Eval_E + 1))
            If Mid$(exp$, 1, 1) = "N" Then Mid$(exp$, 1) = "-"

            temppp$ = DWD(Left$(exp$, s - 2) + " ## " + eval$ + " ## " + Mid$(exp$, E + 1))
        End If
    Loop Until Eval_E = 0
    c = 0
    Do
        c = c + 1
        Select Case Mid$(exp$, c, 1)
            Case "0" To "9", ".", "-" 'At this point, we should only have number values left.
            Case Else: Evaluate_Expression$ = "ERROR - Unknown Diagnosis: (" + exp$ + ") ": Exit Function
        End Select
    Loop Until c >= Len(exp$)

    Evaluate_Expression$ = exp$
End Function



Sub ParseExpression (exp$)
    Dim num(10) As String
    'We should now have an expression with no () to deal with
    If Mid$(exp$, 2, 1) = "-" Then exp$ = "0+" + Mid$(exp$, 2)
    For J = 1 To 250
        lowest = 0
        Do Until lowest = Len(exp$)
            lowest = Len(exp$): OpOn = 0
            For P = 1 To UBound(OName)
                'Look for first valid operator
                If J = PL(P) Then 'Priority levels match
                    If Left$(exp$, 1) = "-" Then op = InStr(2, exp$, OName(P)) Else op = InStr(exp$, OName(P))
                    If op > 0 And op < lowest Then lowest = op: OpOn = P
                End If
            Next
            If OpOn = 0 Then Exit Do 'We haven't gotten to the proper PL for this OP to be processed yet.
            If Left$(exp$, 1) = "-" Then op = InStr(2, exp$, OName(OpOn)) Else op = InStr(exp$, OName(OpOn))
            numset = 0

            '*** SPECIAL OPERATION RULESETS
            If OName(OpOn) = "-" Then 'check for BOOLEAN operators before the -
                Select Case Mid$(exp$, op - 3, 3)
                    Case "NOT", "XOR", "AND", "EQV", "IMP"
                        Exit Do 'Not an operator, it's a negative
                End Select
                If Mid$(exp$, op - 3, 2) = "OR" Then Exit Do 'Not an operator, it's a negative
            End If

            If op Then
                c = Len(OName(OpOn)) - 1
                Do
                    Select Case Mid$(exp$, op + c + 1, 1)
                        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "N": numset = -1 'Valid digit
                        Case "-" 'We need to check if it's a minus or a negative
                            If OName(OpOn) = "_PI" Or numset Then Exit Do
                        Case Else 'Not a valid digit, we found our separator
                            Exit Do
                    End Select
                    c = c + 1
                Loop Until op + c >= Len(exp$)
                E = op + c

                c = 0
                Do
                    c = c + 1
                    Select Case Mid$(exp$, op - c, 1)
                        Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "N" 'Valid digit
                        Case "-" 'We need to check if it's a minus or a negative
                            c1 = c
                            bad = 0
                            Do
                                c1 = c1 + 1
                                Select Case Mid$(exp$, op - c1, 1)
                                    Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."
                                        bad = -1
                                        Exit Do 'It's a minus sign
                                    Case Else
                                        'It's a negative sign and needs to count as part of our numbers
                                End Select
                            Loop Until op - c1 <= 0
                            If bad Then Exit Do 'We found our seperator
                        Case Else 'Not a valid digit, we found our separator
                            Exit Do
                    End Select
                Loop Until op - c <= 0
                s = op - c
                num(1) = Mid$(exp$, s + 1, op - s - 1) 'Get our first number
                num(2) = Mid$(exp$, op + Len(OName(OpOn)), E - op - Len(OName(OpOn)) + 1) 'Get our second number
                If Mid$(num(1), 1, 1) = "N" Then Mid$(num(1), 1) = "-"
                If Mid$(num(2), 1, 1) = "N" Then Mid$(num(2), 1) = "-"
                num(3) = EvaluateNumbers(OpOn, num())
                If Mid$(num(3), 1, 1) = "-" Then Mid$(num(3), 1) = "N"
                'PRINT "*************"
                'PRINT num(1), OName(OpOn), num(2), num(3), exp$
                If Left$(num(3), 5) = "ERROR" Then exp$ = num(3): Exit Sub
                exp$ = LTrim$(N2S(DWD(Left$(exp$, s) + RTrim$(LTrim$(num(3))) + Mid$(exp$, E + 1))))
                'PRINT exp$
            End If
            op = 0
        Loop
    Next

End Sub



Sub Set_OrderOfOperations
    'PL sets our priortity level. 1 is highest to 65535 for the lowest.
    'I used a range here so I could add in new priority levels as needed.
    'OName ended up becoming the name of our commands, as I modified things.... Go figure!  LOL!

    'Constants get evaluated first, with a Priority Level of 1
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_PI"
    ReDim _Preserve PL(i): PL(i) = 1
    'I'm not certain where exactly percentages should go.  They kind of seem like a special case to me.  COS10% should be COS.1 I'd think...
    'I'm putting it here for now, and if anyone knows someplace better for it in our order of operations, let me know.
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "%"
    ReDim _Preserve PL(i): PL(i) = 5
    'Then Functions with PL 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_ACOS"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_ASIN"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_ARCSEC"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_ARCCSC"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_ARCCOT"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_SECH"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_CSCH"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_COTH"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "COS"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "SIN"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "TAN"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "LOG"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "EXP"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "ATN"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_D2R"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_D2G"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_R2D"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_R2G"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_G2D"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_G2R"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "ABS"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "SGN"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "INT"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_ROUND"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "FIX"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_SEC"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_CSC"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "_COT"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "ASC"
    ReDim _Preserve PL(i): PL(i) = 10
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "CHR$"
    ReDim _Preserve PL(i): PL(i) = 10

    'Exponents with PL 20
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "^"
    ReDim _Preserve PL(i): PL(i) = 20
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "SQR"
    ReDim _Preserve PL(i): PL(i) = 20
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "ROOT"
    ReDim _Preserve PL(i): PL(i) = 20
    'Multiplication and Division PL 30
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "*"
    ReDim _Preserve PL(i): PL(i) = 30
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "/"
    ReDim _Preserve PL(i): PL(i) = 30
    'Integer Division PL 40
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "\"
    ReDim _Preserve PL(i): PL(i) = 40
    'MOD PL 50
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "MOD"
    ReDim _Preserve PL(i): PL(i) = 50
    'Addition and Subtraction PL 60
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "+"
    ReDim _Preserve PL(i): PL(i) = 60
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "-"
    ReDim _Preserve PL(i): PL(i) = 60

    'Relational Operators =, >, <, <>, <=, >=  PL 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "<>"
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "><" 'These next three are just reversed symbols as an attempt to help process a common typo
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "<="
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = ">="
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "=<" 'I personally can never keep these things straight.  Is it < = or = <...
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "=>" 'Who knows, check both!
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = ">"
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "<"
    ReDim _Preserve PL(i): PL(i) = 70
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "="
    ReDim _Preserve PL(i): PL(i) = 70
    'Logical Operations PL 80+
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "NOT"
    ReDim _Preserve PL(i): PL(i) = 80
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "AND"
    ReDim _Preserve PL(i): PL(i) = 90
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "OR"
    ReDim _Preserve PL(i): PL(i) = 100
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "XOR"
    ReDim _Preserve PL(i): PL(i) = 110
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "EQV"
    ReDim _Preserve PL(i): PL(i) = 120
    i = i + 1: ReDim _Preserve OName(i): OName(i) = "IMP"
    ReDim _Preserve PL(i): PL(i) = 130

End Sub

Function EvaluateNumbers$ (p, num() As String)
    Dim n1 As _Float, n2 As _Float, n3 As _Float
    Select Case OName(p) 'Depending on our operator..
        Case "_PI": n1 = 3.14159265358979323846264338327950288## 'Future compatable in case something ever stores extra digits for PI
        Case "%": n1 = (Val(num(1))) / 100 'Note percent is a special case and works with the number BEFORE the % command and not after
        Case "_ACOS": n1 = _Acos(Val(num(2)))
        Case "_ASIN": n1 = _Asin(Val(num(2)))
        Case "_ARCSEC": n1 = _Arcsec(Val(num(2)))
        Case "_ARCCSC": n1 = _Arccsc(Val(num(2)))
        Case "_ARCCOT": n1 = _Arccot(Val(num(2)))
        Case "_SECH": n1 = _Sech(Val(num(2)))
        Case "_CSCH": n1 = _Csch(Val(num(2)))
        Case "_COTH": n1 = _Coth(Val(num(2)))
        Case "COS": n1 = Cos(Val(num(2)))
        Case "SIN": n1 = Sin(Val(num(2)))
        Case "TAN": n1 = Tan(Val(num(2)))
        Case "LOG": n1 = Log(Val(num(2)))
        Case "EXP": n1 = Exp(Val(num(2)))
        Case "ATN": n1 = Atn(Val(num(2)))
        Case "_D2R": n1 = 0.0174532925 * (Val(num(2)))
        Case "_D2G": n1 = 1.1111111111 * (Val(num(2)))
        Case "_R2D": n1 = 57.2957795 * (Val(num(2)))
        Case "_R2G": n1 = 0.015707963 * (Val(num(2)))
        Case "_G2D": n1 = 0.9 * (Val(num(2)))
        Case "_G2R": n1 = 63.661977237 * (Val(num(2)))
        Case "ABS": n1 = Abs(Val(num(2)))
        Case "SGN": n1 = Sgn(Val(num(2)))
        Case "INT": n1 = Int(Val(num(2)))
        Case "_ROUND": n1 = _Round(Val(num(2)))
        Case "FIX": n1 = Fix(Val(num(2)))
        Case "_SEC": n1 = _Sec(Val(num(2)))
        Case "_CSC": n1 = _Csc(Val(num(2)))
        Case "_COT": n1 = _Cot(Val(num(2)))
        Case "^": n1 = Val(num(1)) ^ Val(num(2))
        Case "SQR": n1 = Sqr(Val(num(2)))
        Case "ROOT"
            n1 = Val(num(1)): n2 = Val(num(2))
            If n2 = 1 Then EvaluateNumbers$ = RTrim$(LTrim$(Str$(n1))): Exit Function
            If n1 < 0 And n2 >= 1 Then sign = -1: n1 = -n1 Else sign = 1
            n3 = 1## / n2
            If n3 <> Int(n3) And n2 < 1 Then sign = Sgn(n1): n1 = Abs(n1)
            n1 = sign * (n1 ^ n3)
        Case "*": n1 = Val(num(1)) * Val(num(2))
        Case "/": n1 = Val(num(1)) / Val(num(2))
        Case "\"
            If Val(num(2)) <> 0 Then
                n1 = Val(num(1)) \ Val(num(2))
            Else
                EvaluateNumbers$ = "ERROR - Bad operation (We shouldn't see this)"
                Exit Function
            End If
        Case "MOD": n1 = Val(num(1)) Mod Val(num(2))
        Case "+": n1 = Val(num(1)) + Val(num(2))
        Case "-": n1 = Val(num(1)) - Val(num(2))
        Case "=": n1 = Val(num(1)) = Val(num(2))
        Case ">": n1 = Val(num(1)) > Val(num(2))
        Case "<": n1 = Val(num(1)) < Val(num(2))
        Case "<>", "><": n1 = Val(num(1)) <> Val(num(2))
        Case "<=", "=<": n1 = Val(num(1)) <= Val(num(2))
        Case ">=", "=>": n1 = Val(num(1)) >= Val(num(2))
        Case "NOT": n1 = Not Val(num(2))
        Case "AND": n1 = Val(num(1)) And Val(num(2))
        Case "OR": n1 = Val(num(1)) Or Val(num(2))
        Case "XOR": n1 = Val(num(1)) Xor Val(num(2))
        Case "EQV": n1 = Val(num(1)) Eqv Val(num(2))
        Case "IMP": n1 = Val(num(1)) Imp Val(num(2))
        Case Else
            EvaluateNumbers$ = "ERROR - Bad operation (We shouldn't see this)" 'Let's say we're bad...
    End Select
    EvaluateNumbers$ = RTrim$(LTrim$(Str$(n1)))
End Function

Function DWD$ (exp$) 'Deal With Duplicates
    'To deal with duplicate operators in our code.
    'Such as --  becomes a +
    '++ becomes a +
    '+- becomes a -
    '-+ becomes a -
    t$ = exp$
    Do
        bad = 0
        Do
            l = InStr(t$, "++")
            If l Then t$ = Left$(t$, l - 1) + "+" + Mid$(t$, l + 2): bad = -1
        Loop Until l = 0
        Do
            l = InStr(t$, "+-")
            If l Then t$ = Left$(t$, l - 1) + "-" + Mid$(t$, l + 2): bad = -1
        Loop Until l = 0
        Do
            l = InStr(t$, "-+")
            If l Then t$ = Left$(t$, l - 1) + "-" + Mid$(t$, l + 2): bad = -1
        Loop Until l = 0
        Do
            l = InStr(t$, "--")
            If l Then t$ = Left$(t$, l - 1) + "+" + Mid$(t$, l + 2): bad = -1
        Loop Until l = 0
    Loop Until Not bad
    DWD$ = t$
    VerifyString t$
End Function

Sub PreParse (e$)
    Dim f As _Float

    t$ = e$

    'First strip all spaces
    t$ = ""
    For i = 1 To Len(e$)
        If Mid$(e$, i, 1) <> " " Then t$ = t$ + Mid$(e$, i, 1)
    Next

    t$ = UCase$(t$)
    If t$ = "" Then e$ = "ERROR -- NULL string; nothing to evaluate": Exit Sub

    'ERROR CHECK by counting our brackets
    l = 0
    Do
        l = InStr(l + 1, t$, "("): If l Then c = c + 1
    Loop Until l = 0
    l = 0
    Do
        l = InStr(l + 1, t$, ")"): If l Then c1 = c1 + 1
    Loop Until l = 0
    If c <> c1 Then e$ = "ERROR -- Bad Parenthesis:" + Str$(c) + "( vs" + Str$(c1) + ")": Exit Sub

    'Modify so that NOT will process properly
    l = 0
    Do
        l = InStr(l + 1, t$, "NOT")
        If l Then
            'We need to work magic on the statement so it looks pretty.
            ' 1 + NOT 2 + 1 is actually processed as 1 + (NOT 2 + 1)
            'Look for something not proper
            l1 = InStr(l + 1, t$, "AND")
            If l1 = 0 Or (InStr(l + 1, t$, "OR") > 0 And InStr(l + 1, t$, "OR") < l1) Then l1 = InStr(l + 1, t$, "OR")
            If l1 = 0 Or (InStr(l + 1, t$, "XOR") > 0 And InStr(l + 1, t$, "XOR") < l1) Then l1 = InStr(l + 1, t$, "XOR")
            If l1 = 0 Or (InStr(l + 1, t$, "EQV") > 0 And InStr(l + 1, t$, "EQV") < l1) Then l1 = InStr(l + 1, t$, "EQV")
            If l1 = 0 Or (InStr(l + 1, t$, "IMP") > 0 And InStr(l + 1, t$, "IMP") < l1) Then l1 = InStr(l + 1, t$, "IMP")
            If l1 = 0 Then l1 = Len(t$) + 1
            t$ = Left$(t$, l - 1) + "(" + Mid$(t$, l, l1 - l) + ")" + Mid$(t$, l + l1 - l)
            l = l + 3
            'PRINT t$
        End If
    Loop Until l = 0

    'Check for bad operators before a ( bracket
    l = 0
    Do
        l = InStr(l + 1, t$, "(")
        If l And l > 2 Then 'Don't check the starting bracket; there's nothing before it.
            good = 0
            For i = 1 To UBound(OName)
                If Mid$(t$, l - Len(OName(i)), Len(OName(i))) = OName(i) And PL(i) > 1 And PL(i) <= 250 Then good = -1: Exit For 'We found an operator after our ), and it's not a CONST (like PI)
            Next
            If Not good Then e$ = "ERROR - Improper operations before (.": Exit Sub
            l = l + 1
        End If
    Loop Until l = 0

    'Check for bad operators after a ) bracket
    l = 0
    Do
        l = InStr(l + 1, t$, ")")
        If l And l < Len(t$) Then
            good = 0
            For i = 1 To UBound(OName)
                If Mid$(t$, l + 1, Len(OName(i))) = OName(i) And PL(i) > 1 And PL(i) <= 250 Then good = -1: Exit For 'We found an operator after our ), and it's not a CONST (like PI)
            Next
            If Mid$(t$, l + 1, 1) = ")" Then good = -1
            If Not good Then e$ = "ERROR - Improper operations after ).": Exit Sub
            l = l + 1
        End If
    Loop Until l = 0 Or l = Len(t$) 'last symbol is a bracket

    'Turn all &H (hex) numbers into decimal values for the program to process properly
    l = 0
    Do
        l = InStr(t$, "&H")
        If l Then
            E = l + 1: finished = 0
            Do
                E = E + 1
                comp$ = Mid$(t$, E, 1)
                Select Case comp$
                    Case "0" To "9", "A" To "F" 'All is good, our next digit is a number, continue to add to the hex$
                    Case Else
                        good = 0
                        For i = 1 To UBound(OName)
                            If Mid$(t$, E, Len(OName(i))) = OName(i) And PL(i) > 1 And PL(i) <= 250 Then good = -1: Exit For 'We found an operator after our ), and it's not a CONST (like PI)
                        Next
                        If Not good Then e$ = "ERROR - Improper &H value. (" + comp$ + ")": Exit Sub
                        E = E - 1
                        finished = -1
                End Select
            Loop Until finished Or E = Len(t$)
            t$ = Left$(t$, l - 1) + LTrim$(RTrim$(Str$(Val(Mid$(t$, l, E - l + 1))))) + Mid$(t$, E + 1)
        End If
    Loop Until l = 0

    'Turn all &B (binary) numbers into decimal values for the program to process properly
    l = 0
    Do
        l = InStr(t$, "&B")
        If l Then
            E = l + 1: finished = 0
            Do
                E = E + 1
                comp$ = Mid$(t$, E, 1)
                Select Case comp$
                    Case "0", "1" 'All is good, our next digit is a number, continue to add to the hex$
                    Case Else
                        good = 0
                        For i = 1 To UBound(OName)
                            If Mid$(t$, E, Len(OName(i))) = OName(i) And PL(i) > 1 And PL(i) <= 250 Then good = -1: Exit For 'We found an operator after our ), and it's not a CONST (like PI)
                        Next
                        If Not good Then e$ = "ERROR - Improper &B value. (" + comp$ + ")": Exit Sub
                        E = E - 1
                        finished = -1
                End Select
            Loop Until finished Or E = Len(t$)
            bin$ = Mid$(t$, l + 2, E - l - 1)
            For i = 1 To Len(bin$)
                If Mid$(bin$, i, 1) = "1" Then f = f + 2 ^ (Len(bin$) - i)
            Next
            t$ = Left$(t$, l - 1) + LTrim$(RTrim$(Str$(f))) + Mid$(t$, E + 1)
        End If
    Loop Until l = 0

    t$ = N2S(t$)
    VerifyString t$

    e$ = t$
End Sub



Sub VerifyString (t$)
    'ERROR CHECK for unrecognized operations
    j = 1
    Do
        comp$ = Mid$(t$, j, 1)
        Select Case comp$
            Case "0" To "9", ".", "(", ")": j = j + 1
            Case Else
                good = 0
                For i = 1 To UBound(OName)
                    If Mid$(t$, j, Len(OName(i))) = OName(i) Then good = -1: Exit For 'We found an operator after our ), and it's not a CONST (like PI)
                Next
                If Not good Then t$ = "ERROR - Bad Operational value. (" + comp$ + ")": Exit Sub
                j = j + Len(OName(i))
        End Select
    Loop Until j > Len(t$)
End Sub

Function N2S$ (exp$) 'scientific Notation to String
    t$ = LTrim$(RTrim$(exp$))
    If Left$(t$, 1) = "-" Then sign$ = "-": t$ = Mid$(t$, 2)

    dp = InStr(t$, "D+"): dm = InStr(t$, "D-")
    ep = InStr(t$, "E+"): em = InStr(t$, "E-")
    check1 = Sgn(dp) + Sgn(dm) + Sgn(ep) + Sgn(em)
    If check1 < 1 Or check1 > 1 Then N2S = exp$: Exit Function 'If no scientic notation is found, or if we find more than 1 type, it's not SN!

    Select Case l 'l now tells us where the SN starts at.
        Case Is < dp: l = dp
        Case Is < dm: l = dm
        Case Is < ep: l = ep
        Case Is < em: l = em
    End Select

    l$ = Left$(t$, l - 1) 'The left of the SN
    r$ = Mid$(t$, l + 1): r&& = Val(r$) 'The right of the SN, turned into a workable long


    If InStr(l$, ".") Then 'Location of the decimal, if any
        If r&& > 0 Then
            r&& = r&& - Len(l$) + 2
        Else
            r&& = r&& + 1
        End If
        l$ = Left$(l$, 1) + Mid$(l$, 3)
    End If

    Select Case r&&
        Case 0 'what the heck? We solved it already?
            'l$ = l$
        Case Is < 0
            For i = 1 To -r&&
                l$ = "0" + l$
            Next
            l$ = "0." + l$
        Case Else
            For i = 1 To r&&
                l$ = l$ + "0"
            Next
    End Select

    N2S$ = sign$ + l$
End Function



[Image: image.png]


RE: Math Evaluator - MasterGy - 12-12-2022

This must have been a lot of work! very good ! It could be part of something! someone will need it! He doesn't like negative square roots.


RE: Math Evaluator - SMcNeill - 12-12-2022

(12-12-2022, 03:30 PM)MasterGy Wrote: This must have been a lot of work! very good ! It could be part of something! someone will need it! He doesn't like negative square roots.

It is a part of something, and *YOU* -- and the rest of the QB64 user base -- have been using it for ages, without even knowing it. Wink Every time you type CONST in the IDE, it uses this math evaluator to get the answer to what you type, before assigning it to your CONST value.

Process is basically like this:

CONST foo = _RGB32(255, 255, 255)  <-- you type that in the IDE.

Evaluate_Expression takes that "_RGB32(255, 255, 255)" and solves it, substituting it into your CONST foo.

Anytime you use foo in your code after that, it uses the value the math evaluator came up with.  Smile

Feel free to plug it in anywhere you need to do math stuffs -- this one has been stress tested by our user base for YEARS now.  Its results speak for themselves.  I won't swear it's 100% glitch free, but if it was excessively glitchy, we'd see folks posting bug reports on the forum about it everyday -- and that's just not happening.  Wink