Lua:减去十进制数不会返回正确的精度 [英] Lua: subtracting decimal numbers doesn't return correct precision

查看:26
本文介绍了Lua:减去十进制数不会返回正确的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Lua 5.1

I am using Lua 5.1

print(10.08 - 10.07)

上面没有打印 0.01,而是打印 0.0099999999999998.

Rather than printing 0.01, above prints 0.0099999999999998.

知道如何从这个减法中得到 0.01 吗?

Any idea how to get 0.01 form this subtraction?

推荐答案

你从减法中得到了 0.01.它只是一个重复的小数形式,精度损失很小.

You got 0.01 from the subtraction. It is just in the form of a repeating decimal with a tiny amount of lost precision.

Lua 使用 C 类型 double 来表示数字.这几乎在您将使用的每个平台上都是一个 64 位二进制浮点值,具有大约 23 位十进制数字的精度.然而,任何精度都不足以用二进制精确表示 0.01.这种情况类似于试图用十进制写 1/3.

Lua uses the C type double to represent numbers. This is, on nearly every platform you will use, a 64-bit binary floating point value with approximately 23 decimal digits of precision. However, no amount of precision is sufficient to represent 0.01 exactly in binary. The situation is similar to attempting to write 1/3 in decimal.

此外,您要减去两个幅度非常相似的值.这本身会导致额外的精度损失.

Furthermore, you are subtracting two values that are very similar in magnitude. That all by itself causes an additional loss of precision.

解决方案取决于您的上下文.如果您正在做会计,那么我强烈建议您不要使用浮点值来表示帐户值,因为这些小错误会累积,最终整分(或更糟)会出现或消失.最好以整数分存储帐户,然后除以 100 显示.

The solution depends on what your context is. If you are doing accounting, then I would strongly recommend that you not use floating point values to represent account values because these small errors will accumulate and eventually whole cents (or worse) will appear or disappear. It is much better to store accounts in integer cents, and divide by 100 for display.

一般来说,答案是要注意浮点固有的问题,其中之一就是这种微小的精度损失.通过将答案四舍五入到适当的数字位数进行显示,并且从不比较计算结果是否相等,很容易处理.

In general, the answer is to be aware of the issues that are inherent to floating point, and one of them is this sort of small loss of precision. It is easily handled by rounding answers to an appropriate number of digits for display, and never comparing results of calculations for equality.

一些背景资源:

  • The semi-official explanation at the Lua Users Wiki
  • This great page of IEEE floating point calculators where you can enter values in decimal and see how they are represented, and vice-versa.
  • Wiki on IEEE floating point.
  • Wiki on floating point numbers in general.
  • What Every Computer Scientist Should Know About Floating-Point Arithmetic is the most complete discussion of the fine details.

毕竟添加了 WECSSKAFPA 文档.这确实值得研究,尽管在第一次通过时可能看起来有点压倒性.同样,Knuth Volume 2 在关于浮点的一般和大节.而且,由于 lhf 提醒我它的存在,我插入了 Lua wiki 解释为什么浮点可以用作唯一的数字类型作为列表中的第一项.

Added the WECSSKAFPA document after all. It really is worth the study, although it will likely seem a bit overwhelming on the first pass. In the same vein, Knuth Volume 2 has extensive coverage of arithmetic in general and a large section on floating point. And, since lhf reminded me of its existence, I inserted the Lua wiki explanation of why floating point is ok to use as the sole numeric type as the first item on the list.

这篇关于Lua:减去十进制数不会返回正确的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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