Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
别团等shy哥发育
redis
提交
672b0a1b
R
redis
项目概览
别团等shy哥发育
/
redis
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
672b0a1b
编写于
3月 08, 2011
作者:
P
Pieter Noordhuis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fast conversion of double when representable as long long
上级
45290ad9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
41 addition
and
0 deletion
+41
-0
src/redis.h
src/redis.h
+1
-0
src/util.c
src/util.c
+40
-0
未找到文件。
src/redis.h
浏览文件 @
672b0a1b
...
...
@@ -887,6 +887,7 @@ int stringmatchlen(const char *pattern, int patternLen,
int
stringmatch
(
const
char
*
pattern
,
const
char
*
string
,
int
nocase
);
long
long
memtoll
(
const
char
*
p
,
int
*
err
);
int
ll2string
(
char
*
s
,
size_t
len
,
long
long
value
);
int
d2string
(
char
*
s
,
size_t
len
,
double
value
);
int
isStringRepresentableAsLong
(
sds
s
,
long
*
longval
);
int
isStringRepresentableAsLongLong
(
sds
s
,
long
long
*
longval
);
int
isObjectRepresentableAsLongLong
(
robj
*
o
,
long
long
*
llongval
);
...
...
src/util.c
浏览文件 @
672b0a1b
#include "redis.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
/* Glob-style pattern matching. */
int
stringmatchlen
(
const
char
*
pattern
,
int
patternLen
,
...
...
@@ -200,6 +201,45 @@ int ll2string(char *s, size_t len, long long value) {
return
l
;
}
/* Convert a double to a string representation. Returns the number of bytes
* required. The representation should always be parsable by stdtod(3). */
int
d2string
(
char
*
buf
,
size_t
len
,
double
value
)
{
if
(
isnan
(
value
))
{
len
=
snprintf
(
buf
,
len
,
"nan"
);
}
else
if
(
isinf
(
value
))
{
if
(
value
<
0
)
len
=
snprintf
(
buf
,
len
,
"-inf"
);
else
len
=
snprintf
(
buf
,
len
,
"inf"
);
}
else
if
(
value
==
0
)
{
/* See: http://en.wikipedia.org/wiki/Signed_zero, "Comparisons". */
if
(
1
.
0
/
value
<
0
)
len
=
snprintf
(
buf
,
len
,
"-0"
);
else
len
=
snprintf
(
buf
,
len
,
"0"
);
}
else
{
#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
/* Check if the float is in a safe range to be casted into a
* long long. We are assuming that long long is 64 bit here.
* Also we are assuming that there are no implementations around where
* double has precision < 52 bit.
*
* Under this assumptions we test if a double is inside an interval
* where casting to long long is safe. Then using two castings we
* make sure the decimal part is zero. If all this is true we use
* integer printing function that is much faster. */
double
min
=
-
4503599627370495
;
/* (2^52)-1 */
double
max
=
4503599627370496
;
/* -(2^52) */
if
(
val
>
min
&&
val
<
max
&&
value
==
((
double
)((
long
long
)
value
)))
len
=
ll2string
(
buf
,
len
,(
long
long
)
value
);
else
#endif
len
=
snprintf
(
buf
,
len
,
"%.17g"
,
value
);
}
return
len
;
}
/* Check if the sds string 's' can be represented by a long long
* (that is, is a number that fits into long without any other space or
* character before or after the digits, so that converting this number
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录