Search code examples
luaprecisionfloating-accuracy

Different number floating point number between lua and javascript


Why in lua the following computation is

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

While in Javascript

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

or python its

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

What is special about the lua floating point number implementation here assuming they are all double precision IEE 754?


Solution

  • print calls tostring on each argument.

    > print(20.462033216400002)
    20.4620332164
    

    So, try

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

    Simply IEEE-754 doubles.