redis lua - eval returns 处理 Long.MAX_VALUE 时的错误值
redis lua - eval returns wrong value when dealing with Long.MAX_VALUE
127.0.0.1:7501> 评估 "return {1,2,9223372036854775807}" 0
1) (整数) 1
2) (整数) 2
3) (整数) -9223372036854775808
请帮助这个奇怪的行为。我知道 lua 只能表示超过 10^15 的大数,因为它会失去一些精度。
我希望 return 值为“9.2233720368548e+18”
但不是负数。
也供你参考
127.0.0.1:7501> 评估 "return {1,2,tostring(9223372036854775807)}" 0
1) (整数) 1
2) (整数) 2
3) "9.2233720368548e+18"
127.0.0.1:7501> 评估 "return {1,2,tonumber(9223372036854775807)}" 0
1) (整数) 1
2) (整数) 2
3) (整数) -9223372036854775808
摘自文档页面 (https://redis.io/commands/eval):
Lua has a single numerical type, Lua numbers. There is no distinction between integers and floats. So we always convert Lua numbers into integer replies, removing the decimal part of the number if any. If you want to return a float from Lua you should return it as a string, exactly like Redis itself does (see for instance the ZSCORE command).
您使用的数字 (~10^19) 太大,无法在 Lua 中表示为整数,因此它变成了浮点数。 Redis 类型转换为整数时溢出,变成负数
127.0.0.1:7501> 评估 "return {1,2,9223372036854775807}" 0
1) (整数) 1
2) (整数) 2
3) (整数) -9223372036854775808
请帮助这个奇怪的行为。我知道 lua 只能表示超过 10^15 的大数,因为它会失去一些精度。 我希望 return 值为“9.2233720368548e+18” 但不是负数。
也供你参考
127.0.0.1:7501> 评估 "return {1,2,tostring(9223372036854775807)}" 0
1) (整数) 1
2) (整数) 2
3) "9.2233720368548e+18"
127.0.0.1:7501> 评估 "return {1,2,tonumber(9223372036854775807)}" 0
1) (整数) 1
2) (整数) 2
3) (整数) -9223372036854775808
摘自文档页面 (https://redis.io/commands/eval):
Lua has a single numerical type, Lua numbers. There is no distinction between integers and floats. So we always convert Lua numbers into integer replies, removing the decimal part of the number if any. If you want to return a float from Lua you should return it as a string, exactly like Redis itself does (see for instance the ZSCORE command).
您使用的数字 (~10^19) 太大,无法在 Lua 中表示为整数,因此它变成了浮点数。 Redis 类型转换为整数时溢出,变成负数