[英]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.