lua和javascript之间的不同数字浮点数

问题描述 投票:1回答:1

为什么在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

假设它们都是双精度IEE 754,这里的lua浮点数实现有什么特别之处?

lua precision floating-accuracy
1个回答
4
投票

print在每个论点上都称tostring

> print(20.462033216400002)
20.4620332164

所以,试试吧

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

简单地IEEE-754加倍。

© www.soinside.com 2019 - 2024. All rights reserved.