如今何时使用定点
对于密集的数字运算,我正在考虑使用定点而不是浮点。 当然,定点类型的大小有多少字节、它将在什么 CPU 上运行、如果我可以使用(对于英特尔)MMX 或 SSE 或任何新出现的东西,这都很重要......
我'我想知道现在浮点运行速度比以往任何时候都快,是否值得考虑定点? 是否有一般的经验法则可以让我们说它的影响超过几个百分点? 35,000 英尺的数值表现概览如何? (顺便说一句,我假设大多数计算机中都使用通用 CPU,而不是 DSP 或专用嵌入式系统。)
For intense number-crunching i'm considering using fixed point instead of floating point. Of course it'll matter how many bytes the fixed point type is in size, on what CPU it'll be running on, if i can use (for Intel) the MMX or SSE or whatever new things come up...
I'm wondering if these days when floating point runs faster than ever, is it ever worth considering fixed point? Are there general rules of thumb where we can say it'll matter by more than a few percent? What is the overview from 35,000 feet of numerical performance? (BTW, i'm assuming a general CPU as found in most computers, not DSP or specialized embedded systems.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
还是值得的。 浮点比过去更快,但定点也一样。 如果您关心的精度超出了 IEEE 754 所保证的范围,那么固定仍然是唯一的方法。
It's still worth it. Floating point is faster than in the past, but fixed-point is also. And fixed is still the only way to go if you care about precision beyond that guaranteed by IEEE 754.
在处理大量数据的情况下,定点可以使内存效率提高两倍,例如,四字节长整数而不是八字节双精度数。 大型地理空间数据集中经常使用的一种技术是将所有数据减少到一个共同的来源,以便可以处理最高有效位,并使用其余的定点整数。 仅当该点确实浮动时,浮点才重要,即您正在以非常高的精度处理非常广泛的数字。
In situations where you are dealing with very large amounts of data, fixed point can be twice as memory efficient, e.g. a four byte long integer as opposed to an eight byte double. A technique often used in large geospatial datasets is to reduce all the data to a common origin, such that the most significant bits can be disposed of, and work with fixed point integers for the rest. Floating point is only important if the point does actually float, i.e. you are dealing with a very wide range of numbers at very high accuracy.
使用固定小数的另一个好理由是舍入更简单且可预测。 大多数财务软件使用半偶数舍入的定点任意精度小数来表示金钱。
Another good reason to use fixed decimal is that rounding is much simpler and predictable. Most of the financial software uses fixed point arbitrary precision decimals with half-even rounding to represent money.
使用定点几乎总是更快(x86、pentium、68k 和 ARM 的经验)。 不过,它也可能取决于应用程序类型。 对于图形编程(我对定点的主要用途之一),我已经能够使用预构建的余弦表、对数表等来优化代码。而且基本的数学运算也被证明更快。
对财务软件的评论。 在之前的回答中曾说过,定点对于财务计算很有用。 根据我自己的经验(大型财务管理系统的开发和信用卡处理的丰富经验),我不会使用定点。 使用浮点或定点都会产生舍入误差。 我们总是使用整数来表示货币金额,并计算可能的最小金额(欧元或美元为 1c)。 这可确保不会丢失部分金额。 当进行复杂计算时,值会转换为双精度,应用程序特定的舍入规则将被应用,结果将转换回整数。
Its nearly ALWAYS faster to use fixed point (experience of x86, pentium, 68k and ARM). It can, though, also depend on the application type. For graphics programming (one of my main uses of fixed point) I've been able to optimize the code using prebuilt cosine tables, log tables etc. But also the basic mathematical operations have also proven faster.
A comment on financial software. It was said in an earlier answer that fixed point is useful for financial calculations. In my own experience (development of large treasury management system and extensive experience of credit card processing) I would NOT use fixed point. You will have rounding errors using either floating or fixed point. We always use whole amounts to represent monetary amounts, counting the minimum amount possible (1c for Euro or dollar). This ensure no partial amounts are ever lost. When doing complex calculations values are converted to doubles, application specific rounding rules are applied and results are converted back to whole numbers.
当硬件不支持浮点或硬件实现很糟糕时,请使用定点。
为其上课时也要小心。 当涉及到由于(不必要的)类副本而进行分析时,您认为很快的东西实际上可能会变成一条狗。 不过,这是另一个问题。
Use fixed-point when the hardware doesn't support floating-point or the hardware implementation sucks.
Also beware when making classes for it. Something you think would be quick could actually turn out to be a dog when it comes to profiling due to (un)necessary copies of classes. That is another question for another time however.
使用定点的另一个原因是
ARM
设备,如手机和平板电脑,缺乏FPU(至少其中很多)。对于开发实时应用程序,使用定点算法优化函数是有意义的。 FFT(快速傅立叶变换)的实现对于图形非常重要,其效率的提高依赖于浮点运算。
Another reason to use fixed-point is that
ARM
devices, like mobile phones and tablets, lack of FPU (at least many of them).For developing real-time applications it makes sense to optimize functions using fixed-point arithmetic. There are implementations of FFTs (Fast Fourier Transform), very importan for graphics, that base its improvements on efficiency on relying on floating point arithmetic.
由于您使用的是通用 CPU,我建议不要使用定点,除非性能对于您的应用程序至关重要,以至于您必须计算每次抽动。 当你有一个 CPU 时,实现定点和处理溢出等问题的麻烦是不值得的,它会为你做这件事。
恕我直言,仅当您使用不支持浮点运算的硬件的 DSP 时才需要定点。
Since you are using a general-purpose CPU, I would suggest not using fixed point, unless performance is so critical for your application that you have to count every tic. The hassle of implementing fixed point, and dealing with issues like overflow is just not worth it, when you have a CPU, which will do it for you.
IMHO, fixed point is only necessary when you are using a DSP without hardware support for floating point operations.