Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added config option incrbyfloat-precision, the decimal point precisio…

…n of the command result.

Default to 17.
  • Loading branch information...
commit 776d58fe5dec607d1376cbf098de233e4c7cdae8 1 parent a1fbb32
Leo P. authored
5 src/config.c
@@ -278,6 +278,8 @@ void loadServerConfigFromString(char *config) {
278 278 server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
279 279 } else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
280 280 server.zset_max_ziplist_value = memtoll(argv[1], NULL);
  281 + } else if (!strcasecmp(argv[0],"incrbyfloat-precision") && argc == 2) {
  282 + longDoubleFormatString(atoi(argv[1]), server.string_from_long_double_format);
281 283 } else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
282 284 struct redisCommand *cmd = lookupCommand(argv[1]);
283 285 int retval;
@@ -542,6 +544,9 @@ void configSetCommand(redisClient *c) {
542 544 } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
543 545 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
544 546 server.zset_max_ziplist_value = ll;
  547 + } else if (!strcasecmp(c->argv[2]->ptr,"incrbyfloat-precision")) {
  548 + if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
  549 + longDoubleFormatString((int)ll, server.string_from_long_double_format);
545 550 } else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
546 551 if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
547 552 server.lua_time_limit = ll;
7 src/object.c
@@ -51,12 +51,7 @@ robj *createStringObjectFromLongDouble(long double value) {
51 51 char buf[256];
52 52 int len;
53 53
54   - /* We use 17 digits precision since with 128 bit floats that precision
55   - * after rouding is able to represent most small decimal numbers in a way
56   - * that is "non surprising" for the user (that is, most small decimal
57   - * numbers will be represented in a way that when converted back into
58   - * a string are exactly the same as what the user typed.) */
59   - len = snprintf(buf,sizeof(buf),"%.17Lg", value);
  54 + len = snprintf(buf,sizeof(buf), server.string_from_long_double_format, value);
60 55 return createStringObject(buf,len);
61 56 }
62 57
1  src/redis.c
@@ -1038,6 +1038,7 @@ void initServerConfig() {
1038 1038 server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES;
1039 1039 server.zset_max_ziplist_entries = REDIS_ZSET_MAX_ZIPLIST_ENTRIES;
1040 1040 server.zset_max_ziplist_value = REDIS_ZSET_MAX_ZIPLIST_VALUE;
  1041 + longDoubleFormatString(REDIS_STRING_MAX_FLOAT_DIGITS_AFTER_DECIMAL, server.string_from_long_double_format);
1041 1042 server.shutdown_asap = 0;
1042 1043 server.repl_ping_slave_period = REDIS_REPL_PING_SLAVE_PERIOD;
1043 1044 server.repl_timeout = REDIS_REPL_TIMEOUT;
12 src/redis.h
@@ -216,6 +216,15 @@
216 216 #define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128
217 217 #define REDIS_ZSET_MAX_ZIPLIST_VALUE 64
218 218
  219 +/* Floating point to string conversions */
  220 +/* We use 17 digits precision since with 80 bit floats (the default long
  221 + * double on x86) that precision) after rouding is able to represent most
  222 + * small decimal numbers in a way that is "non surprising" for the user
  223 + * (that is, most small decimal numbers will be represented in a way that
  224 + * when converted back into a string are exactly the same as what
  225 + * the user typed.) */
  226 +#define REDIS_STRING_MAX_FLOAT_DIGITS_AFTER_DECIMAL 17
  227 +
219 228 /* Sets operations codes */
220 229 #define REDIS_OP_UNION 0
221 230 #define REDIS_OP_DIFF 1
@@ -567,6 +576,8 @@ struct redisServer {
567 576 size_t set_max_intset_entries;
568 577 size_t zset_max_ziplist_entries;
569 578 size_t zset_max_ziplist_value;
  579 + /* INCRBYFLOAT and HINCRBYFLOAT precision */
  580 + char string_from_long_double_format[128]; /* format string for LongDoubleObject to stringObject conversion */
570 581 time_t unixtime; /* Unix time sampled every second. */
571 582 /* Pubsub */
572 583 dict *pubsub_channels; /* Map channels to list of subscribed clients */
@@ -694,6 +705,7 @@ extern dictType hashDictType;
694 705 long long ustime(void);
695 706 long long mstime(void);
696 707 void getRandomHexChars(char *p, unsigned int len);
  708 +void longDoubleFormatString(int precision, char *format);
697 709
698 710 /* networking.c -- Networking and Client related operations */
699 711 redisClient *createClient(int fd);
6 src/util.c
@@ -329,6 +329,12 @@ int d2string(char *buf, size_t len, double value) {
329 329 return len;
330 330 }
331 331
  332 +/* Generate format string for sprintf conversion of long double to string
  333 + * with given amount for precision past the decimal point */
  334 +void longDoubleFormatString(int precision, char *format) {
  335 + snprintf(format, 128, "%%.%iLf", precision);
  336 +}
  337 +
332 338 /* Generate the Redis "Run ID", a SHA1-sized random number that identifies a
333 339 * given execution of Redis, so that if you are talking with an instance
334 340 * having run_id == A, and you reconnect and it has run_id == B, you can be

0 comments on commit 776d58f

Please sign in to comment.
Something went wrong with that request. Please try again.