Thread: For some reason, this makes an infinite loop, but I can't see why

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    For some reason, this makes an infinite loop, but I can't see why

    For some reason, the following code generates an infinite loop, but I can't see why. Also, the output to the screen for values of x gives -1 for all values, which isn't right.

    Code:
    #define a       -1
    #define b       1
    #define NUM   1000
    #define dx      (b-a)/NUM
     
    int main()
    {
    float x;
    x = a;
     
    while(x<=b)
    {
    // Call a few functions etc
    printf("%f\n", x);
    x = x+dx;
    }
    }


  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you need to make dx a float calculation (e.g. cast the (b-a) into a float, or make the constant 1000 into 1000.0f).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    #define dx ((float)b-a)/NUM
    Otherwise the calculation becomes an integer calculation, thus resulting in 0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Your #defines are being cast to int by the compiler. Variable dx ends up being zero every loop.

    Todd

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not cast... they're interpreted as ints, since 1, 2, 3, 4, 5, etc = int.
    1.0f = float and 1.0 = double.
    If all the numbers are integer, it will do an integer computation (naturally, since there's no need for floating point). But if you make at least one of the numbers a float or double - vo&#237;la, the result is floating point operation, which is what you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You are right - poor choice of words. Is it correct to say they are "defined" as ints?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I do know that it's better to say they're interpreted as ints, since that's how the compilers work. If it sees a numeric value, then it interprets it as a int, float or double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Being utterly pedantic: The numbers in the define are not interpreted, cast or otherwise "dealt with". The preprocessor will replace all "a" with "-1", all "b" with "1", etc. The preprocessor itself has no concept of numbers vs. other "content" - it is simply a text replacement process.

    However, the compiler will use "the simplest form that can accommodate the number given", and the compiler will do more than int, float, double, it also distinguishes [when necessary] between unsigned and signed, short and long integers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    wow. You guys are quick to get back, lol. Cheers guys. 1 more quick question (saves me from starting a new thread, lol). I calculate doubles k1,k2,k3 and k4 in my code, and i i want to calculate the double k5 as so:
    Code:
    k5 = (1/6)*dx*(k1+2*(k2+k3)+k4);
    so in my loop, I have:
    Code:
    while(...)
    {
    func();                          // Function calculates k1,k2,k3,k4
    k5 = (1/6)*dx*(k1+2*(k2+k3)+k4);
    }
    but for some reason k5 is always 0. I have made sure that both of them are doubles, so thats not the problem.


  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because dx is an int, not a double.
    #define a -1.0
    #define b 1.0
    Should make dx a double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    I've defined dx as a double, and yet it still does it.


  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    (1/6) will result in zero.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, when the compiler decides how to perform calculatins, it looks at the CURRENT expression, e.g. (1/6) and calculates that using the simplest possible types that can describe the expression, in this case, integers. 1/6 in integer is 0. You can work around this - the simplest way is to add a .0 on the end of the 1 and 6 [strictly speaking, you only need to add .0 to ONE of the numbers, but it looks neater as "1.0/6.0" than "1/6.0" or "1.0/6" - at least that's what I think]. If you add a decimal point to the number, the compiler WILL have to use a floating point type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's also a thing. Since the calculation is in parenthesis, it will be calculated before other calculations. But both numbers are ints, so it performs an integer division which results in 0 and the rest you know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  4. Why does the loop in my program turn infinite?
    By DaniiChris in forum C Programming
    Replies: 6
    Last Post: 07-08-2008, 02:44 AM
  5. Infinite Loop!!!
    By catmom in forum C++ Programming
    Replies: 9
    Last Post: 12-11-2001, 10:44 AM