# simple C++ question about floats

**URL:** <https://boards.straightdope.com/t/simple-c-question-about-floats/378338>\
**Category:** Factual Questions\
**Created:** [October 29, 2006, 4:46pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338 "2006-10-29T16:46:48Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [October 30, 2006, 8:19am UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/21 "2006-10-30T08:19:03Z")

</div>

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

```auto

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!)

---

<div class="post-metadata">

**Author:** ![Stealth\_Potato](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Stealth\_Potato](https://boards.straightdope.com/u/Stealth_Potato)\
**Post date:** [October 30, 2006, 8:29am UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/22 "2006-10-30T08:29:52Z")

</div>

Whoa there, **Derleth**. I gave my example in that form because that was the form the OP used. 😉 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. 😃

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

---

<div class="post-metadata">

**Author:** ![ftg](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ftg/32/2801_2.png) [@ftg](https://boards.straightdope.com/u/ftg)\
**Post date:** [October 30, 2006, 3:26pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/23 "2006-10-30T15:26:55Z")

</div>

Sigh.

---

<div class="post-metadata">

**Author:** ![Gozu](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/gozu/32/6615_2.png) [@Gozu](https://boards.straightdope.com/u/Gozu)\
**Post date:** [October 30, 2006, 3:41pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/24 "2006-10-30T15:41:38Z")

</div>

[QUOTE=ftg]  
You should also learn how to figure out how to code loops correctly. Hint: replace 1000 with 0 in the while statement and see how many times the loop runs.

Haven’t learned about “for” statements yet I take it.  
[/QUOTE]

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

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

---

<div class="post-metadata">

**Author:** ![Trunk](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@Trunk](https://boards.straightdope.com/u/Trunk)\
**Post date:** [October 30, 2006, 5:09pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/25 "2006-10-30T17:09:58Z")

</div>

Do it like this

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Trunk](https://avatars.discourse-cdn.com/v4/letter/t/87869e/32.png) [@Trunk](https://boards.straightdope.com/u/Trunk)\
**Post date:** [October 30, 2006, 5:12pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/26 "2006-10-30T17:12:01Z")

</div>

Never mind.

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

```auto

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

```

and submitted too soon.

---

<div class="post-metadata">

**Author:** ![UncleRojelio](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/unclerojelio/32/3160_2.png) [@UncleRojelio](https://boards.straightdope.com/u/UncleRojelio)\
**Post date:** [October 30, 2006, 5:32pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/27 "2006-10-30T17:32:39Z")

</div>

Anyhoo, the upshot of all this:

```auto

#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;
}

```

---

<div class="post-metadata">

**Author:** ![Mbossa](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Mbossa](https://boards.straightdope.com/u/Mbossa)\
**Post date:** [October 30, 2006, 5:34pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/28 "2006-10-30T17:34:39Z")

</div>

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

```auto

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

---

<div class="post-metadata">

**Author:** ![pulykamell](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pulykamell/32/3166_2.png) [@pulykamell](https://boards.straightdope.com/u/pulykamell)\
**Post date:** [October 30, 2006, 5:37pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/29 "2006-10-30T17:37:38Z")

</div>

or how about:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Una\_Persson](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/una_persson/32/346_2.png) [@Una\_Persson](https://boards.straightdope.com/u/Una_Persson)\
**Post date:** [October 30, 2006, 5:44pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/30 "2006-10-30T17:44:33Z")

</div>

[QUOTE=Derleth]  
Easier for everyone to read, nobody wonders why you did it in an odd fashion.  
[/QUOTE]

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.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [October 30, 2006, 6:18pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/31 "2006-10-30T18:18:21Z")

</div>

> [@](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Stealth\_Potato](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Stealth\_Potato](https://boards.straightdope.com/u/Stealth_Potato)\
**Post date:** [October 30, 2006, 7:26pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/32 "2006-10-30T19:26:30Z")

</div>

[QUOTE=ftg]  
Sigh.  
[/QUOTE]  
Oops, looks like I missed your correction; you beat me to it. And here I thought I had read the entire thread. :smack:

---

<div class="post-metadata">

**Author:** ![Shalmanese](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Shalmanese](https://boards.straightdope.com/u/Shalmanese)\
**Post date:** [October 30, 2006, 8:11pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/33 "2006-10-30T20:11:56Z")

</div>

I prefer

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Derleth](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Derleth](https://boards.straightdope.com/u/Derleth)\
**Post date:** [October 30, 2006, 11:22pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/34 "2006-10-30T23:22:25Z")

</div>

[QUOTE=Una Persson]  
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.  
[/QUOTE]  
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. 😉

---

<div class="post-metadata">

**Author:** ![Una\_Persson](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/una_persson/32/346_2.png) [@Una\_Persson](https://boards.straightdope.com/u/Una_Persson)\
**Post date:** [October 31, 2006, 1:58am UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/35 "2006-10-31T01:58:03Z")

</div>

[QUOTE=Derleth]  
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.  
[/QUOTE]

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

---

<div class="post-metadata">

**Author:** ![Gozu](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/gozu/32/6615_2.png) [@Gozu](https://boards.straightdope.com/u/Gozu)\
**Post date:** [November 1, 2006, 3:58am UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/36 "2006-11-01T03:58:46Z")

</div>

[QUOTE=pulykamell]  
or how about:

```auto

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

```

[/QUOTE]

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

---

<div class="post-metadata">

**Author:** ![CandidGamera](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/candidgamera/32/2878_2.png) [@CandidGamera](https://boards.straightdope.com/u/CandidGamera)\
**Post date:** [November 1, 2006, 4:11am UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/37 "2006-11-01T04:11:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pulykamell](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pulykamell/32/3166_2.png) [@pulykamell](https://boards.straightdope.com/u/pulykamell)\
**Post date:** [November 1, 2006, 5:28am UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/38 "2006-11-01T05:28:14Z")

</div>

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

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:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [November 1, 2006, 7:06pm UTC](https://boards.straightdope.com/t/simple-c-question-about-floats/378338/39 "2006-11-01T19:06:59Z")

</div>

> [@](#):
>
> 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.

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.

[Previous page](https://boards.straightdope.com/t/simple-c-question-about-floats/378338.md?page=1)
