防止舍入错误 [英] Preventing Rounding Errors

查看:69
本文介绍了防止舍入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在阅读有关C ++中的舍入错误的信息.因此,如果我要编写一个数学密集型程序(或任何重要的计算),我应该只将浮点数全部放在一起并且仅使用双精度数还是有一种更简单的方法来防止舍入错误?

I was just reading about rounding errors in C++. So, if I'm making a math intense program (or any important calculations) should I just drop floats all together and use only doubles or is there an easier way to prevent rounding errors?

推荐答案

必修课:每个程序员应了解的浮点算法.

此外,请尝试阅读 IEEE浮点标准.

您总是会遇到舍入错误.除非您使用 infinite 任意精度库,例如 gmplib .您必须确定您的应用程序是否真的需要这种工作.

You'll always get rounding errors. Unless you use an infinite arbitrary precision library, like gmplib. You have to decide if your application really needs this kind of effort.

或者,您可以使用整数运算,仅在需要时才转换为浮点数.这仍然很难做到,您必须决定是否值得.

Or, you could use integer arithmetic, converting to floats only when needed. This is still hard to do, you have to decide if it's worth it.

最后,可以使用floatdouble小心,不要在表示精度的限制下对值进行假设.我希望此Valgrind插件已实现(frep的grep)...

Lastly, you can use float or double taking care not to make assumption about values at the limit of representation's precision. I'd wish this Valgrind plugin was implemented (grep for float)...

这篇关于防止舍入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆