Skip to content
Browse files

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

…n of the command result.

Default to 17.
  • Loading branch information...
1 parent a1fbb32 commit 776d58fe5dec607d1376cbf098de233e4c7cdae8 @slact committed Mar 31, 2012
Showing with 25 additions and 6 deletions.
  1. +5 −0 src/config.c
  2. +1 −6 src/object.c
  3. +1 −0 src/redis.c
  4. +12 −0 src/redis.h
  5. +6 −0 src/util.c
View
5 src/config.c
@@ -278,6 +278,8 @@ void loadServerConfigFromString(char *config) {
server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
} else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
server.zset_max_ziplist_value = memtoll(argv[1], NULL);
+ } else if (!strcasecmp(argv[0],"incrbyfloat-precision") && argc == 2) {
+ longDoubleFormatString(atoi(argv[1]), server.string_from_long_double_format);
} else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
struct redisCommand *cmd = lookupCommand(argv[1]);
int retval;
@@ -542,6 +544,9 @@ void configSetCommand(redisClient *c) {
} else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
server.zset_max_ziplist_value = ll;
+ } else if (!strcasecmp(c->argv[2]->ptr,"incrbyfloat-precision")) {
+ if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
+ longDoubleFormatString((int)ll, server.string_from_long_double_format);
} else if (!strcasecmp(c->argv[2]->ptr,"lua-time-limit")) {
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
server.lua_time_limit = ll;
View
7 src/object.c
@@ -51,12 +51,7 @@ robj *createStringObjectFromLongDouble(long double value) {
char buf[256];
int len;
- /* We use 17 digits precision since with 128 bit floats that precision
- * after rouding is able to represent most small decimal numbers in a way
- * that is "non surprising" for the user (that is, most small decimal
- * numbers will be represented in a way that when converted back into
- * a string are exactly the same as what the user typed.) */
- len = snprintf(buf,sizeof(buf),"%.17Lg", value);
+ len = snprintf(buf,sizeof(buf), server.string_from_long_double_format, value);
return createStringObject(buf,len);
}
View
1 src/redis.c
@@ -1038,6 +1038,7 @@ void initServerConfig() {
server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES;
server.zset_max_ziplist_entries = REDIS_ZSET_MAX_ZIPLIST_ENTRIES;
server.zset_max_ziplist_value = REDIS_ZSET_MAX_ZIPLIST_VALUE;
+ longDoubleFormatString(REDIS_STRING_MAX_FLOAT_DIGITS_AFTER_DECIMAL, server.string_from_long_double_format);
server.shutdown_asap = 0;
server.repl_ping_slave_period = REDIS_REPL_PING_SLAVE_PERIOD;
server.repl_timeout = REDIS_REPL_TIMEOUT;
View
12 src/redis.h
@@ -216,6 +216,15 @@
#define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128
#define REDIS_ZSET_MAX_ZIPLIST_VALUE 64
+/* Floating point to string conversions */
+/* We use 17 digits precision since with 80 bit floats (the default long
+ * double on x86) that precision) after rouding is able to represent most
+ * small decimal numbers in a way that is "non surprising" for the user
+ * (that is, most small decimal numbers will be represented in a way that
+ * when converted back into a string are exactly the same as what
+ * the user typed.) */
+#define REDIS_STRING_MAX_FLOAT_DIGITS_AFTER_DECIMAL 17
+
/* Sets operations codes */
#define REDIS_OP_UNION 0
#define REDIS_OP_DIFF 1
@@ -567,6 +576,8 @@ struct redisServer {
size_t set_max_intset_entries;
size_t zset_max_ziplist_entries;
size_t zset_max_ziplist_value;
+ /* INCRBYFLOAT and HINCRBYFLOAT precision */
+ char string_from_long_double_format[128]; /* format string for LongDoubleObject to stringObject conversion */
time_t unixtime; /* Unix time sampled every second. */
/* Pubsub */
dict *pubsub_channels; /* Map channels to list of subscribed clients */
@@ -694,6 +705,7 @@ extern dictType hashDictType;
long long ustime(void);
long long mstime(void);
void getRandomHexChars(char *p, unsigned int len);
+void longDoubleFormatString(int precision, char *format);
/* networking.c -- Networking and Client related operations */
redisClient *createClient(int fd);
View
6 src/util.c
@@ -329,6 +329,12 @@ int d2string(char *buf, size_t len, double value) {
return len;
}
+/* Generate format string for sprintf conversion of long double to string
+ * with given amount for precision past the decimal point */
+void longDoubleFormatString(int precision, char *format) {
+ snprintf(format, 128, "%%.%iLf", precision);
+}
+
/* Generate the Redis "Run ID", a SHA1-sized random number that identifies a
* given execution of Redis, so that if you are talking with an instance
* 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.