# One simple question

Printable View

• 10-14-2005
turkertopal
One simple question
Hi all,here i have one very simple program.I want to divide an interval [-1,1] into 10 sub intervals.so then i will have each of them devided by 10 more sub sub interval.You can get this from code easy though.What im asking is when its supposed to be zero it gives computer epsilon i.e 3,27826e-8 but it has to be zero.here is my code.what do i need to change this?
output that i have is like this

Code:

```interval -1 -0.8 -1 -0.998 -0.996 - 0.994...........................-0.8 .... ... interval -0.2 3,27826e-8 -0.2 -0.198 - 0.196 -0.194.......................3,27826e-8```
Code:

```#include <iostream> using namespace std; int main() {         float X[11],XSub[11];     int i=0,j=0;       X[0]=-1;            for (i=0;i<10;i++)     {         X[i+1]=X[i]-(- 0.2);          XSub[0]=X[i];                 cout<<"interval "<<X[i]<<" "<<X[i+1]<<endl;         for (j=0;j<11;j++)         {           XSub[j+1]=XSub[j]-(-0.02) ;           cout<<XSub[j]<<" ";                     }         cout<<endl;              }         cin.get();     return 0; }```
• 10-14-2005
7stud
if(mynumber < .001) mynumber = 0;

?
• 10-14-2005
turkertopal
modulus of mynumber<0.001 would be ok. but why it happens? 0.02-0.02 is 0 anyway..
• 10-14-2005
SlyMaelstrom
Format your output.

Code:

```#include <iostream> #include <iomanip> using namespace std; int main() {         float X[11],XSub[11];     int i=0,j=0;       X[0]=-1;            for (i=0;i<10;i++)     {         X[i+1]=X[i]-(- 0.2);          XSub[0]=X[i];                 cout.setf(ios::fixed,ios::floatfield);  // You need these flags         cout.setf(ios::showpoint);  // for floats to read properly                                               cout << setprecision(2);                              cout << "interval " << X[i] << " " <<X [i+1] <<endl;         for (j=0;j<11;j++)         {           XSub[j+1]=XSub[j]-(-0.02) ;           cout<<XSub[j]<<" ";                     }         cout<<endl;              }         cin.get();     return 0; }```
Sample Output:
Code:

```interval -1.00 -0.80 -1.00 -0.98 -0.96 -0.94 -0.92 -0.90 -0.88 -0.86 -0.84 -0.82 -0.80 interval -0.80 -0.60 -0.80 -0.78 -0.76 -0.74 -0.72 -0.70 -0.68 -0.66 -0.64 -0.62 -0.60 interval -0.60 -0.40 -0.60 -0.58 -0.56 -0.54 -0.52 -0.50 -0.48 -0.46 -0.44 -0.42 -0.40 interval -0.40 -0.20 -0.40 -0.38 -0.36 -0.34 -0.32 -0.30 -0.28 -0.26 -0.24 -0.22 -0.20 interval -0.20 -0.00 -0.20 -0.18 -0.16 -0.14 -0.12 -0.10 -0.08 -0.06 -0.04 -0.02 -0.00 interval -0.00 0.20 -0.00 0.02 0.04 0.06 0.08 0.10 0.12 0.14 0.16 0.18 0.20 interval 0.20 0.40 0.20 0.22 0.24 0.26 0.28 0.30 0.32 0.34 0.36 0.38 0.40 interval 0.40 0.60 0.40 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58 0.60 interval 0.60 0.80 0.60 0.62 0.64 0.66 0.68 0.70 0.72 0.74 0.76 0.78 0.80 interval 0.80 1.00 0.80 0.82 0.84 0.86 0.88 0.90 0.92 0.94 0.96 0.98 1.00```
• 10-14-2005
turkertopal
thanks a lot :)
• 10-14-2005
7stud
Quote:

but why it happens? 0.02-0.02 is 0 anyway..
Because a computer has to convert decimal numbers to binary format, i.e a series of 1's and 0's. Some numbers written in decimal format cannot be represented exactly in binary format--just like the number 1/3 written in fraction format can't be represented exactly in decimal format, 1/3 = .3333... on out to infinity. At some point, if you stop the repeating 3's, and use that decimal as a representation of 1/3, for instance .333, then you have only an approximation of 1/3. When you use an approximation of a number, then you can get small differences when you start doing calculations with other numbers.