Results 1 to 18 of 18

Thread: [RESOLVED] Improper calculation

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Resolved [RESOLVED] Improper calculation

    Hi,
    I am having issues with expression evaluator - it gives me incorrect results. For example - 1+2+3+4+5+6+7+8+9*10+11+12+13+14+15+16+17+18+19*20 etc up to 200 gives me correct result 265181.

    But when I have tried to express only half and then another half of expression - 1...100 is 33019
    Then I typed 33019+101+102+103+104 etc up to 200 according to pattern, it gives me incorrect result - 3557009.

    How can it be solved?

    EE is rewritten for GUI by myself from this thread as passel suggested it
    Last edited by VB.NET Developer; Nov 11th, 2021 at 03:14 PM. Reason: small typo
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Improper calculation

    If you are truly limiting yourself to simple operators, just use DataTable.Compute method (documentation).

    Here is an example of it being used:
    Code:
    Dim expression As String = "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 * 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 * 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 * 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 * 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 * 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 * 60 + 61 + 62 + 63 + 64 + 65 + 66 + 67 + 68 + 69 * 70 + 71 + 72 + 73 + 74 + 75 + 76 + 77 + 78 + 79 * 80 + 81 + 82 + 83 + 84 + 85 + 86 + 87 + 88 + 89 * 90 + 91 + 92 + 93 + 94 + 95 + 96 + 97 + 98 + 99 * 100 + 101 + 102 + 103 + 104 + 105 + 106 + 107 + 108 + 109 * 110 + 111 + 112 + 113 + 114 + 115 + 116 + 117 + 118 + 119 * 120 + 121 + 122 + 123 + 124 + 125 + 126 + 127 + 128 + 129 * 130 + 131 + 132 + 133 + 134 + 135 + 136 + 137 + 138 + 139 * 140 + 141 + 142 + 143 + 144 + 145 + 146 + 147 + 148 + 149 * 150 + 151 + 152 + 153 + 154 + 155 + 156 + 157 + 158 + 159 * 160 + 161 + 162 + 163 + 164 + 165 + 166 + 167 + 168 + 169 * 170 + 171 + 172 + 173 + 174 + 175 + 176 + 177 + 178 + 179 * 180 + 181 + 182 + 183 + 184 + 185 + 186 + 187 + 188 + 189 * 190 + 191 + 192 + 193 + 194 + 195 + 196 + 197 + 198 + 199 * 200"
    Dim table As New DataTable()
    Dim result As Integer = Convert.ToInt32(table.Compute(expression, String.Empty))
    Fiddle: https://dotnetfiddle.net/H0DYy8
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Improper calculation

    Quote Originally Posted by dday9 View Post
    If you are truly limiting yourself to simple operators, just use DataTable.Compute
    That is not an option, since I have EE with BigInteger - I will be using it also.
    Problem is that I have a long list of strings for EE and computing LONG list of even small values takes veeeery long. Therefore I have decided to split these into parts, compute for example half of it and finally use addition to resulting small integers. But the result does not match...
    Last edited by VB.NET Developer; Nov 11th, 2021 at 04:58 PM.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Improper calculation

    Looks like an operator precedence problem.
    The order of operations is BIDMAS:- Brackets, Indices, Division, Multiplication, Addition, Subtraction
    What do you expect?…

    1+2+3+4+5+6+7+8+9*10 = 126

    Whereas…

    (1+2+3+4+5+6+7+8+9)*10 = 450

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Improper calculation

    If that’s not an option then use my expression evaluator in the codebank.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Improper calculation

    Thanks, I have successfully rewritten your EE to GUI, but it does not work...
    original expression
    Code:
    1+2+3+4+5+6+7+8+9+10*11+12+13+14+15+16+17+18+19+20*21+22+23+24+25+26+27+28+29+30*31+32+33+34+35+36+37+38+39+40*41+42+43+44+45+46+47+48+49+50*51+52+53+54+55+56+57+58+59+60*61+62+63+64+65+66+67+68+69+70*71+72+73+74+75+76+77+78+79+80*81+82+83+84+85+86+87+88+89+90*91+92+93+94+95+96+97+98+99+100*101+102+103+104+105+106+107+108+109+110*111+112+113+114+115+116+117+118+119+120*121+122+123+124+125+126+127+128+129+130*131+132+133+134+135+136+137+138+139+140*141+142+143+144+145+146+147+148+149+150*151+152+153+154+155+156+157+158+159+160*161+162+163+164+165+166+167+168+169+170*171+172+173+174+175+176+177+178+179+180*181+182+183+184+185+186+187+188+189+190*191+192+193+194+195+196+197+198+199+200
    produces 265181

    half of expression
    Code:
    1+2+3+4+5+6+7+8+9+10*11+12+13+14+15+16+17+18+19+20*21+22+23+24+25+26+27+28+29+30*31+32+33+34+35+36+37+38+39+40*41+42+43+44+45+46+47+48+49+50*51+52+53+54+55+56+57+58+59+60*61+62+63+64+65+66+67+68+69+70*71+72+73+74+75+76+77+78+79+80*81+82+83+84+85+86+87+88+89+90*91+92+93+94+95+96+97+98+99+100*
    produces 33091.

    This expression
    Code:
    33091*101+102+103+104+105+106+107+108+109+110*111+112+113+114+115+116+117+118+119+120*121+122+123+124+125+126+127+128+129+130*131+132+133+134+135+136+137+138+139+140*141+142+143+144+145+146+147+148+149+150*151+152+153+154+155+156+157+158+159+160*161+162+163+164+165+166+167+168+169+170*171+172+173+174+175+176+177+178+179+180*181+182+183+184+185+186+187+188+189+190*191+192+193+194+195+196+197+198+199+200*
    produces 3564281
    and it should produce original 265181.
    Last edited by VB.NET Developer; Nov 12th, 2021 at 11:55 AM.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Improper calculation

    No, it should NOT produce the original 265181. The very first operation is this:

    33091 * 101.

    That, alone, is 3342191, which is FAR larger than 265181.

    Did you get that operator wrong? Did you mean to have a + instead of a * as that first operator?

    You need to do a bit of debugging on this code. I don't know what expression evaluator you are using, but if you can step through the code, you would see right away that the first multiplication takes the value to far higher than what you were expecting it to be.

    I also see further multiplication signs in there, which will serve to do some very strange things. I have no idea what you are doing, but it's hard to imagine a scenario where you are adding sequences of numbers like this, except that you then multiply together two numbers at various places. Just figuring out whether or not you are getting the right answer would be a total nightmare with that calculation. The way it is arranged, it would be horrible to perform by any other means, such as by hand, or using a calculator, so you'd never know whether your results were right, or not. You'd just have results, without having any way to know whether or not they are right. You've already seen that, because a casual glance can show you that your math is wrong.

    After all, when I looked at it, I noticed that the first item was being multiplied by 101. Rather than doing that, I multiplied the first value by 100, which is far easier, because you just add two zeroes to the first item, which gives you 3309100, and that is already higher than what you were expecting. I would expect that the expression evaluator is actually giving you the right answer, but I'd rather write this lengthy reply than do the math to check the result.

    Some problems are too tedious. I wrote a program that solved a certain novel statistical calculation. It took the computer eight hours to process the batch of data, so before I did that, my boss and I would sit down with calculators and work through a single example to check the results of one set of data (the whole batch was LOTS of sets of data). Two of us with calculators NEVER got the math right the first time. In fact, we averaged more than one error per attempt. That meant that the program was very hard to validate.

    You're doing the same kind of thing, on a lesser scale. Proving that you are getting the right/wrong answer for those equations is going to be terribly difficult, as you have already seen. It may well be right, but by hand you did it wrong, so you were expecting the wrong answer.
    My usual boring signature: Nothing

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Improper calculation

    Nope, it does not. You are doing something wrong.

    Look at this fiddle: https://dotnetfiddle.net/7bcpCp

    Original expression produces 300820, first half of the expression produces 33091, first half of the expression multiplied by the second half produces 3564281.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Improper calculation

    Yeah, I didnt noticed that, sorry, but even when I have used
    Code:
    33091+101+102+103+104+105+106+107+108+109+110*111+112+113+114+115+116+117+118+119+120*121+122+123+124+125+126+127+128+129+130*131+132+133+134+135+136+137+138+139+140*141+142+143+144+145+146+147+148+149+150*151+152+153+154+155+156+157+158+159+160*161+162+163+164+165+166+167+168+169+170*171+172+173+174+175+176+177+178+179+180*181+182+183+184+185+186+187+188+189+190*191+192+193+194+195+196+197+198+199+200
    it produces 255282 which is still not the original 265181.
    @dday9
    My code:
    Code:
    Imports System.Text
    Imports System.Drawing
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
                Dim source As String = TextBox1.Text
                If Not String.IsNullOrWhiteSpace(source) Then
                    Dim format As String = FormatExpression(source)
                    Dim rpn() As Token = ShuntingYardAlgorithm(Scan(format))
                    TextBox2.Text = (Evaluate(rpn))
                End If
        End Sub
    
        Private Function FormatExpression(ByVal expression As String) As String
            'Remove all blanks spaces
            Dim format As String = expression.Replace(" ", String.Empty)
    
            'Add a space before and after all parenthesis
            format = format.Replace("(", " ( ").Replace(")", " ) ")
    
            'Add a space before unary operator
            Dim unaryEvaluator As System.Text.RegularExpressions.MatchEvaluator = New System.Text.RegularExpressions.MatchEvaluator(AddressOf ReplaceUnary)
            format = System.Text.RegularExpressions.Regex.Replace(format, "(\+|-|\*|\\|\^)-", unaryEvaluator)
    
            'Add a space before and after any number
            Dim digitEvaluator As System.Text.RegularExpressions.MatchEvaluator = New System.Text.RegularExpressions.MatchEvaluator(AddressOf ReplaceDigits)
            format = System.Text.RegularExpressions.Regex.Replace(format, "(-?[0-9]+(?:\.[0-9]*)?)", digitEvaluator)
    
            'Remove any excess whitespace
            format = System.Text.RegularExpressions.Regex.Replace(format, " {2,}", " ")
    
            'Trim any leading or trailing whitespace
            format = format.Trim()
    
            Return format
        End Function
    
        Private Function ReplaceUnary(ByVal m As System.Text.RegularExpressions.Match) As String
            Return " " & m.Value
        End Function
    
        Private Function ReplaceDigits(ByVal m As System.Text.RegularExpressions.Match) As String
            Return " " & m.Value & " "
        End Function
        Private Function AddNumbers(ByVal operand1 As Double, ByVal operand2 As Double) As Double
            Return operand1 + operand2
        End Function
        Private Function SubtractNumbers(ByVal operand1 As Double, ByVal operand2 As Double) As Double
            Return operand1 - operand2
        End Function
        Private Function MultiplyNumbers(ByVal operand1 As Double, ByVal operand2 As Double) As Double
            Return operand1 * operand2
        End Function
        Private Function DivideNumbers(ByVal operand1 As Double, ByVal operand2 As Double) As Double
            Return operand1 / operand2
        End Function
        Private Function ModuloNumbers(ByVal operand1 As Double, ByVal operand2 As Double) As Double
            Return operand1 Mod operand2
        End Function
        Private Function RaiseNumbers(ByVal operand1 As Double, ByVal operand2 As Double) As Double
            Return operand1 ^ operand2
        End Function
        Private Function Scan(ByVal source As String) As Token()
            'Create the rules of the lexical analyzer
            'Rule 1: Left Parenthesis
            'Rule 2: Right Parenthesis
            'Rule 3: Doubles
            'Rules 4 - 5: Addition and Subtraction, precedence: 1
            'Rules 6 - 8: Multiplication, Division, and MOD, precedence: 2
            'Rule 9: Exponent, precedence: 3
            Dim definitions() As Token = {New Token() With {.Pattern = "^\($", .Type = Token.TokenType.LeftParenthesis, .Value = String.Empty}, _
                             New Token() With {.Pattern = "^\)$", .Type = Token.TokenType.RightParenthesis, .Value = String.Empty}, _
                             New Token() With {.Pattern = "^([-+]?(\d*[.])?\d+)$", .Type = Token.TokenType.Digit, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf AddNumbers), .Pattern = "^\+$", .Precedence = 1, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf SubtractNumbers), .Pattern = "^-$", .Precedence = 1, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf MultiplyNumbers), .Pattern = "^\*$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf DivideNumbers), .Pattern = "^\/$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf ModuloNumbers), .Pattern = "^%$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf RaiseNumbers), .Pattern = "^\^$", .Precedence = 3, .Type = Token.TokenType.Operator, .Value = String.Empty}}
            Dim tokens As List(Of Token) = New List(Of Token)
            'Loop through each item in the source
            For Each item As String In source.Split({" "}, StringSplitOptions.RemoveEmptyEntries)
                Dim currentToken As Token = Nothing
                Dim regex As RegularExpressions.Regex
                'Loop through each rule of the lexical analyzer
                For Each definition As Token In definitions
                    regex = New RegularExpressions.Regex(definition.Pattern)
                    'If the item is a match then add it and exit the loop for the rules of the lexical analyzer
                    If regex.IsMatch(item) Then
                        currentToken = New Token With {.Operation = definition.Operation, .Pattern = definition.Pattern, .Precedence = definition.Precedence, .Type = definition.Type, .Value = item}
                        tokens.Add(currentToken)
                        Exit For
                    End If
                Next
                'If a match was never found then throw and exception
                If currentToken Is Nothing Then
                    Throw New Exception(item & " is not a valid character.")
                End If
            Next
            Return tokens.ToArray
        End Function
        Private Function ShuntingYardAlgorithm(ByVal tokens() As Token) As Token()
            Dim output As List(Of Token) = New List(Of Token)
            Dim operatorStack As Stack(Of Token) = New Stack(Of Token)
            'Loop through each token in the collection
            For Each item As Token In tokens
                If item.Type = Token.TokenType.Digit Then
                    'If the current token is a digit then add it to the output
                    output.Add(item)
                ElseIf item.Type = Token.TokenType.Operator Then
                    'If the current token is an operator then add each operator in the stack to the output until we've reached an operator with a lesser precedence
                    While operatorStack.Count > 0 AndAlso item.Precedence <= operatorStack.Peek.Precedence
                        output.Add(operatorStack.Pop)
                    End While
                    'Add the current token to the stack
                    operatorStack.Push(item)
                ElseIf item.Type = Token.TokenType.LeftParenthesis Then
                    'If the current token is a left parenthesis then add it to the output
                    operatorStack.Push(item)
                ElseIf item.Type = Token.TokenType.RightParenthesis Then
                    Dim flag As Boolean = False
                    'If the current token is a right parenthesis then add all the operators to the output until we've reached a left parenthesis
                    'Once we hit the left parenthesis, just dispose of the left parenthesis and exit the loop
                    While operatorStack.Count > 0 AndAlso flag = False
                        If operatorStack.Peek.Type = Token.TokenType.Operator Then
                            output.Add(operatorStack.Pop)
                        ElseIf operatorStack.Peek.Type = Token.TokenType.LeftParenthesis Then
                            operatorStack.Pop()
                            flag = True
                        End If
                    End While
                    'If a left parenthesis was never found then throw an error because there are too many right parenthesis
                    If flag = False Then
                        Throw New Exception("There are more right parenthesis than there are left parenthesis in the expression.")
                    End If
                End If
            Next
            If operatorStack.Count > 0 Then
                'Loop through each token left in the stack
                Do
                    If operatorStack.Peek.Type = Token.TokenType.Operator Then
                        'If the current token in the stack is an operator then add it to the output
                        output.Add(operatorStack.Pop)
                    ElseIf operatorStack.Peek.Type = Token.TokenType.LeftParenthesis Then
                        'current token in the stack is a left parenthesis then throw an error because there are too many left parenthesis
                        Throw New Exception("There are more left parenthesis than there are right parenthesis in the expression.")
                    End If
                Loop Until operatorStack.Count = 0
            End If
            Return output.ToArray()
        End Function
        Private Function Evaluate(ByVal tokens() As Token) As Double
            Dim valueStack As Stack(Of Token) = New Stack(Of Token)
            'Loop through each token in the collection
            For Each item As Token In tokens
                If item.Type = Token.TokenType.Operator Then
                    'If the current token is an operator then get the top two digits off the stack
                    Dim operand2 As Token = valueStack.Pop
                    Dim operand1 As Token = valueStack.Pop
                    'Add a new token to the stack with the value returned by the math operation performed
                    valueStack.Push(New Token With {.Type = Token.TokenType.Digit, .Value = item.Operation(Double.Parse(operand1.Value), Double.Parse(operand2.Value)).ToString})
                Else
                    'If the current token is a digit, then add it to the stack
                    valueStack.Push(item)
                End If
            Next
            'The last item in the stack will be the final value
            Return Double.Parse(valueStack.Pop.Value)
        End Function
        Public Class Token
            Public Enum TokenType
                Digit
                LeftParenthesis
                [Operator]
                RightParenthesis
            End Enum
            Public Delegate Function MathOperation(ByVal value1 As Double, ByVal value2 As Double) As Double
            Private _operation As MathOperation
            Public Property Operation() As MathOperation
                Get
                    Return _operation
                End Get
                Set(ByVal value As MathOperation)
                    _operation = value
                End Set
            End Property
            Private _pattern As String
            Public Property Pattern() As String
                Get
                    Return _pattern
                End Get
                Set(ByVal value As String)
                    _pattern = value
                End Set
            End Property
            Private _precedence As Integer
            Public Property Precedence() As Integer
                Get
                    Return _precedence
                End Get
                Set(ByVal value As Integer)
                    _precedence = value
                End Set
            End Property
            Private _type As TokenType
            Public Property Type() As TokenType
                Get
                    Return _type
                End Get
                Set(ByVal value As TokenType)
                    _type = value
                End Set
            End Property
            Private _value As String
            Public Property Value() As String
                Get
                    Return _value
                End Get
                Set(ByVal value As String)
                    _value = value
                End Set
            End Property
        End Class
    End Class
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Improper calculation

    That is because of order of operations.
    Code:
    1+2+3+4+5+6+7+8+9+10*11+12+13+14+15+16+17+18+19+20*21+22+23+24+25+26+27+28+29+30*31+32+33+34+35+36+37+38+39+40*41+42+43+44+45+46+47+48+49+50*51+52+53+54+55+56+57+58+59+60*61+62+63+64+65+66+67+68+69+70*71+72+73+74+75+76+77+78+79+80*81+82+83+84+85+86+87+88+89+90*91+92+93+94+95+96+97+98+99+100*101+102+103+104+105+106+107+108+109+110*111+112+113+114+115+116+117+118+119+120*121+122+123+124+125+126+127+128+129+130*131+132+133+134+135+136+137+138+139+140*141+142+143+144+145+146+147+148+149+150*151+152+153+154+155+156+157+158+159+160*161+162+163+164+165+166+167+168+169+170*171+172+173+174+175+176+177+178+179+180*181+182+183+184+185+186+187+188+189+190*191+192+193+194+195+196+197+198+199+200
    Is not the same as:
    Code:
    (1+2+3+4+5+6+7+8+9+10*11+12+13+14+15+16+17+18+19+20*21+22+23+24+25+26+27+28+29+30*31+32+33+34+35+36+37+38+39+40*41+42+43+44+45+46+47+48+49+50*51+52+53+54+55+56+57+58+59+60*61+62+63+64+65+66+67+68+69+70*71+72+73+74+75+76+77+78+79+80*81+82+83+84+85+86+87+88+89+90*91+92+93+94+95+96+97+98+99+100) * (101+102+103+104+105+106+107+108+109+110*111+112+113+114+115+116+117+118+119+120*121+122+123+124+125+126+127+128+129+130*131+132+133+134+135+136+137+138+139+140*141+142+143+144+145+146+147+148+149+150*151+152+153+154+155+156+157+158+159+160*161+162+163+164+165+166+167+168+169+170*171+172+173+174+175+176+177+178+179+180*181+182+183+184+185+186+187+188+189+190*191+192+193+194+195+196+197+198+199+200)
    Which is essentially what you're doing when you "halve" the expression.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Improper calculation

    I have looked via your code and I have tried to alter the precedence from 2 to 1 and vice versa, but no luck.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Improper calculation

    What does that mean? Operator precedence isn't something you can alter. It's part of math.
    My usual boring signature: Nothing

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Improper calculation

    I don't know how to explain why you're getting different results any better than you're getting different results because you're running two fundamentally different expressions.

    If there was not change in operator precedence then you could get away with "halving" the expression. For example "1 + 2 + 3 + 4 + 5 + 6" is the same as "6 + 4 + 5 + 6".
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Improper calculation

    In dday9 s code theres a precedence value.
    Code:
    New Token() With {.Operation = New Token.MathOperation(AddressOf AddNumbers), .Pattern = "^\+$", .Precedence = 1, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf SubtractNumbers), .Pattern = "^-$", .Precedence = 1, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf MultiplyNumbers), .Pattern = "^\*$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf DivideNumbers), .Pattern = "^\/$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf ModuloNumbers), .Pattern = "^%$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf RaiseNumbers), .Pattern = "^\^$", .Precedence = 3, .Type = Token.TokenType.Operator, .Value = String.Empty}}
    But altering the numbers doesnt do anything. So why they are there?
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Improper calculation

    Nope, it does something: https://dotnetfiddle.net/aNoSFy
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Improper calculation

    I mean OPERATOR ( add, subtract, multiply, divide etc.) precedence value (precedence=1, precedence=2 etc.).

    This:
    Code:
    New Token() With {.Operation = New Token.MathOperation(AddressOf SubtractNumbers), .Pattern = "^-$", .Precedence = 1, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf MultiplyNumbers), .Pattern = "^\*$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf DivideNumbers), .Pattern = "^\/$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf ModuloNumbers), .Pattern = "^%$", .Precedence = 2, .Type = Token.TokenType.Operator, .Value = String.Empty}, _
                             New Token() With {.Operation = New Token.MathOperation(AddressOf RaiseNumbers), .Pattern = "^\^$", .Precedence = 3, .Type = Token.TokenType.Operator, .Value = String.Empty}}
    Last edited by VB.NET Developer; Nov 12th, 2021 at 03:30 PM.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Improper calculation

    Still does something. Take a look at this fiddle: https://dotnetfiddle.net/PaBB1U

    Line 50 is commented out whereas line 51 I've swapped precedence values 1 and 2.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Improper calculation

    Finally, I was able to solve my issue. The problem was in expression itself.
    This
    Code:
    (1+2+3+4+5+6+7+8+9+10*11+12+13+14+15+16+17+18+19+20*21+22+23+24+25+26+27+28+29+30*31+32+33+34+35+36+37+38+39+40*41+42+43+44+45+46+47+48+49+50*51+52+53+54+55+56+57+58+59+60*61+62+63+64+65+66+67+68+69+70*71+72+73+74+75+76+77+78+79+80*81+82+83+84+85+86+87+88+89+90*91+92+93+94+95+96+97+98+99+100*101)+(102+103+104+105+106+107+108+109+110*111+112+113+114+115+116+117+118+119+120*121+122+123+124+125+126+127+128+129+130*131+132+133+134+135+136+137+138+139+140*141+142+143+144+145+146+147+148+149+150*151+152+153+154+155+156+157+158+159+160*161+162+163+164+165+166+167+168+169+170*171+172+173+174+175+176+177+178+179+180*181+182+183+184+185+186+187+188+189+190*191+192+193+194+195+196+197+198+199+200)
    produces the desired output 265181.

    @dday9,
    thanks a lot for your help! Really!
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width