One simple question

This is a discussion on One simple question within the C++ Programming forums, part of the General Programming Boards category; Hi all,here i have one very simple program.I want to divide an interval [-1,1] into 10 sub intervals.so then i ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    18

    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;
    }
    Last edited by turkertopal; 10-14-2005 at 02:54 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    if(mynumber < .001) mynumber = 0;

    ?
    Last edited by 7stud; 10-14-2005 at 04:39 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    modulus of mynumber<0.001 would be ok. but why it happens? 0.02-0.02 is 0 anyway..
    Last edited by turkertopal; 10-14-2005 at 05:01 AM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    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
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    thanks a lot

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 10-14-2005 at 12:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21