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

1. 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. 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

3. #define dx ((float)b-a)/NUM
Otherwise the calculation becomes an integer calculation, thus resulting in 0.

4. Your #defines are being cast to int by the compiler. Variable dx ends up being zero every loop.

Todd

5. 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.

6. You are right - poor choice of words. Is it correct to say they are "defined" as ints?

7. 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.

8. 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

9. 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. Because dx is an int, not a double.
#define a -1.0
#define b 1.0
Should make dx a double.

11. I've defined dx as a double, and yet it still does it.

12. (1/6) will result in zero.

13. 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

14. 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.