简体   繁体   中英

Why is my program returning a garbage value when k == 0 in the for-loop?

This is a simple C++ program which computes f(x) using a formula for a fixed number of values between 2 given numbers.

There seems to be some problem when k = 0 in the for loop. It is returning a garbage value.

Can anyone tell me why?

Any help is highly appreciated. Thanks in advance.

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

int main ()
{

  int const POINTS = 21;
  double const PI = 3.1416;
  double min, max, increment, dataPoints[21];

  cout << "Enter max value: ";
  cin >> max;

  cout << "Enter min value: ";
  cin >> min;

  increment = (max - min) / (POINTS - 1);


  cout << setw (20) << "X-Value" << "|";
  cout << setw (20) << "Y-Value" << endl;

  double k;
  int l;

  for (k = min, l = 0; l < POINTS, k <= max; l++, k += increment)
    {
      dataPoints[l] = (PI / 16) * sin (6.036 * k) + (1 / 64) * PI * cos (24.44 * k);
      cout << setw (20) << k << setw (20) << dataPoints[l] << endl;
    }

}

Output:

Enter max value: 4
Enter min value: -4
             X-Value|             Y-Value
                  -4            0.164018
                -3.6          -0.0507715
                -3.2          -0.0881608
                -2.8            0.182492
                -2.4           -0.184497
                  -2           0.0931637
                -1.6           0.0453027
                -1.2            -0.16085
                -0.8            0.195021
                -0.4           -0.130529
       -5.55112e-016       -6.57901e-016
                 0.4            0.130529
                 0.8           -0.195021
                 1.2             0.16085
                 1.6          -0.0453027
                   2          -0.0931637
                 2.4            0.184497
                 2.8           -0.182492
                 3.2           0.0881608
                 3.6           0.0507715
                   4           -0.164018

Process returned 0 (0x0)   execution time : 3.634 s
Press any key to continue.

One problem problem lies in the code (1 / 64) ... because you have put this expression in brackets. It is, thus, integer division and, as such, will always have the value of zero .

Try this, instead:

    dataPoints[l] = (PI / 16) * sin (6.036 * k) + (1.0 / 64) * PI * cos (24.44 * k);

There is also a problem in the way you have expressed the 'test' condition in your for loop - the comma operator here will, effectively, ignore the first part of the expression:

for (k = min, l = 0; l < POINTS, k <= max; l++, k += increment)

For safety, when you want both conditions to be tested, use the && operator:

for (k = min, l = 0; l < POINTS && k <= max; l++, k += increment) {

Lastly, your actual question:

There seems to be some problem when k = 0 in the for loop. It is returning a garbage value.

No, it's not! The floating-point operations you perform on the k variable (ie k += increment , on each loop) are not precise: what you think will be 0.4 + (-0.4) will actually be 'nearly' '0.4' - 'nearly' '0.4' ; as such, the value you get (my system gives -5.55112e-16 ) is a 'reasonable approximation' to zero, given the ranges of the numbers that you've used (and the accumulated 'errors' in the previous loops).

Feel free to ask for further clarification and/or explanation.

Because of rounding error when you increment by 0.2, your k value never gets to be exactly zero -- its off by a little bit. SeeWhat every computer scientist should know about floating point

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM