127.0.0.1:6379> set a 10000000
OK
127.0.0.1:6379> eval 'return tostring(redis.call("get", "a") + 1.0000008)' 0
"10000001.000001"
I have to do other operations besides addition.
I found that Lua's Number supports up to 14 digits, It is no longer accurate after more than 14. While this scope is sufficient for my current needs, my scalability is limited by this.
Is there any workaround?
Comment From: itamarhaber
Hello @JoverZhang
AFAIK this is a limitation of Lua (also in 5.4, whereas Redis uses 5.1). An alternative is to use Redis' own double-precision floats without relying on Lua's arithmetics, i.e.:
127.0.0.1:6379> SET a 10000000.0
OK
127.0.0.1:6379> INCRBYFLOAT a 1.0000008
"10000001.00000079999972513"
Or with a Lua wrapper:
127.0.0.1:6379> SET a 10000000.0
OK
127.0.0.1:6379> EVAL 'return redis.call("INCRBYFLOAT", KEYS[1], ARGV[1])' 1 a 1.0000008
"10000001.00000079999972513"