summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-11-04 17:16:34 +0100
committerantirez <antirez@gmail.com>2015-11-04 17:16:34 +0100
commit71aa9b75f2c4dd6faa20a4849ff732536bdfceab (patch)
treea200de83a3eee77b96a5168f877eab775fb872ce
parentf6255703b0bd4996ab2278e1253796468aed108a (diff)
downloadredis-71aa9b75f2c4dd6faa20a4849ff732536bdfceab.tar.gz
Fix HINCRBYFLOAT to work with long doubles.
During the refactoring needed for lazy free, specifically the conversion of t_hash from struct robj to plain SDS strings, HINCRBFLOAT was accidentally moved away from long doubles to doubles for internal processing of increments and formatting. The diminished precision created more obvious artifacts in the way small numbers are formatted once we convert from decimal number in radix 10 to double and back to its string in radix 10. By using more precision, we now have less surprising results at least with small numbers like "1.23", exactly like in the previous versions of Redis. See issue #2846.
-rw-r--r--src/t_hash.c8
-rw-r--r--src/util.c6
-rw-r--r--src/util.h2
3 files changed, 8 insertions, 8 deletions
diff --git a/src/t_hash.c b/src/t_hash.c
index 7da8fb171..c75b391d7 100644
--- a/src/t_hash.c
+++ b/src/t_hash.c
@@ -596,23 +596,23 @@ void hincrbyCommand(client *c) {
}
void hincrbyfloatCommand(client *c) {
- double value, incr;
+ long double value, incr;
long long ll;
robj *o;
sds new;
unsigned char *vstr;
unsigned int vlen;
- if (getDoubleFromObjectOrReply(c,c->argv[3],&incr,NULL) != C_OK) return;
+ if (getLongDoubleFromObjectOrReply(c,c->argv[3],&incr,NULL) != C_OK) return;
if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
if (hashTypeGetValue(o,c->argv[2]->ptr,&vstr,&vlen,&ll) == C_OK) {
if (vstr) {
- if (string2d((char*)vstr,vlen,&value) == 0) {
+ if (string2ld((char*)vstr,vlen,&value) == 0) {
addReplyError(c,"hash value is not a float");
return;
}
} else {
- value = (double)ll;
+ value = (long double)ll;
}
} else {
value = 0;
diff --git a/src/util.c b/src/util.c
index c734b1b6d..c1494e021 100644
--- a/src/util.c
+++ b/src/util.c
@@ -426,9 +426,9 @@ int string2l(const char *s, size_t slen, long *lval) {
* Note that this function demands that the string strictly represents
* a double: no spaces or other characters before or after the string
* representing the number are accepted. */
-int string2d(const char *s, size_t slen, double *dp) {
+int string2ld(const char *s, size_t slen, long double *dp) {
char buf[256];
- double value;
+ long double value;
char *eptr;
if (slen >= sizeof(buf)) return 0;
@@ -436,7 +436,7 @@ int string2d(const char *s, size_t slen, double *dp) {
buf[slen] = '\0';
errno = 0;
- value = strtod(buf, &eptr);
+ value = strtold(buf, &eptr);
if (isspace(buf[0]) || eptr[0] != '\0' ||
(errno == ERANGE &&
(value == HUGE_VAL || value == -HUGE_VAL || value == 0)) ||
diff --git a/src/util.h b/src/util.h
index d28049eaf..d7784495b 100644
--- a/src/util.h
+++ b/src/util.h
@@ -41,7 +41,7 @@ uint32_t sdigits10(int64_t v);
int ll2string(char *s, size_t len, long long value);
int string2ll(const char *s, size_t slen, long long *value);
int string2l(const char *s, size_t slen, long *value);
-int string2d(const char *s, size_t slen, double *dp);
+int string2ld(const char *s, size_t slen, long double *dp);
int d2string(char *buf, size_t len, double value);
int ld2string(char *buf, size_t len, long double value, int humanfriendly);
sds getAbsolutePath(char *filename);