Looping error 
Author Message
 Looping error

I am writing a program that deals with the Ideal Gas Law.  I have a number of
gasses that I want to test for the pressure on a given volume container.

for (i = 0; i < recCount; ++i)
{
        // Don't put a new line at the top of the output file.
        if (i != 0)
                output << '\n';

        // Set the variables to be passed to the function pressure.
        numMol = 10. / MW[i];

        // Get the interval of Volume from the user.
        cout << "Please enter the Volume interval for Sample #"
                  << i + 1 << ": ";
        cin >> interval;

        // Print and write each interval Volume.
        for (Volume = 1; Volume < 100; Volume += interval)
        {
                // find the pressure of the Gas Sample.
                Pressure = pressure(Volume, Temperature, numMol);

                // Display ordered pairs on the screen,
                // and put them in the output file.
                cout << Volume << '\t' << '\t' << Pressure << endl;
                output << Volume << '\t' << Pressure << '\n';
        } // end Volume for loop.

        // Print and write Volume == 100;
        Volume = 100;
        Pressure = pressure(Volume, Temperature, numMol);
        cout << Volume << "\t\t" << Pressure << endl;
        output << Volume << '\t' << Pressure;

Quote:
} // end record for loop.

This works great except for the interval of 0.1.  If this is the interval it
displays and records 100 twice.  Once within the Volume for loop, and once for
the 100 finalizing routine.  I have tried other fractal intervals such as 0.01
and they also work fine.  Is this a problem within C++ or is there a programming
error in the code?


Tue, 14 Oct 2003 05:12:13 GMT  
 Looping error
To expand on what MediaOne said, try this.  At the end of your for loop, try
printing out the difference between Volume and 100. Then look at the
precision your cout << can print and notice the roundoff.
Dan Evens


Tue, 14 Oct 2003 05:56:46 GMT  
 Looping error
I'm assuming that Volume and interval are declared as type double.  The IEEE
double type can not represent the quantity 0.1.  Instead, it's value is
something like 0.09873339281 or something like that.  You're experiencing
loss of precision and loss of representation accuracy.


I am writing a program that deals with the Ideal Gas Law.  I have a number
of
gasses that I want to test for the pressure on a given volume container.

for (i = 0; i < recCount; ++i)
{
// Don't put a new line at the top of the output file.
if (i != 0)
output << '\n';

// Set the variables to be passed to the function pressure.
numMol = 10. / MW[i];

// Get the interval of Volume from the user.
cout << "Please enter the Volume interval for Sample #"
  << i + 1 << ": ";
cin >> interval;

// Print and write each interval Volume.
for (Volume = 1; Volume < 100; Volume += interval)
{
// find the pressure of the Gas Sample.
Pressure = pressure(Volume, Temperature, numMol);

// Display ordered pairs on the screen,
// and put them in the output file.
cout << Volume << '\t' << '\t' << Pressure << endl;
output << Volume << '\t' << Pressure << '\n';

Quote:
} // end Volume for loop.

<<<<<<<<<<<<<<SNIP>>>>>>>>>>>>>>

This works great except for the interval of 0.1.  If this is the interval it
displays and records 100 twice.  Once within the Volume for loop, and once
for
the 100 finalizing routine.  I have tried other fractal intervals such as
0.01
and they also work fine.  Is this a problem within C++ or is there a
programming
error in the code?



Tue, 14 Oct 2003 05:51:10 GMT  
 Looping error
Never do something like

double delta, start, end;
for (double v = start; v < end; v += delta)
{
    // Do something

Quote:
}

The computer cannot represent real numbers exactly - there's always some
rounding error involved. And when you perform addition multiple times, you
accumulate those errors. For example, imagine you want delta to be 1.0, but
it is actually 0.999. You do v += delta one thousand times and end up with
999, not 1000.

Better do something like

int n = (end - start) / delta;
for (int i = 0; i < n; i++)
{
    double v = start + i * delta;
    // Do something

Quote:
}

--
With best wishes,
    Igor Tandetnik


I am writing a program that deals with the Ideal Gas Law.  I have a number
of
gasses that I want to test for the pressure on a given volume container.

for (i = 0; i < recCount; ++i)
{
// Don't put a new line at the top of the output file.
if (i != 0)
output << '\n';

// Set the variables to be passed to the function pressure.
numMol = 10. / MW[i];

// Get the interval of Volume from the user.
cout << "Please enter the Volume interval for Sample #"
  << i + 1 << ": ";
cin >> interval;

// Print and write each interval Volume.
for (Volume = 1; Volume < 100; Volume += interval)
{
// find the pressure of the Gas Sample.
Pressure = pressure(Volume, Temperature, numMol);

// Display ordered pairs on the screen,
// and put them in the output file.
cout << Volume << '\t' << '\t' << Pressure << endl;
output << Volume << '\t' << Pressure << '\n';

Quote:
} // end Volume for loop.

// Print and write Volume == 100;
Volume = 100;
Pressure = pressure(Volume, Temperature, numMol);
cout << Volume << "\t\t" << Pressure << endl;
output << Volume << '\t' << Pressure;

Quote:
} // end record for loop.

This works great except for the interval of 0.1.  If this is the interval it
displays and records 100 twice.  Once within the Volume for loop, and once
for
the 100 finalizing routine.  I have tried other fractal intervals such as
0.01
and they also work fine.  Is this a problem within C++ or is there a
programming
error in the code?


Tue, 14 Oct 2003 06:05:05 GMT  
 Looping error
Your code will work if I am running from (0 * step + 1) to (n * step + 1), but
I'm running into a problem with the incrementing that is required for this.  I
have to display a 1 liter container and 100 liter container, and the increments
in between need to be the increment starting at 'increment':
(i.e. increment = 5; Display Volume = 1, 5, 10, 15, ...).
When I ran this with the 1 liter outside the loop and started with 'increment' it
displayed 1, 5, 10, ..., 95, 100, 100.
When I ran this from (i = 0; i < n; ++i) it displayed 1, 5, 10, ..., 90, 100.

I guess what I'm saying is I have to include 1 and 100 and increments from 0 +
increment + increment + ...

Quote:
-----Original Message-----

Never do something like

double delta, start, end;
for (double v = start; v < end; v += delta)
{
    // Do something
}

The computer cannot represent real numbers exactly - there's always some
rounding error involved. And when you perform addition multiple times, you
accumulate those errors. For example, imagine you want delta to be 1.0, but
it is actually 0.999. You do v += delta one thousand times and end up with
999, not 1000.

Better do something like

int n = (end - start) / delta;
for (int i = 0; i < n; i++)
{
    double v = start + i * delta;
    // Do something
}

--
With best wishes,
    Igor Tandetnik



I am writing a program that deals with the Ideal Gas Law.  I have a number
of
gasses that I want to test for the pressure on a given volume container.

for (i = 0; i < recCount; ++i)
{
// Don't put a new line at the top of the output file.
if (i != 0)
output << '\n';

// Set the variables to be passed to the function pressure.
numMol = 10. / MW[i];

// Get the interval of Volume from the user.
cout << "Please enter the Volume interval for Sample #"
  << i + 1 << ": ";
cin >> interval;

// Print and write each interval Volume.
for (Volume = 1; Volume < 100; Volume += interval)
{
// find the pressure of the Gas Sample.
Pressure = pressure(Volume, Temperature, numMol);

// Display ordered pairs on the screen,
// and put them in the output file.
cout << Volume << '\t' << '\t' << Pressure << endl;
output << Volume << '\t' << Pressure << '\n';
} // end Volume for loop.

// Print and write Volume == 100;
Volume = 100;
Pressure = pressure(Volume, Temperature, numMol);
cout << Volume << "\t\t" << Pressure << endl;
output << Volume << '\t' << Pressure;
} // end record for loop.

This works great except for the interval of 0.1.  If this is the interval it
displays and records 100 twice.  Once within the Volume for loop, and once
for
the 100 finalizing routine.  I have tried other fractal intervals such as
0.01
and they also work fine.  Is this a problem within C++ or is there a
programming
error in the code?

.



Tue, 14 Oct 2003 07:32:31 GMT  
 Looping error
I want to thank you all for helping with this.  I finally got it to work using
the double to int conversion that Igor suggested.  I made 1 and 100 special
cases, my start point is interval, unless interval is less than 1 then it starts
at interval + 1.  And then used (end - start) / delta; for the inner loop.

I am now getting a warning about losing the integrity of the double, that round
off errors might occur.  Is this something I should just ignore?

Thanks again,

Rene Larsen

Quote:
-----Original Message-----

I am writing a program that deals with the Ideal Gas Law.  I have a number of
gasses that I want to test for the pressure on a given volume container.

for (i = 0; i < recCount; ++i)
{
        // Don't put a new line at the top of the output file.
        if (i != 0)
                output << '\n';

        // Set the variables to be passed to the function pressure.
        numMol = 10. / MW[i];

        // Get the interval of Volume from the user.
        cout << "Please enter the Volume interval for Sample #"
                  << i + 1 << ": ";
        cin >> interval;

        // Print and write each interval Volume.
        for (Volume = 1; Volume < 100; Volume += interval)
        {
                // find the pressure of the Gas Sample.
                Pressure = pressure(Volume, Temperature, numMol);

                // Display ordered pairs on the screen,
                // and put them in the output file.
                cout << Volume << '\t' << '\t' << Pressure << endl;
                output << Volume << '\t' << Pressure << '\n';
        } // end Volume for loop.

        // Print and write Volume == 100;
        Volume = 100;
        Pressure = pressure(Volume, Temperature, numMol);
        cout << Volume << "\t\t" << Pressure << endl;
        output << Volume << '\t' << Pressure;
} // end record for loop.

This works great except for the interval of 0.1.  If this is the interval it
displays and records 100 twice.  Once within the Volume for loop, and once for
the 100 finalizing routine.  I have tried other fractal intervals such as 0.01
and they also work fine.  Is this a problem within C++ or is there a programming
error in the code?
.



Wed, 15 Oct 2003 06:35:15 GMT  
 Looping error
That's when you convert double to int - it truncates thus losing significant
digits, and compiler warns you about it. You can tell the compiler it is
exactly what you want by explicit cast:

int n = int((end - start) / delta);

--
With best wishes,
    Igor Tandetnik


I want to thank you all for helping with this.  I finally got it to work
using
the double to int conversion that Igor suggested.  I made 1 and 100 special
cases, my start point is interval, unless interval is less than 1 then it
starts
at interval + 1.  And then used (end - start) / delta; for the inner loop.

I am now getting a warning about losing the integrity of the double, that
round
off errors might occur.  Is this something I should just ignore?

Thanks again,

Rene Larsen

Quote:
-----Original Message-----

I am writing a program that deals with the Ideal Gas Law.  I have a number
of
gasses that I want to test for the pressure on a given volume container.

for (i = 0; i < recCount; ++i)
{
// Don't put a new line at the top of the output file.
if (i != 0)
output << '\n';

// Set the variables to be passed to the function pressure.
numMol = 10. / MW[i];

// Get the interval of Volume from the user.
cout << "Please enter the Volume interval for Sample #"
  << i + 1 << ": ";
cin >> interval;

// Print and write each interval Volume.
for (Volume = 1; Volume < 100; Volume += interval)
{
// find the pressure of the Gas Sample.
Pressure = pressure(Volume, Temperature, numMol);

// Display ordered pairs on the screen,
// and put them in the output file.
cout << Volume << '\t' << '\t' << Pressure << endl;
output << Volume << '\t' << Pressure << '\n';
} // end Volume for loop.

// Print and write Volume == 100;
Volume = 100;
Pressure = pressure(Volume, Temperature, numMol);
cout << Volume << "\t\t" << Pressure << endl;
output << Volume << '\t' << Pressure;
} // end record for loop.

This works great except for the interval of 0.1.  If this is the interval it
displays and records 100 twice.  Once within the Volume for loop, and once
for
the 100 finalizing routine.  I have tried other fractal intervals such as
0.01
and they also work fine.  Is this a problem within C++ or is there a
programming
error in the code?
.



Fri, 17 Oct 2003 22:19:58 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. foreach-loop error

2. weird loop error while using IO

3. Continue Looping on Error

4. Will loop refrence cause error?

5. error check loops

6. Loop though the Connection Errors Collection...

7. Why This Error? Abnormal Result in Loop

8. redefinition error in a for-loop

9. Error messege in a while loop

10. file pointer errors in a loop

11. For loops into for loops

12. Loop or Loops in "C"

 

 
Powered by phpBB® Forum Software