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

查看:338
本文介绍了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文档.确实值得研究,尽管在初次通过时可能看起来有些不知所措.同样,第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天全站免登陆