限制浮点精度问题的做法 [英] Practices to limit floating point accuracy problems

查看:125
本文介绍了限制浮点精度问题的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为程序员,大多数(如果不是全部的话)知道浮点数有一个不太准确的倾向。我知道这个问题不能完全避免,但我想知道是否有任何可以用来至少减少浮点错误的某些做法,模式等。



在此先感谢。

解决方案

使用可以处理已知有限精度的定点数学。例如,Rockbox音乐播放器固件使用几乎完全定点媒体编解码器。



如果您必须完全准确,使用GMP库提供的无限长存储类型。



如果你只是想减少你的错误,接近于零的地方,IEEE FP编号更精确。重新排序你的操作,以避免让你的绝对值太大。


As programmers, most (if not all of us) know that floating point numbers have a tendency to not be very accurate. I know that this problem can't be avoided entirely, but I am wondering if there are any certain practices, patterns, etc. that can be used to at least reduce floating point errors.

Thanks in advance.

解决方案

Use fixed-point mathematics where you can deal with a known limited precision.

As an example, the Rockbox music player firmware uses almost entirely fixed-point media codecs.

If you must be perfectly accurate, use an infinite-length storage type like those provided by the GMP library.

If you're just trying to cut down on your errors, try to work as close to zero as possible, where the IEEE FP numbers are more precise. Reorder your operations to avoid letting your absolute values get too large.

这篇关于限制浮点精度问题的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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