我正在尝试在学校项目的代码中实施Gram Schmidt,我有一个问题。它输出一个奇怪的数字,我不知道为什么。其余的都很好,但这个很糟糕。如果帖子是不好,但这是我在这里的第一篇文章,我真的需要帮助。谢谢
#include <iostream> #define dim 100 using namespace std; void inputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i = 0; i < *n; i++){ cout << "V" << i << ":"; for(j = 0; j < *m; j++) cin >> x[i][j]; } } void outputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i=0;i<*n;i++){ for(j=0;j<*m;j++) cout<<x[i][j]<<" "; cout<<endl; } } void initialize(int *m,double v[dim]){ int i; for(i=0;i<*m;i++){ v[i]=0; } } int main(){ double v[dim][dim], f[dim][dim], e[dim][dim],p1,p2,a[dim]; int n,m,i,j,z; cout << "Introduceti numarul de vectori: ";cin >> n; cout << "Introduceti numarul de elemente: ";cin >> m; inputMatrix(&n,&m,v); double div = 0; for(i = 0; i < m; i++){ f[0][i] = v[0][i]; } outputMatrix(&n,&m,v); cout << endl; outputMatrix(&n,&m,f); for(i = 1;i < n; i++){ z = 0; initialize(&m,a); mk1: p1 = 0; p2 = 0; for(j = 0; j < m; j++){ p1 = f[z][j] * v[i][j] + p1; p2 = f[z][j] * f[z][j] + p2; } div = p1 / p2; for(j = 0; j < m; j++){ a[j] = f[z][j] * div + a[j]; } z++; if( z < i){ goto mk1; } else{ for(j = 0; j < m;j++){ f[i][j] = v[i][j] - a[j]; } } cout << endl; outputMatrix(&n,&m,f); return 0; }输出:
Introduceti numarul de vectori: 3 Introduceti numarul de elemente: 4 V0:1 2 3 0 V1:1 2 0 0 V2:1 0 0 1 1 2 3 0 1 2 0 0 1 0 0 1 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3 0 0.642857 1.28571 -1.07143 0 0.8 -0.4 5.55112e-17 1我不明白为什么输出这个“5.55112e-17”感谢您的帮助!
I'm trying to implement Gram Schmidt in code for a school project and I have a problem.It outputs a weird number and I don't know why.The rest of them are good but this one is bad.Sorry if the post is bad but it's my first post here and I really need help.Thanks
#include <iostream> #define dim 100 using namespace std; void inputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i = 0; i < *n; i++){ cout << "V" << i << ":"; for(j = 0; j < *m; j++) cin >> x[i][j]; } } void outputMatrix(int *n, int *m, double x[dim][dim]){ int i,j; for(i=0;i<*n;i++){ for(j=0;j<*m;j++) cout<<x[i][j]<<" "; cout<<endl; } } void initialize(int *m,double v[dim]){ int i; for(i=0;i<*m;i++){ v[i]=0; } } int main(){ double v[dim][dim], f[dim][dim], e[dim][dim],p1,p2,a[dim]; int n,m,i,j,z; cout << "Introduceti numarul de vectori: ";cin >> n; cout << "Introduceti numarul de elemente: ";cin >> m; inputMatrix(&n,&m,v); double div = 0; for(i = 0; i < m; i++){ f[0][i] = v[0][i]; } outputMatrix(&n,&m,v); cout << endl; outputMatrix(&n,&m,f); for(i = 1;i < n; i++){ z = 0; initialize(&m,a); mk1: p1 = 0; p2 = 0; for(j = 0; j < m; j++){ p1 = f[z][j] * v[i][j] + p1; p2 = f[z][j] * f[z][j] + p2; } div = p1 / p2; for(j = 0; j < m; j++){ a[j] = f[z][j] * div + a[j]; } z++; if( z < i){ goto mk1; } else{ for(j = 0; j < m;j++){ f[i][j] = v[i][j] - a[j]; } } cout << endl; outputMatrix(&n,&m,f); return 0; }Output:
Introduceti numarul de vectori: 3 Introduceti numarul de elemente: 4 V0:1 2 3 0 V1:1 2 0 0 V2:1 0 0 1 1 2 3 0 1 2 0 0 1 0 0 1 1 2 3 0 0 0 0 0 0 0 0 0 1 2 3 0 0.642857 1.28571 -1.07143 0 0.8 -0.4 5.55112e-17 1I don't understand why it outputs this "5.55112e-17" Thanks for help!
最满意答案
我不明白为什么输出这个“5.55112e-17”感谢您的帮助!
这是舍入误差的结果,这种表示法意味着5.55112乘以10的幂,并且可以被认为是用(实数)数字的计算机表示计算的伪像。 有关详细说明,请考虑这个答案。
I don't understand why it outputs this "5.55112e-17" Thanks for help!
This is the result of rounding errors, this notation means 5.55112 times 10 to the power of -17 and can be considered an artifact of calculating with computer representations of (real) numbers. For some more elaboration, consider this answer.
更多推荐
发布评论