到目前为止,我得到了一个可能的答案:限制浮点精度?
阅读第二个答案,获得了 10 票,而不是仅获得 4 票的接受的答案。不要这样做。
当您在纸上进行计算时,您不想这样做,更不用说在计算机上。这些中间计算最好比基础数据显示的至少一个额外的有效数字,最好是两个或更多。您截断到最后数据指示的精度。我们在纸上这样做的唯一原因是人们不擅长处理大量数字。这是一种短路操作,可根据人们的计算方式(或错误计算方式)进行调整。
All that you are doing by rounding intermediate calculations accomplishes is to create an opening for errors to creep in and slowing the computer down, oftentimes by a quite a bit. Don't worry about the extra precision in those intermediate results. Simply use display the results to the desired precision on output.
The opposite problem sometimes does apply. You may need to worry about loss of precision in your intermediate results. Sometimes that loss of precision will mean changing from floats to doubles, or from doubles to variable precision arithmetic (which is slow).