Results 1 to 12 of 12

Thread: [2008] Arithmetic operation resulted in an overflow

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    [2008] Arithmetic operation resulted in an overflow

    When the loop is
    For x = 3 to 10
    the codes work fine

    when i change it to 100, the code gives an eroor
    "Arithmetic operation resulted in an overflow."
    on the line "If (Result + PreResult) Mod 2 = 0 Then y = y + Result + PreResult"

    WHY????

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x As Integer, Result As Long, PreResult As Long, oldresult As Long
            Dim y As Long
            Result = 2
            PreResult = 1
            y = 3
            For x = 3 To 100
                If (Result + PreResult) Mod 2 = 0 Then y = y + Result + PreResult
                oldresult = PreResult
                PreResult = Result
                Result = Result + oldresult
            Next
            TextBox1.Text = y
        End Sub
    Im using Visual Studio 2008 Professional Edition
    .Net Framework 3.5

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Arithmetic operation resulted in an overflow

    Quote Originally Posted by perito
    WHY????
    Because 'Result' will hold 7540113804746346429, and 'PreResult' will hold 4660046610375530309. And you're trying to add them together. Thats a pretty big number.

    Besides, the logic in that loop is pretty..odd, what are you trying to accomplish?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Arithmetic operation resulted in an overflow

    Put a breakpoint on the line Result = result + oldresult, then step through the code watching the size of result balloon. That math is not quite a binary expansion, but it is close, because it is similar to n += 2n, which, for any n>1 is a binary sequence, which means that on the 100th iteration, n holds 2^99, but a long integer can only hold about 2^63, so it overflows long before you reach the 100th iteration.
    My usual boring signature: Nothing

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Arithmetic operation resulted in an overflow

    I knew I'd be too slow.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Arithmetic operation resulted in an overflow

    The question is this

    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

    Find the sum of all the even-valued terms in the sequence which do not exceed one million.


    i cant think of a better way to do this...
    please give me new ideas
    Im using Visual Studio 2008 Professional Edition
    .Net Framework 3.5

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Arithmetic operation resulted in an overflow

    But you don't need to go past 1,000,000, and you are currently going FAR beyond that.

    Just add a condition into your loop to see if the value has gone over one million, and exit for if it has. Alternatively, rather than using a For Next loop, since you don't really know how many times you are going to loop, use a Do...Loop. Have x just be a counter in there that gets incremented each time through the loop, and loop until the value exceeds one million. That's a better solution than using the Exit For, though they will do about the same thing.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Arithmetic operation resulted in an overflow

    hmmm....
    i need to loop a million times right? because in the example there are 10 numbers, the sum of the even is 44

    to do that, we loop 10 times, check if the number is even, add it.
    I didnt understand what u mean...
    Do Until y>= 1000000 will dont work...

    please can u explain more
    Im using Visual Studio 2008 Professional Edition
    .Net Framework 3.5

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Arithmetic operation resulted in an overflow

    I read your last post to suggest mean that you are looking for all numbers less than one million in the sequence, rather than looking for the first million numbers in the sequence.

    Do While Y<= 1000000

    would get you pretty close to the result, however it will end with the first value of y that is greater than 1000000, and it looks like you want the result just before that one. Therefore, it seems like a safer loop would be:

    DO

    Loop While (y + result + oldresult <= 1000000)

    Since it appears that as soon as that becomes false, then the next value of y will be greater than 1,000,000, so the current value of y will be the largest one that is less than 1,000,000.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Arithmetic operation resulted in an overflow

    the answer is still wrong :S
    i even tried
    Loop While (y + Result + oldresult <= 1000000)
    y = y - oldresult - PreResult
    and its still wrong.

    Im wondering, the question might be different
    Find the sum of all the even-valued terms in the sequence which do not exceed one million.

    they might mean that the sequence doesnt exceed one million, not the sum. that will result in a different code.
    ideas?
    Im using Visual Studio 2008 Professional Edition
    .Net Framework 3.5

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Arithmetic operation resulted in an overflow

    Oh, I hadn't seen that. Now I get it.

    In y, you are creating the sum of all the values. In Result you are getting the next item in the sequence. At the end of the loop:

    Result: Holds the next item in the sequence (item N).
    PreResult: Holds the last item in the sequence (item N-1).
    OldResult: Holds the second to last item (item N-2).

    So your sequence is correct, but what about your If statement?

    Why are you checking Result + PreResult Mod 2? Result is the next item in the sequence, so shouldn't the if be:

    If Result Mod 2 = 0 Then Y += Result

    Then the loop would be:

    Do

    Loop while Result <= 1000000
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Arithmetic operation resulted in an overflow

    no the final result is (Result + PreResult)
    make the loop until 10 and u will see that
    Result + PreResult = 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
    Result alone is meaningless
    i made the changes and checked anyway and the answer is still wrong...
    Im using Visual Studio 2008 Professional Edition
    .Net Framework 3.5

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

    Re: [2008] Arithmetic operation resulted in an overflow

    It works as it does because you are "looking ahead" at the next result, and adding it in if it belongs, whereas I was suggesting altering it around such that you are looking back. I see that I was out of phase a little, so I wrote it myself:

    vb Code:
    1. Dim Result As Long, PreResult As Long, oldresult As Long
    2.         Dim y As Long
    3.  
    4.         Me.lb1.Items.Clear()
    5.         Me.lbAll.Items.Clear()
    6.         Result = 2
    7.         PreResult = 1
    8.         y = 3
    9.         Do
    10.             oldresult = PreResult
    11.             PreResult = Result
    12.             Result = Result + oldresult
    13.             Me.lbAll.Items.Add(Result)
    14.             If Result Mod 2 = 0 Then
    15.                 y += Result
    16.                 Me.lb1.Items.Add(Result)
    17.             End If
    18.         Loop While Result <= 1000000
    19.  
    20.         lMess.Text = y.ToString

    I put two listboxes on the form, one shows the even results, the other shows ALL the results. The sequence is correct.

    The value of y is: 1,089,155
    The highest result is: 832,040
    My usual boring signature: Nothing

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