【问题标题】:Different number floating point number between lua and javascriptlua和javascript之间的不同数字浮点数
【发布时间】:2019-07-28 10:47:04
【问题描述】:

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

    print 在每个参数上调用tostring

    > print(20.462033216400002)
    20.4620332164
    

    所以,试试

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

    只需 IEEE-754 加倍。

    【讨论】:

      猜你喜欢
      • 2023-01-21
      • 2010-11-06
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      相关资源
      最近更新 更多