开发者

Why is Lua arithmetic is not equal to itself? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What is a simple example of floating point/rounding error?

When I execute the follo开发者_JAVA技巧wing Lua code:

a = 5.6
b = 14 * 0.4
c = 11.2 / 2
d = 28 * 0.2
print( a == b )
print( a == c )
print( a == d )
print( b == c )
print( b == d )
print( c == d )

I get the following results:

false
true
false
false
true
false

Can someone explain why 14 * 0.4 and 28 * 0.2 are not equal to 5.6?

Thanks


You are dealing with the natural imprecision of binary floating point numbers. Your number a might be 5.5999999999999996 and your result b might be 5.6000000000000005, which are not equal.

In fact, here is the same calculation as done by Python:

>>> 5.6
5.5999999999999996
>>> 14 * 0.4
5.6000000000000005

This behaviour is common to all implementations of binary floating point numbers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜