dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Console shows negative integer in addition test

  1. #1
    Join Date
    Sep 2016
    Posts
    3

    Console shows negative integer in addition test

    Hi,

    Code:
    int test = 42443;
    int next = 45454;
    		
    for (int i = 0; i < 1000000; i++)
    {
    	next += test;
    }
    		
    System.out.println(next);
    My console in Eclipse shows -506627506

    Any ideas? If I change the loop count to 1000000000, then the console now prints a positive value: 133226382

    Bit confused as to what is going on here.

    I'm fairly new to Java so just checking.

    Thanks in advance.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,924

    Re: Console shows negative integer in addition test

    You're overflowing the maximum size of the positive number that can be stored as a signed int number. This is 2,147,483,647. See http://stackoverflow.com/questions/1...lue-of-integer If a positive number greater than 2,147,483,647 is tried to be stored then this is treated as a negative number.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  3. #3
    Join Date
    Sep 2016
    Posts
    3

    Re: Console shows negative integer in addition test

    Thank you for the response. Just another question, if this is due to an overflow, then I don't understanding why changing the loop count from 1000000 to 1000000000 results in a positive value? Surely if the former is overflowing, the latter should too?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,924

    Re: Console shows negative integer in addition test

    It changes from positive to negative to positive to negative to positive.... Overflowing a positive value makes it negative and then overflowing a negative value makes it positive.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)