Menu

#320 json plugin value anomaly

open
nobody
None
2017-04-25
2017-04-25
Anonymous
No

Originally created by: keszegh

using json.decode
many times if i have a value stored as -139.89
the value restored is -139.88999999999999
etc with most of the values i store like this (all have 2 precision digits like this one).

Discussion

  • Anonymous

    Anonymous - 2017-04-25

    Originally posted by: ar2rsawseen

    It is not a json problem per se, but rather floating point precision problem and how computer stored floating points. So it is mostly Lua or C specific and only thing is to round up the number to what you expect it to be, like 2 number precision
    http://lua-users.org/wiki/SimpleRound

    More info:
    http://stackoverflow.com/questions/3580486/lua-subtracting-decimal-numbers-doesnt-return-correct-precision

     
  • Anonymous

    Anonymous - 2017-04-25

    Originally posted by: keszegh

    perhaps it could be done on the c side to make it faster?
    or if this adds a performance hit then it would be good to be able to add to json decode/encode the required precision as a parameter.

     
  • Anonymous

    Anonymous - 2017-04-25

    Originally posted by: ar2rsawseen

    Can't really do that I think, there could be multiple floats in json which require different precision

     
  • Anonymous

    Anonymous - 2017-04-25

    Originally posted by: keszegh

    thanks for the info.
    and btw. at least if i store an integer, then they remain an integer, right?

     
  • Anonymous

    Anonymous - 2017-04-25

    Originally posted by: ar2rsawseen

    actually I think in Lua all numbers are stored as double underneath, but for integer, without point, at least it can be determined that expected value is integer and is probably rounded internally.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.