小数字代替零?

编程入门 行业动态 更新时间:2024-10-28 18:25:05
本文介绍了小数字代替零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在做一个矩阵类(作为一个学习练习),我在测试我的逆函数时遇到了问题。

I have been making a matrix class (as a learning exercise) and I have come across and issue whilst testing my inverse function.

我输入一个任意矩阵例如:

I input a arbitrary matrix as such:

2 1 1 1 2 1 1 1 2

得到它来计算逆,我得到正确的结果:

And got it to calculate the inverse and I got the correct result:

0.75 -0.25 -0.25 -0.25 0.75 -0.25 -0.25 -0.25 0.75

但是当我尝试将两者相乘以确保我得到单位矩阵时,我得到:

But when I tried multiplying the two together to make sure I got the identity matrix I get:

1 5.5111512e-017 0 0 1 0 -1.11022302e-0.16 0 1

为什么得到这些结果?我会理解,如果我是乘以奇怪的数字,我可以理解一些舍入误差,但它的总和是:

Why am I getting these results? I would understand if I was multiplying weird numbers where I could understand some rounding errors but the sum it's doing is:

2 * -0.25 + 1 * 0.75 + 1 * -0.25

这显然是0,而不是5.111512e-017

which is clearly 0, not 5.111512e-017

如果我手动得到它做计算;例如:

If I manually get it to do the calculation; eg:

std::cout << (2 * -0.25 + 1 * 0.75 + 1 * -0.25) << "\n";

我预期得到0?

所有数字都表示为双精度。 这是我的乘法重载:

All the numbers are represented as doubles. Here's my multiplcation overload:

Matrix operator*(const Matrix& A, const Matrix& B) { if(A.get_cols() == B.get_rows()) { Matrix temp(A.get_rows(), B.get_cols()); for(unsigned m = 0; m < temp.get_rows(); ++m) { for(unsigned n = 0; n < temp.get_cols(); ++n) { for(unsigned i = 0; i < temp.get_cols(); ++i) { temp(m, n) += A(m, i) * B(i, n); } } } return temp; } throw std::runtime_error("Bad Matrix Multiplication"); }

和访问功能:

double& Matrix::operator()(unsigned r, unsigned c) { return data[cols * r + c]; } double Matrix::operator()(unsigned r, unsigned c) const { return data[cols * r + c]; }

这里是找到反函数的函数:

Here's the function to find the inverse:

Matrix Inverse(Matrix& M) { if(M.rows != M.cols) { throw std::runtime_error("Matrix is not square"); } int r = 0; int c = 0; Matrix augment(M.rows, M.cols*2); augment.copy(M); for(r = 0; r < M.rows; ++r) { for(c = M.cols; c < M.cols * 2; ++c) { augment(r, c) = (r == (c - M.cols) ? 1.0 : 0.0); } } for(int R = 0; R < augment.rows; ++R) { double n = augment(R, R); for(c = 0; c < augment.cols; ++c) { augment(R, c) /= n; } for(r = 0; r < augment.rows; ++r) { if(r == R) { continue; } double a = augment(r, R); for(c = 0; c < augment.cols; ++c) { augment(r, c) -= a * augment(R, c); } } } Matrix inverse(M.rows, M.cols); for(r = 0; r < M.rows; ++r) { for(c = M.cols; c < M.cols * 2; ++c) { inverse(r, c - M.cols) = augment(r, c); } } return inverse; }

推荐答案

0.250000000000000005在你的倒置矩阵,他们只是四舍五入显示,所以你看到很好的小圆的数字,如0.25。

You've got numbers like 0.250000000000000005 in your inverted matrix, they're just rounded for display so you see nice little round numbers like 0.25.

更多推荐

小数字代替零?

本文发布于:2023-10-26 12:37:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!