Lua和Javascript之间的不同浮点数

为什么在Lua中以下计算结果为

Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> print(6.4620332164+14)
20.4620332164

但在Javascript中

console.log(6.4620332164+14)
VM208:1 20.462033216400002

或Python中

Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
>>> print(6.4620332164+14)
20.462033216400002

Lua的浮点数实现有什么特别之处,假设它们都是双精度IEEE 754码吗?

点赞
用户2226988
用户2226988

print 函数对每个参数调用 tostring

> print(20.462033216400002)
20.4620332164

因此,尝试使用以下方式:

> print(string.format("%2.15f", 20.462033216400002))
20.462033216400002

这是 IEEE-754 标准的双精度浮点数。

2019-03-06 17:39:07