whitecatboard / Lua-RTOS-ESP32

Lua RTOS for ESP32
Other
1.2k stars 221 forks source link

cjson encode function isn't exact for real numer #134

Closed lu1210 closed 6 years ago

lu1210 commented 6 years ago

/ > a = 8.8979 / > print(a,require('cjson').encode(a)) 8.8979 8.8978996276855 / > a = tonumber(string.format('%3.1f',a)) / > print(a,require('cjson').encode(a)) 8.9 8.8999996185303 / >

is there a newer cjson lib available?

jolivepetrus commented 6 years ago

@lu1210,

As far I can see this is not a cjson issue. In fact, in the lua-cjson library snprintf is used, using 14 digits of precision that can be changed in programming time.

If you do the same with lua:

a = 8.8979 string.format('%3.14f',a) 8.89789962768555

jolivepetrus commented 6 years ago

Solved in https://github.com/whitecatboard/Lua-RTOS-ESP32/commit/37c9ce090b00ffece6809e0d015cd203308c6ef9