How to reduce C/C++ floating-point roundoff

限于喜欢 提交于 2019-12-10 11:52:34

问题


Are there any generally-applicable tips to reduce the accumulation of floating-point roundoff errors in C or C++? I'm thinking mainly about how to write code that gets compiled into optimal assembly language instructions, although strategies on overall algorithm design are also welcome.


回答1:


The only trick I know is that when you're summing a bunch of numbers, don't do them one at a time - group them so that the additions are on numbers of approximately the same magnitude. To sum a huge array of random numbers for example, recursively sum by pairs.




回答2:


Numerical analysis is a whole field of mathematics and it isn't reduced to some tips one can apply blindly.




回答3:


People get PhD's writing about this stuff, so you won't get really solid advice here, just tips. One tip is to avoid subtracting numbers that are fairly close in value; that amplifies the effect of the noise bits.




回答4:


You can enable extended floating point precision of the FPU to use 10 bytes internally. This is what we use.

http://www.website.masmforum.com/tutorials/fptute/fpuchap1.htm

You can also sort numbers so that operations are performed on numbers of similar magnitude.




回答5:


There are many small things you can do such as doing as many floating point operations as possible in a single expression and making sure that all inputs to the operation are converted into floating point format. When switching between floating point and integers make sure to add a factor of 0.5 to the float before the integer conversion to ensure values are rounded to the closest integer. Using doubles or long doubles will increase the amount of precision and thus lessen the significance of the rounding/accumulated errors.

You will have some amount of roundoff errors, so you really want to push them past the significance you're looking for. One option for this would be using an extended precision floating point software library, such as the High Precision Arithmetic Library. Using a library has a benefit of higher precision at the cost of slower operation.



来源:https://stackoverflow.com/questions/12487659/how-to-reduce-c-c-floating-point-roundoff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!