Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
R
rapidjson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
rapidjson
Commits
23e410b1
Commit
23e410b1
authored
Feb 12, 2016
by
Milo Yip
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #540 from miloyip/issue341_float
Add Value::Get/SetFloat(), Value::IsLossLessFloat/Double()
parents
3cb5733e
8b4c9998
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
0 deletions
+82
-0
document.h
include/rapidjson/document.h
+34
-0
valuetest.cpp
test/unittest/valuetest.cpp
+48
-0
No files found.
include/rapidjson/document.h
View file @
23e410b1
...
...
@@ -788,6 +788,29 @@ public:
bool
IsDouble
()
const
{
return
(
flags_
&
kDoubleFlag
)
!=
0
;
}
bool
IsString
()
const
{
return
(
flags_
&
kStringFlag
)
!=
0
;
}
// Checks whether a number can be losslessly converted to a double.
bool
IsLosslessDouble
()
const
{
if
(
!
IsNumber
())
return
false
;
if
(
IsUint64
())
return
static_cast
<
uint64_t
>
(
static_cast
<
double
>
(
GetUint64
()))
==
GetUint64
();
if
(
IsInt64
())
return
static_cast
<
int64_t
>
(
static_cast
<
double
>
(
GetInt64
()))
==
GetInt64
();
return
true
;
// double, int, uint are always lossless
}
// Checks whether a number is a float (possible lossy).
bool
IsFloat
()
const
{
if
((
flags_
&
kDoubleFlag
)
==
0
)
return
false
;
double
d
=
GetDouble
();
return
d
>=
-
3.4028234e38
&&
d
<=
3.4028234e38
;
}
// Checks whether a number can be losslessly converted to a float.
bool
IsLosslessFloat
()
const
{
if
(
!
IsNumber
())
return
false
;
double
a
=
GetDouble
();
double
b
=
static_cast
<
double
>
(
static_cast
<
float
>
(
a
));
return
a
>=
b
&&
a
<=
b
;
// Prevent -Wfloat-equal
}
//@}
//!@name Null
...
...
@@ -1445,6 +1468,9 @@ public:
int64_t
GetInt64
()
const
{
RAPIDJSON_ASSERT
(
flags_
&
kInt64Flag
);
return
data_
.
n
.
i64
;
}
uint64_t
GetUint64
()
const
{
RAPIDJSON_ASSERT
(
flags_
&
kUint64Flag
);
return
data_
.
n
.
u64
;
}
//! Get the value as double type.
/*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessDouble() to check whether the converison is lossless.
*/
double
GetDouble
()
const
{
RAPIDJSON_ASSERT
(
IsNumber
());
if
((
flags_
&
kDoubleFlag
)
!=
0
)
return
data_
.
n
.
d
;
// exact type, no conversion.
...
...
@@ -1454,11 +1480,19 @@ public:
RAPIDJSON_ASSERT
((
flags_
&
kUint64Flag
)
!=
0
);
return
static_cast
<
double
>
(
data_
.
n
.
u64
);
// uint64_t -> double (may lose precision)
}
//! Get the value as double type.
/*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless.
*/
double
GetFloat
()
const
{
return
static_cast
<
float
>
(
GetDouble
());
}
GenericValue
&
SetInt
(
int
i
)
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
i
);
return
*
this
;
}
GenericValue
&
SetUint
(
unsigned
u
)
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
u
);
return
*
this
;
}
GenericValue
&
SetInt64
(
int64_t
i64
)
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
i64
);
return
*
this
;
}
GenericValue
&
SetUint64
(
uint64_t
u64
)
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
u64
);
return
*
this
;
}
GenericValue
&
SetDouble
(
double
d
)
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
d
);
return
*
this
;
}
GenericValue
&
SetFloat
(
float
f
)
{
this
->~
GenericValue
();
new
(
this
)
GenericValue
(
f
);
return
*
this
;
}
//@}
...
...
test/unittest/valuetest.cpp
View file @
23e410b1
...
...
@@ -579,6 +579,54 @@ TEST(Value, Double) {
EXPECT_NEAR
(
56.78
,
z
.
GetDouble
(),
0.0
);
}
TEST
(
Value
,
Float
)
{
// Constructor with double
Value
x
(
12.34
f
);
EXPECT_EQ
(
kNumberType
,
x
.
GetType
());
EXPECT_NEAR
(
12.34
f
,
x
.
GetFloat
(),
0.0
);
EXPECT_TRUE
(
x
.
IsNumber
());
EXPECT_TRUE
(
x
.
IsDouble
());
EXPECT_TRUE
(
x
.
IsFloat
());
EXPECT_FALSE
(
x
.
IsInt
());
EXPECT_FALSE
(
x
.
IsNull
());
EXPECT_FALSE
(
x
.
IsBool
());
EXPECT_FALSE
(
x
.
IsFalse
());
EXPECT_FALSE
(
x
.
IsTrue
());
EXPECT_FALSE
(
x
.
IsString
());
EXPECT_FALSE
(
x
.
IsObject
());
EXPECT_FALSE
(
x
.
IsArray
());
// SetFloat()
Value
z
;
z
.
SetFloat
(
12.34
f
);
EXPECT_NEAR
(
12.34
f
,
z
.
GetFloat
(),
0.0
f
);
z
=
56.78
f
;
EXPECT_NEAR
(
56.78
f
,
z
.
GetFloat
(),
0.0
f
);
}
TEST
(
Value
,
IsLosslessDouble
)
{
EXPECT_TRUE
(
Value
(
12.34
).
IsLosslessDouble
());
EXPECT_TRUE
(
Value
(
-
123
).
IsLosslessDouble
());
EXPECT_TRUE
(
Value
(
2147483648u
).
IsLosslessDouble
());
EXPECT_TRUE
(
Value
(
static_cast
<
int64_t
>
(
-
RAPIDJSON_UINT64_C2
(
0x40000000
,
0x00000000
))).
IsLosslessDouble
());
EXPECT_TRUE
(
Value
(
RAPIDJSON_UINT64_C2
(
0xA0000000
,
0x00000000
)).
IsLosslessDouble
());
EXPECT_FALSE
(
Value
(
static_cast
<
int64_t
>
(
-
RAPIDJSON_UINT64_C2
(
0x7FFFFFFF
,
0xFFFFFFFF
))).
IsLosslessDouble
());
EXPECT_FALSE
(
Value
(
RAPIDJSON_UINT64_C2
(
0xFFFFFFFF
,
0xFFFFFFFF
)).
IsLosslessDouble
());
}
TEST
(
Value
,
IsLosslessFloat
)
{
EXPECT_TRUE
(
Value
(
12.25
).
IsLosslessFloat
());
EXPECT_TRUE
(
Value
(
-
123
).
IsLosslessFloat
());
EXPECT_TRUE
(
Value
(
2147483648u
).
IsLosslessFloat
());
EXPECT_TRUE
(
Value
(
3.4028234e38
f
).
IsLosslessFloat
());
EXPECT_TRUE
(
Value
(
-
3.4028234e38
f
).
IsLosslessFloat
());
EXPECT_FALSE
(
Value
(
3.4028235e38
).
IsLosslessFloat
());
EXPECT_FALSE
(
Value
(
0.3
).
IsLosslessFloat
());
}
TEST
(
Value
,
String
)
{
// Construction with const string
Value
x
(
"Hello"
,
5
);
// literal
...
...
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