写入 RREF 时处理舍入误差
我正在尝试编写一个程序,在给定矩阵时解决简化的行梯形形式。基本上我正在做的就是编写一个解决方程组的程序。然而,由于有时我需要进行除法以产生重复的数字(例如 2/3,即 .66666...)并且 java 四舍五入到某个数字,因此有时会出现枢轴应该是 0 (意味着没有枢轴)类似于 .0000001 ,它搞乱了我的整个程序。
我的第一个问题是,如果我有某种 if 语句,最好的写法是什么,比如“如果这个数字距离整数小于 0.00001,则四舍五入到最接近的整数”。
我的第二个问题是,是否有人对处理这种情况的更优化方法有任何想法,而不是仅仅将 if 语句四舍五入到各处。
非常感谢。
I'm trying to write a program that solves for the reduced row echelon form when given a matrix. Basically what I'm doing is writing a program that solves systems of equations. However, due to the fact that there are times when I need to do division to result in repeating digits (such as 2/3 which is .66666...) and java rounds off to a certain digit, there are times when a pivot should be 0 (meaning no pivot) is something like .0000001 and it messes up my whole program.
My first question is if I were to have some sort of if statement, what is the best way to write something like "if this number is less than .00001 away from being an integer, then round to that closest integer".
My second question is does anyone have any ideas on more optimal ways of handling this situation rather than just put if statements rounding numbers all over the place.
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说您正在编写一个求解方程组的程序。这是一个相当复杂的问题。如果您只想使用这样的程序,那么最好使用其他人编写的库。我假设您真的想自己编写该程序,以供娱乐和/或教育。
您确定了主要问题:使用浮点数会导致四舍五入,从而导致结果不精确。对此有两种解决方案。
第一个解决方案是不使用浮点数。仅使用整数并将矩阵简化为行梯形形式(不简化);这可以在不进行分裂的情况下完成。由于所有整数计算都是精确的,因此应该为 0 的主元将恰好为 0(实际上,可能存在溢出问题)。当然,只有当您开始使用的矩阵由整数组成时,这才有效。您可以通过使用分数而不是整数来推广此方法。
第二种解决方案是使用浮点数,并且要非常小心。这是数学/计算机科学的一个完整分支的主题,称为数值分析。这里的答案太复杂了,无法解释,所以你必须买一本关于数值分析的书。简单来说,你想要做的是如果 Math.abs(pivot) <某个小值,那么您假设主元应该为零,但由于舍入错误,它类似于 0.0000000001,因此您只需将主元视为零即可。问题是找出“一些小值”是什么。
You say that you are writing a program that solves systems of equations. This is quite a complicated problem. If you only want to use such a program, you are better off using a library written by somebody else. I will assume that you really want to write the program yourself, for fun and/or education.
You identified the main problem: using floating point numbers leads to rounding and thus to inexact results. There are two solutions for this.
The first solution is not to use floating point numbers. Use only integers and reduce the matrix to row echelon form (not reduced); this can be done without divisions. Since all computations with integers are exact, a pivot that should be 0 will be exactly 0 (actually, there may be a problem with overflow). Of course, this will only work if the matrix you start with consists of integers. You can generalize this approach by working with fractions instead of integers.
The second solution is to use floating point numbers and be very careful. This is a topic of a whole branch of mathematics / computer science called numerical analysis. It is too complicated to explain in an answer here, so you have to get a book on numerical analysis. In simple terms, what you want to do is to say that if Math.abs(pivot) < some small value, then you assume that the pivot should be zero, but that it is something like .0000000001 because of rounding errors, so you just act as if the pivot is zero. The problem is finding out what "some small value" is.