如何减少C / C ++浮点舍入 [英] How to reduce C/C++ floating-point roundoff

查看:154
本文介绍了如何减少C / C ++浮点舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何通用的提示可以减少C或C ++中浮点舍入错误的累积?我主要考虑如何编写能够编译成最佳汇编语言指令的代码,尽管整体算法设计的策略也是受欢迎的。 解决方案

我知道的唯一技巧是,当你总结一堆数字的时候,不要一次一个地把它们组合起来,把它们加在一起,以便增加的数量大致相同。例如,总结一个庞大的随机数列,按递归方式求和。


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.

解决方案

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.

这篇关于如何减少C / C ++浮点舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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