redis 现象求解

2017-10-11 20:35:31 +08:00
 zcj2898

用 redis 存储大整数,出现精度丢失问题。网上查了下找不到有关信息。有没有大佬来解释一波?感激不尽!以及这个问题如何解决?

127.0.0.1:6379> zadd test 14338073069842465 2239

(integer) 0

127.0.0.1:6379> zscore test 2239

"14338073069842464"//储存 65,拿到 64

127.0.0.1:6379> zadd test 14338073069842466 2239

(integer) 0

127.0.0.1:6379> zscore test 2239

"14338073069842466"//储存 66,拿到 66

127.0.0.1:6379> zadd test 14338073069842463 2239

(integer) 0

127.0.0.1:6379> zscore test 2239

"14338073069842464"//储存 63,拿到 64

127.0.0.1:6379> zadd test 14338073069842462 2239

(integer) 0

127.0.0.1:6379> zscore test 2239

"14338073069842462"//储存 62,拿到 62

6362 次点击
所在节点    Redis
18 条回复
crystom
2017-10-11 20:44:59 +08:00
9007199254740992
crystom
2017-10-11 20:45:50 +08:00
Redis sorted sets use a double 64-bit floating point number to represent the score. In all the architectures we support, this is represented as an IEEE 754 floating point number, that is able to represent precisely integer numbers between -(2^53) and +(2^53) included. In more practical terms, all the integers between -9007199254740992 and 9007199254740992 are perfectly representable. Larger integers, or fractions, are internally represented in exponential form, so it is possible that you get only an approximation of the decimal number, or of the very big integer, that you set as score
zcj2898
2017-10-11 20:49:18 +08:00
@crystom 谢谢了,用来实现游戏排行,高 32bit 是排行值,低 32bit 是时间戳,有什么解决方案吗~
des
2017-10-11 20:52:23 +08:00
@zcj2898 为什么要这么存?是有什么特殊需求嘛?
zcj2898
2017-10-11 20:58:07 +08:00
@des 游戏从业者,比如英雄排行榜数据,依次比较英雄的战力、等级、星级、阶级如果都一样就比先到这个战力的时间戳,依次把这些值序列后就很容易造成 value 很大超过楼上说的 53 位精度。
daybyday
2017-10-11 21:19:41 +08:00
合理分配好:"战力、等级、星级、阶级、时间戳" 各项占用的位数,使得 score 值总大小在精度不丢失范围内
你这场景,比较好的解决方案是:牺牲时间戳的精度,单位存分,甚至存小时,另外存成偏移值,而不是绝对值,也能省一些
crystom
2017-10-11 21:46:23 +08:00
在 redis2.6 上可能可以使用 lua script 解决(我没实际用过),不过我比较赞同楼上,如果让我做的话,会在业务上妥协精度。
WinterWu
2017-10-11 22:29:48 +08:00
1. 如楼上所言,想办法缩减长度,牺牲点时间戳精度之类 /尤其看需要的比较时间范围,用偏移值解决,比如今天零点开始的偏移值,可以算算去掉多少位数
2. 改成字符串格式,利用字符串排序
3. 分段-类似分桶,将分值按照范围分别保存和排序,然后在每个范围内仅保存偏移
zcj2898
2017-10-11 23:48:08 +08:00
感谢各位回复,我缩减一下各数据所占的位数。就是排行榜原来的数据要作废了。
wsy2220
2017-10-11 23:52:56 +08:00
存字符串吧
lslqtz
2017-10-12 00:11:38 +08:00
@zcj2898 对原来的数据进行解析转成新数据嘛
dangyuluo
2017-10-12 00:17:06 +08:00
原来的数据也不用作废。写个小程序转换一下就好。
czheo
2017-10-12 03:47:48 +08:00
可以存 score + timestamp/2^31。(2^31 - 1 是 timestamp 最大值,可以用到 2038 年)
比如积分 100,timestamp= 1507750681,存 100 + 1507750681/2^31 = 100.70210112305。
当 score 不够大,精度准确。score 过大,小数位精度会自动丢失。

如果为了 debug 方便,也可以存 score + timestamp/10^10。
crystom
2017-10-12 08:57:11 +08:00
@czheo 浮点浮点,除以几的话和不除差不多的
blackshadow
2017-10-12 09:13:53 +08:00
base64 一下,用的时候在解开不就好了。
zhx1991
2017-10-12 12:56:17 +08:00
存成 string 自己转
yexiangyang
2017-10-12 16:00:55 +08:00
@zhx1991 zadd 不支持字符串分值
czheo
2017-10-12 16:12:06 +08:00
@crystom 想想好像确实差不多。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/396897

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX