simple C++ question about floats

Don’t code the loop like Stealth Potato said. Code it like this:


for (i = 0; i < 1000; i++) {
   /* stuff */
}

Easier for everyone to read, nobody wonders why you did it in an odd fashion.

(And my code formatting is better, too. Nyah!)

Whoa there, Derleth. I gave my example in that form because that was the form the OP used. :wink: I didn’t want to change everything around when I was only pointing out a problem with the comparison, and not a problem with the style.

And I’ve been next-lining my opening braces for a decade, and I intend to do so until the day I die. But I think this would be better material for GD. :smiley:

Really, I’m astonished at how strongly I feel about brace placement…

Sigh.

I have, actually (and it’s my favorite loop) . As stated previously, that isn’t my code. The number of iterations was not important in this case so I didn’t bother checking. Thanks though.

And to everybody else, I’m utterly amazed at the number of answers I got. I guess the SDMB is crawling with programmers :slight_smile: .

Also, a friend of mine also recommended me the book “Thinking in C++”, I’ll buy it.

Do it like this



i = 0;
while (true)
   {
   x = x + .0001;
i++
   }


Never mind.

As an example of a terrible way to write a loop I was trying to write



i = 0;
while (true)
   {
   i++;
   if (i > 1000) break;
   }


and submitted too soon.

Anyhoo, the upshot of all this:



#include <iostream>

int main(void) {
	float x = 1.0;
	for (int i = 0; i < 1000; i++) {
              std::cout << "x = " << x << std::endl;
              x = x + .0001;
        }
	return 0;
}


Nope, this is the way the cool kids write their loops:


float x = 1.0;
int i = 1;
loopstart:
cout << "x = " << x << endl;
if (i % 1000 == 0) goto loopend;
x = x + .0001;
goto loopstart;
loopend:

Disclaimer: Don’t do this

or how about:



int i = 1000;
while (i)
{
    x=x+.0001;
    i--;
}


That’s your opinion; there are others. “Braces” wars are something that people who code professionally try to avoid no matter what the case. I recommend that the OP organize his braces first in the way which is required by the project/class, and if no requirement is made, then they should discover what they can read better than anyone else in practice.

Right. How exactly you position your braces and indentation doesn’t matter to the compiler. It might matter to you, your instructor, or other humans who read the code.

Oops, looks like I missed your correction; you beat me to it. And here I thought I had read the entire thread. :smack:

I prefer




for (int i = 0; i < 1000; [increment_by_one(i)](http://boards.straightdope.com/sdmb/showthread.php?t=370866))
{
    ...
}



I was joking about the brace placement. I understand about Holy Wars (Hey, kids, vi or Emacs? Linux or BSD or Solaris or VMS? BSD or System V or was Seventh Edition the Last Real Unix? Are you an ITS troglodyte or a Unix weenie? Dvorak or QWERTY, and does it really make a difference?) and I was attempting an inside joke.

So, if you were explaining my joke please excuse this digression.

ftg: Good point bear repeating, bad points exist at the top of my head. :wink:

I think we are/were in agreement, no problems.

I like this one the best. It seems any value other than zero counts as “TRUE” then? I didn’t know that.

Once you spend a few years around IT, you’ll learn… they’re all floats down here.

I’m only a very casual programmer, but any non-zero value is TRUE, as far as I understand it. I wanted to be extra clever, and see if I could stick a float or double in there and use only one variable, with while (1-x) doing the loop, but that never evaluates to false it seems. Even though with a double, x does show a value of “1” after 1000 iterations of the loop, (1-x) does not evaluate to FALSE.

Perhaps someone can explain why.

:smack: I just figured out the extra-clever and esoteric way of doing it. Duh. Forgot about casting:




float x;
while (1 - (int) x)
{
    x += .0001;
}



What it’s showing you is rounded. The actual value probably won’t ever be quite exactly equal to 1, so 1 minus the value won’t ever be quite exactly equal to 0.