简体   繁体   English

为什么我的程序在 for 循环中当 k == 0 时返回垃圾值?

[英]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.这是一个简单的 C++ 程序,它使用 2 个给定数字之间的固定数量值的公式计算 f(x)。

There seems to be some problem when k = 0 in the for loop.当 for 循环中的 k = 0 时,似乎存在一些问题。 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.一个问题出在代码(1 / 64) ……因为你把这个表达式放在括号里了。 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循环中表达“测试”条件的方式也存在问题 - 此处的逗号运算符将有效地忽略表达式的第一部分:

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.当 for 循环中的 k = 0 时,似乎存在一些问题。 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' ;您对k变量执行的浮点运算(即k += increment ,在每个循环中)并不精确:您认为0.4 + (-0.4)实际上'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).因此,您得到的值(我的系统给出-5.55112e-16 )是对零的“合理近似”,考虑到您使用的数字范围(以及之前循环中累积的“错误”)。

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.由于当您增加 0.2 时的舍入误差,您的 k 值永远不会完全为零——它会稍微偏离。 SeeWhat every computer scientist should know about floating point看看每个计算机科学家应该知道的关于浮点数的知识

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在访问指针的值时程序会返回垃圾? - Why does my program return garbage when accessing the value of a pointer? 程序返回垃圾值 - Program returning garbage value 为什么冒泡排序程序在for循环中使用“break”时会显示垃圾值? - Why does the bubble sort program show the garbage value when using "break" in for loop? 为什么函数返回垃圾值? - Why the function is returning garbage value? 为什么我的程序会打印垃圾? - Why is my program printing garbage? 为什么我的函数应该返回一个字符时返回垃圾? - Why is my function returning garbage when it should return a char? 当我的 char 变量达到 [del] 字符 (127) 值时,为什么我的程序会进入无限循环? - Why does my program enters into an infinite loop when my char variable reaches the [del] character (127) value? 如何正确构建我的 for 循环以便我的程序运行? - How to structure my for-loop correctly so my program runs? 为什么我的代码用于在阶乘返回垃圾值中查找尾随零的数量 - Why is my code for finding number of trailing zeros in a factorial returning garbage value 为什么我的函数中的此for循环仅运行一次,即使应该运行多次? - Why does this for-loop in my function will only run once, even when it's supposed to run multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM