Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
36320262
Commit
36320262
authored
Dec 03, 2014
by
antirez
Browse files
Handle infinite explicitly in createStringObjectFromLongLong().
parent
92c5ab40
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/object.c
View file @
36320262
...
@@ -111,18 +111,30 @@ robj *createStringObjectFromLongLong(long long value) {
...
@@ -111,18 +111,30 @@ robj *createStringObjectFromLongLong(long long value) {
/* Create a string object from a long double. If humanfriendly is non-zero
/* Create a string object from a long double. If humanfriendly is non-zero
* it does not use exponential format and trims trailing zeroes at the end,
* it does not use exponential format and trims trailing zeroes at the end,
* however this result in loss of precision. Otherwise exp format is used
* however this results in loss of precision. Otherwise exp format is used
* and the output of snprintf() is not modified. */
* and the output of snprintf() is not modified.
*
* The 'humanfriendly' option is used for INCRBYFLOAT and HINCRBYFLOAT. */
robj
*
createStringObjectFromLongDouble
(
long
double
value
,
int
humanfriendly
)
{
robj
*
createStringObjectFromLongDouble
(
long
double
value
,
int
humanfriendly
)
{
char
buf
[
256
];
char
buf
[
256
];
int
len
;
int
len
;
/* We use 17 digits precision since with 128 bit floats that precision
if
(
isinf
(
value
))
{
* after rounding is able to represent most small decimal numbers in a way
/* Libc in odd systems (Hi Solaris!) will format infinite in a
* that is "non surprising" for the user (that is, most small decimal
* different way, so better to handle it in an explicit way. */
* numbers will be represented in a way that when converted back into
if
(
value
>
0
)
{
* a string are exactly the same as what the user typed.) */
memcpy
(
buf
,
"inf"
,
3
);
if
(
humanfriendly
)
{
len
=
3
;
}
else
{
memcpy
(
buf
,
"-inf"
,
4
);
len
=
4
;
}
}
else
if
(
humanfriendly
)
{
/* We use 17 digits precision since with 128 bit floats that precision
* after rounding 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
),
"%.17Lf"
,
value
);
len
=
snprintf
(
buf
,
sizeof
(
buf
),
"%.17Lf"
,
value
);
/* Now remove trailing zeroes after the '.' */
/* Now remove trailing zeroes after the '.' */
if
(
strchr
(
buf
,
'.'
)
!=
NULL
)
{
if
(
strchr
(
buf
,
'.'
)
!=
NULL
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment