# Need loop help

• 04-10-2005
SomeCrazyGuy
Need loop help
I need help to figure out what went wrong... As I am just learning how to program this, I was testing out a 'for' statment:

Code:

```//SomeCrazyGuy //'For' Looping Test #include <iostream.h> #include <stdlib.h> using namespace std; int main () {     float a;         for(a=0;a<=1;a=a+.01) {     cout<< a <<endl; }         system("pause");     return 0; }```
~~~

While the result was find for a short moment, something quite odd happened:

0
.1
.2
.3
...
.81
.82
.83
.839999
.849999
...
.999999
Press any key to continue . . .

Mainly from curiosity, but also from a need, I wish to know how this went wrong, when it went wrong, and how it can be fixed so I may avoid this in future programs.

Thank you for any and all help you provide, it is greatly appreciated! n_n
• 04-10-2005
Micko
It's called Round-off error.
The decimal fraction 0.1 cannot be precisely represented in binary. It is a repeater fraction 0.00011001100110... It is like the repeater fraction 1/3 = 0.33333 in base 10.
Round-off arises from the finite nature of the computing machine. The machine is used to represent the mathematician's number system, which uses infinitely long representations. Although round-off may be a small effect in a single computation, errors may accumulate over the incredible number of arithmetic operations that can occur in a problem requiring only a few seconds of machine time. The greatest loss of significance in results occurs when two numbers of about the same size are subtracted so that most of the leading digits cancel out. This effect can arise unexpectedly in almost any long computation.

http://mathworld.wolfram.com/RoundoffError.html
• 04-10-2005
Russell
Quote:

Originally Posted by SomeCrazyGuy
Code:

```//SomeCrazyGuy //'For' Looping Test #include <iostream.h> #include <stdlib.h> using namespace std; int main () {     float a;         for(a=0;a<=1;a=a+.01) {     cout<< a <<endl; }         system("pause");     return 0; }```

Also, when incrementing a variable, a+=.01 is suffice.
• 04-10-2005
7stud
Quote:

Originally Posted by SomeCrazyGuy
I am just learning how to program

Quote:

Originally Posted by Russel
Also, when incrementing a variable, a+=.01 is suffice.

Most beginners don't start out using the op= form of assignment. Did you?

Quote:

but also from a need, I wish to know how this went wrong
You decided to use a computer. A computer uses a binary number system(i.e base 2), which means there are only 1's and 0's for digits and they are represented on your computer chip by either an electric charge being present or no electric charge being present. Humans tend to use a decimal number system(i.e base 10) where the digits run from 0-9. Just as 1/3 can't be represented exactly using a decimal number--it's .33333....<forever> so at some point you just have to approximate it with .3333333--it's the same way with binary numbers, and some numbers can't be represented exactly.

Quote:

when it went wrong, and how it can be fixed so I may avoid this in future programs
I don't think it is possible to fix, but you can minimize those errors, but you will need to start doing a lot of reading about the problem.
• 04-10-2005
hk_mp5kpdw
Quote:

Originally Posted by SomeCrazyGuy
I need help to figure out what went wrong... As I am just learning how to program this, I was testing out a 'for' statment:

Code:

```//SomeCrazyGuy //'For' Looping Test #include <iostream>  // Drop the ".h" #include <cstdlib>  // Drop the ".h" add the "c" using namespace std; int main () {     float a;         for(a=0;a<=1;a=a+.01) {     cout<< a <<endl; }         system("pause");     return 0; }```

• 04-10-2005
SomeCrazyGuy
So in other words, I need to find an alternative way to go about doing this?... Oh well... I'll figure out a way to continue through this when the time comes. Thanks for the help (and I am almost glad to hear that it isn't my fault)!
• 04-10-2005
Kirdra
I think you need to use integer type data (int/char) to iterate through loops.
• 04-10-2005
pliang
There is a way to make it look more like you intended, but it doesn't fix the intrinsic problem just hides it.

Code:

```//SomeCrazyGuy //'For' Looping Test #include <iostream.h> #include <stdlib.h> using namespace std; int main () {     float a;         for(a=0;a<=1;a=a+.01) {     cout.precision(2);     cout<< a <<endl; }         system("pause");     return 0; }```
• 04-10-2005
SomeCrazyGuy
Huh... I just thought of something... If I was to multiply each value by 1.0000009536752259018191354934077 (1/((209715/2097152)*10)), would that then negate any sort of flaws created by the flaw in this issue?... Or would the flaws creating the issues then affect that value and further flaw any output? I'm gona test this and tell you what I got...
• 04-10-2005
SomeCrazyGuy
With everything I tried, I got either the same result, or this new result:

.04
.05
.0600001
.0700001
...
.0900001
.1
.11
.12
...so on...

~~~

So I suppose there is no way, beyond pliangs suggestion, to at at least mask the flaw... Thanks! n_n'