CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Question Why does this loop turn out negatives?

    Ok so i am using a loop to make a large array of additions.

    the loop takes all of the values before it and adds them together, then saves them into the array.

    (in this case the short had would be to multiply the preceding number by 2)

    But for some reason near the end (the 29th spot to be exact) the number is negative, while still being the correct absolute value. Then it just turns 0's

    The referancing of the array and array places is correct, and there is no subtraction, of refrencing of places that do not exist.

    so why is place 29 negative?

    1 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 -2147483648 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <fstream>
    
    using namespace std;
    
    int as = 100;
    int state;
    int loop;
    int pre;
    int array[100];
    
    int main(int argc, char *argv[])
    {
    ofstream ST("Out.txt", ios::out | ios::app);
    for(state = 0; state != as; state++)
    {
               array[0] = 1;
               pre = 0;
    for(loop = 0; loop != state; loop++)
               {
              pre = array[loop] + pre;
              }
              array[state] = pre;
    }
    for(pre = 0; pre != as; pre++)
    {
    ST << array[pre] << " ";
    cout << array[pre] << " ";
    }          
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by H aun; March 21st, 2009 at 02:57 PM.

  2. #2
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Why does this loop turn out negatives?

    Your variable overflowed. Try using floating point if possible, or check out a big number library, which can handle infinitely large numbers

    Additional info:
    Variable overflow occurs when you try to increment a variable to a value too large for it to hold in it's limited memory space, which for a 32 bit integer, is 2147483647. Making your variables unsigned will take it up to 4294967295, but even that will run out quickly.
    If you use a floating point variable, such as double, you'll still have a finite number of significant digits, but by using scientific notation, it can understand values of relatively gargantuan sizes.
    Check out: http://gmplib.org/
    Last edited by Etherous; March 21st, 2009 at 03:05 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  3. #3
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Re: Why does this loop turn out negatives?

    so is that what caused it to be negative

  4. #4
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Why does this loop turn out negatives?

    Yes. How signed variables are stored in memory, I think the negative series is actually usually higher than the positive series in term of binary/hexadecimal. So when it overflows, it goes into the negative range. Your best bet is to use one of the alternate methods I gave above.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  5. #5
    Join Date
    Apr 2003
    Posts
    1,755

    Re: Why does this loop turn out negatives?

    Signed numbers (e.g. long, int, short, char) take the most significant bit (MSb) as the sign indicator. When the MSb is 0, it is considered positive and when it is 1 (binary 1), it is considered negative.

    In your case, your number became 0x80000000 (hexadecimal value) and this makes your number to be considered negative. The actual value can be computed by getting the two’s compliment on the 31 remaining bits.

    Hope it will help you.

    Edit: You could use unsigned number (unsigned int for example) so you can use the whole 32 bit value. For your 100 loops, you might need a variable type that can handle 100 bits.
    Last edited by rxbagain; March 21st, 2009 at 08:54 PM.

  6. #6
    Join Date
    Jan 2009
    Location
    IN my computer,
    Posts
    24

    Re: Why does this loop turn out negatives?

    for the gmp library, how would i install that into Dev C++, windows Vista?

  7. #7
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Why does this loop turn out negatives?

    1) However you tell Dev-C++ to link with libraries, link with the libgmp (AND libgmpxx if using C++) libraries
    2) Wherever your gmp headers were installed, include them with '#include gmp.h' OR '#include gmpxx.h' if using C++.
    3) Follow GMP documentation from that point
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

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

Featured