Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite Loops: int vs. float

I'm working on a homework assignment and am probably psyching myself out about this thing a little too much, so I am just seeking some input. Here's the basic code:

for(x = 100; x > 0; x = x + x) {
    sum = sum + x;

There are two versions: one where x is a float and one where it is an int. The question is are these infinite loops.

I am thinking that when x is an int, it will eventually overflow, making it less than zero and the loop will stop. When x is a float, x will reach infinity and the loop will be infinite.

Am I close?

like image 254
SmileFactory Avatar asked Jun 11 '11 00:06

SmileFactory


People also ask

Why float is not used in loop?

All simple fractions exactly. All decimals precisely, even when the decimals can be represented in a small number of digits. All digits of large values, meaning that incrementing a large floating-point value might not change that value within the available precision.

How do you know if a loop is infinite?

An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over.

Should infinite loops be avoided?

An infinite loop can crash your program or browser and freeze your computer. To avoid such incidents it is important to be aware of infinite loops so that we can avoid them.

Is it good to use infinite loop?

Infinite loops are useful if you want your script (or part of the code) to run endlessly, until a specific action is taken. Especially, when there can be more than one action that stops your script or block of code from working. In event-based programming paradigm endless loops are very common.


3 Answers

The behavior when a signed integer is increased beyond its limit is undefined. So the loop may end or it may be infinite. Or it may crash (or the loop may never run at all). Or as some C gurus like to say, demons may fly out of your nose - though personally I doubt any compiler implementor would go through the trouble of implementing nasal demon functionality.

As far as floating point values are concerned, you are correct that it will be an infinite loop.

like image 85
sepp2k Avatar answered Nov 14 '22 23:11

sepp2k


When signed integer overflows, the behavior is undefined. Expecting that x will become negative is naive at best.

Some compilers (like GCC) actually implement so called strict value semantics, which means that the compiler takes advantage of that undefined behavior for optimization purposes. In your specific example, the compiler might immediately generate a straightforward infinite loop, i.e. a loop that doesn't have any termination condition at all.

like image 33
AnT Avatar answered Nov 15 '22 01:11

AnT


You are indeed correct, integers will overflow to negative values (as long as they're signed) so the loop will end, and floats will stick to "+infinity" which is always greater than any number except NaN.

Edit: I stand corrected, the int version does loop infinitely (on some compilers due to their assumptions): http://ideone.com/HZkht

like image 42
Blindy Avatar answered Nov 15 '22 00:11

Blindy