Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yaml-cpp
Overview
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
yaml-cpp
Commits
d4e00bd4
Commit
d4e00bd4
authored
Jul 16, 2024
by
Simon Gene Gottlieb
Committed by
Jesse Beder
Nov 07, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: rename fp_to_string to FpToString to match coding style
parent
bd070a7b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
188 additions
and
188 deletions
+188
-188
include/yaml-cpp/emitter.h
+1
-1
include/yaml-cpp/fp_to_string.h
+10
-10
include/yaml-cpp/node/convert.h
+1
-1
test/fp_to_string_test.cpp
+176
-176
No files found.
include/yaml-cpp/emitter.h
View file @
d4e00bd4
...
@@ -181,7 +181,7 @@ inline Emitter& Emitter::WriteStreamable(T value) {
...
@@ -181,7 +181,7 @@ inline Emitter& Emitter::WriteStreamable(T value) {
}
}
if
(
!
special
)
{
if
(
!
special
)
{
stream
<<
fp_to_s
tring
(
value
,
stream
.
precision
());
stream
<<
FpToS
tring
(
value
,
stream
.
precision
());
}
}
m_stream
<<
stream
.
str
();
m_stream
<<
stream
.
str
();
...
...
include/yaml-cpp/fp_to_string.h
View file @
d4e00bd4
...
@@ -26,13 +26,13 @@ namespace fp_formatting {
...
@@ -26,13 +26,13 @@ namespace fp_formatting {
*
*
* Example:
* Example:
* std::array<char, 20> buffer;
* std::array<char, 20> buffer;
* auto ct =
c
onvertToChars(buffer.begin(), buffer.end(), 23, 3);
* auto ct =
C
onvertToChars(buffer.begin(), buffer.end(), 23, 3);
* assert(ct = 3);
* assert(ct = 3);
* assert(buffer[0] == '0');
* assert(buffer[0] == '0');
* assert(buffer[1] == '2');
* assert(buffer[1] == '2');
* assert(buffer[2] == '3');
* assert(buffer[2] == '3');
*/
*/
inline
auto
c
onvertToChars
(
char
*
begin
,
char
*
end
,
size_t
value
,
int
width
=
1
)
->
int
{
inline
auto
C
onvertToChars
(
char
*
begin
,
char
*
end
,
size_t
value
,
int
width
=
1
)
->
int
{
assert
(
width
>=
1
);
assert
(
width
>=
1
);
assert
(
end
>=
begin
);
// end must be after begin
assert
(
end
>=
begin
);
// end must be after begin
assert
(
end
-
begin
>=
width
);
// Buffer must be large enough
assert
(
end
-
begin
>=
width
);
// Buffer must be large enough
...
@@ -62,7 +62,7 @@ inline auto convertToChars(char* begin, char* end, size_t value, int width=1) ->
...
@@ -62,7 +62,7 @@ inline auto convertToChars(char* begin, char* end, size_t value, int width=1) ->
* converts a value 'v' to a string. Uses dragonbox for formatting.
* converts a value 'v' to a string. Uses dragonbox for formatting.
*/
*/
template
<
typename
T
>
template
<
typename
T
>
auto
fp_to_s
tring
(
T
v
,
int
precision
=
0
)
->
std
::
string
{
auto
FpToS
tring
(
T
v
,
int
precision
=
0
)
->
std
::
string
{
// assert(precision > 0);
// assert(precision > 0);
// hardcoded constant, at which exponent should switch to a scientific notation
// hardcoded constant, at which exponent should switch to a scientific notation
int
const
lowerExponentThreshold
=
-
5
;
int
const
lowerExponentThreshold
=
-
5
;
...
@@ -81,7 +81,7 @@ auto fp_to_string(T v, int precision = 0) -> std::string {
...
@@ -81,7 +81,7 @@ auto fp_to_string(T v, int precision = 0) -> std::string {
auto
r
=
jkj
::
dragonbox
::
to_decimal
(
v
);
auto
r
=
jkj
::
dragonbox
::
to_decimal
(
v
);
auto
digits
=
std
::
array
<
char
,
20
>
{};
// max digits of size_t is 20.
auto
digits
=
std
::
array
<
char
,
20
>
{};
// max digits of size_t is 20.
auto
digits_ct
=
c
onvertToChars
(
digits
.
data
(),
digits
.
data
()
+
digits
.
size
(),
r
.
significand
);
auto
digits_ct
=
C
onvertToChars
(
digits
.
data
(),
digits
.
data
()
+
digits
.
size
(),
r
.
significand
);
// check if requested precision is lower than
// check if requested precision is lower than
// required digits for exact representation
// required digits for exact representation
...
@@ -136,7 +136,7 @@ auto fp_to_string(T v, int precision = 0) -> std::string {
...
@@ -136,7 +136,7 @@ auto fp_to_string(T v, int precision = 0) -> std::string {
*
(
output_ptr
++
)
=
'e'
;
*
(
output_ptr
++
)
=
'e'
;
*
(
output_ptr
++
)
=
(
exponent
>=
0
)
?
'+'
:
'-'
;
*
(
output_ptr
++
)
=
(
exponent
>=
0
)
?
'+'
:
'-'
;
auto
exp_digits
=
std
::
array
<
char
,
20
>
{};
auto
exp_digits
=
std
::
array
<
char
,
20
>
{};
auto
exp_digits_ct
=
c
onvertToChars
(
exp_digits
.
data
(),
exp_digits
.
data
()
+
exp_digits
.
size
(),
std
::
abs
(
exponent
),
/*.precision=*/
2
);
auto
exp_digits_ct
=
C
onvertToChars
(
exp_digits
.
data
(),
exp_digits
.
data
()
+
exp_digits
.
size
(),
std
::
abs
(
exponent
),
/*.precision=*/
2
);
for
(
int
i
{
0
};
i
<
exp_digits_ct
;
++
i
)
{
for
(
int
i
{
0
};
i
<
exp_digits_ct
;
++
i
)
{
*
(
output_ptr
++
)
=
exp_digits
[
i
];
*
(
output_ptr
++
)
=
exp_digits
[
i
];
}
}
...
@@ -184,18 +184,18 @@ auto fp_to_string(T v, int precision = 0) -> std::string {
...
@@ -184,18 +184,18 @@ auto fp_to_string(T v, int precision = 0) -> std::string {
}
}
}
}
inline
auto
fp_to_s
tring
(
float
v
,
size_t
precision
=
0
)
->
std
::
string
{
inline
auto
FpToS
tring
(
float
v
,
size_t
precision
=
0
)
->
std
::
string
{
return
detail
::
fp_formatting
::
fp_to_s
tring
(
v
,
precision
);
return
detail
::
fp_formatting
::
FpToS
tring
(
v
,
precision
);
}
}
inline
auto
fp_to_s
tring
(
double
v
,
size_t
precision
=
0
)
->
std
::
string
{
inline
auto
FpToS
tring
(
double
v
,
size_t
precision
=
0
)
->
std
::
string
{
return
detail
::
fp_formatting
::
fp_to_s
tring
(
v
,
precision
);
return
detail
::
fp_formatting
::
FpToS
tring
(
v
,
precision
);
}
}
/**
/**
* dragonbox only works for floats/doubles not long double
* dragonbox only works for floats/doubles not long double
*/
*/
inline
auto
fp_to_s
tring
(
long
double
v
,
size_t
precision
=
std
::
numeric_limits
<
long
double
>::
max_digits10
)
->
std
::
string
{
inline
auto
FpToS
tring
(
long
double
v
,
size_t
precision
=
std
::
numeric_limits
<
long
double
>::
max_digits10
)
->
std
::
string
{
std
::
stringstream
ss
;
std
::
stringstream
ss
;
ss
.
precision
(
precision
);
ss
.
precision
(
precision
);
ss
.
imbue
(
std
::
locale
(
"C"
));
ss
.
imbue
(
std
::
locale
(
"C"
));
...
...
include/yaml-cpp/node/convert.h
View file @
d4e00bd4
...
@@ -130,7 +130,7 @@ inner_encode(const T& rhs, std::stringstream& stream){
...
@@ -130,7 +130,7 @@ inner_encode(const T& rhs, std::stringstream& stream){
stream
<<
".inf"
;
stream
<<
".inf"
;
}
}
}
else
{
}
else
{
stream
<<
fp_to_s
tring
(
rhs
,
stream
.
precision
());
stream
<<
FpToS
tring
(
rhs
,
stream
.
precision
());
}
}
}
}
...
...
test/fp_to_string_test.cpp
View file @
d4e00bd4
...
@@ -19,223 +19,223 @@ static std::string convert_with_stringstream(T v, size_t precision = 0) {
...
@@ -19,223 +19,223 @@ static std::string convert_with_stringstream(T v, size_t precision = 0) {
// Caution: Test involving 'convert_with_stringstream' are based on std::stringstream
// Caution: Test involving 'convert_with_stringstream' are based on std::stringstream
// having certain printing behavior, if these changes, the unit test might fail.
// having certain printing behavior, if these changes, the unit test might fail.
// This is not a fault of
fp_to_s
tring just a weakness of the way these
// This is not a fault of
FpToS
tring just a weakness of the way these
// tests are constructed
// tests are constructed
TEST
(
FpToStringTest
,
conversion_double
)
{
TEST
(
FpToStringTest
,
conversion_double
)
{
// Issue motivating
fp_to_s
tring function,
// Issue motivating
FpToS
tring function,
// https://github.com/jbeder/yaml-cpp/issues/1289
// https://github.com/jbeder/yaml-cpp/issues/1289
// Original problem at hand:
// Original problem at hand:
EXPECT_EQ
(
"34.34"
,
fp_to_s
tring
(
34.34
));
EXPECT_EQ
(
"34.34"
,
FpToS
tring
(
34.34
));
EXPECT_EQ
(
"56.56"
,
fp_to_s
tring
(
56.56
));
EXPECT_EQ
(
"56.56"
,
FpToS
tring
(
56.56
));
EXPECT_EQ
(
"12.12"
,
fp_to_s
tring
(
12.12
));
EXPECT_EQ
(
"12.12"
,
FpToS
tring
(
12.12
));
EXPECT_EQ
(
"78.78"
,
fp_to_s
tring
(
78.78
));
EXPECT_EQ
(
"78.78"
,
FpToS
tring
(
78.78
));
// Special challenge with rounding
// Special challenge with rounding
// https://github.com/jbeder/yaml-cpp/issues/1289#issuecomment-2211705536
// https://github.com/jbeder/yaml-cpp/issues/1289#issuecomment-2211705536
EXPECT_EQ
(
"1.54743e+26"
,
fp_to_s
tring
(
1.5474250491e+26
f
));
EXPECT_EQ
(
"1.54743e+26"
,
FpToS
tring
(
1.5474250491e+26
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1.5474250491e+26
f
),
fp_to_s
tring
(
1.5474250491e+26
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1.5474250491e+26
f
),
FpToS
tring
(
1.5474250491e+26
f
));
EXPECT_EQ
(
"1.5474251e+26"
,
fp_to_s
tring
(
1.5474250491e+26
f
,
8
));
EXPECT_EQ
(
"1.5474251e+26"
,
FpToS
tring
(
1.5474250491e+26
f
,
8
));
// prints the same way as std::stringstream
// prints the same way as std::stringstream
EXPECT_EQ
(
convert_with_stringstream
(
1.
),
fp_to_s
tring
(
1.
));
EXPECT_EQ
(
convert_with_stringstream
(
1.
),
FpToS
tring
(
1.
));
EXPECT_EQ
(
convert_with_stringstream
(
1e0
),
fp_to_s
tring
(
1e0
));
EXPECT_EQ
(
convert_with_stringstream
(
1e0
),
FpToS
tring
(
1e0
));
EXPECT_EQ
(
convert_with_stringstream
(
1e1
),
fp_to_s
tring
(
1e1
));
EXPECT_EQ
(
convert_with_stringstream
(
1e1
),
FpToS
tring
(
1e1
));
EXPECT_EQ
(
convert_with_stringstream
(
1e2
),
fp_to_s
tring
(
1e2
));
EXPECT_EQ
(
convert_with_stringstream
(
1e2
),
FpToS
tring
(
1e2
));
EXPECT_EQ
(
convert_with_stringstream
(
1e3
),
fp_to_s
tring
(
1e3
));
EXPECT_EQ
(
convert_with_stringstream
(
1e3
),
FpToS
tring
(
1e3
));
EXPECT_EQ
(
convert_with_stringstream
(
1e4
),
fp_to_s
tring
(
1e4
));
EXPECT_EQ
(
convert_with_stringstream
(
1e4
),
FpToS
tring
(
1e4
));
EXPECT_EQ
(
convert_with_stringstream
(
1e5
),
fp_to_s
tring
(
1e5
));
EXPECT_EQ
(
convert_with_stringstream
(
1e5
),
FpToS
tring
(
1e5
));
EXPECT_EQ
(
convert_with_stringstream
(
1e6
),
fp_to_s
tring
(
1e6
));
EXPECT_EQ
(
convert_with_stringstream
(
1e6
),
FpToS
tring
(
1e6
));
EXPECT_EQ
(
convert_with_stringstream
(
1e7
),
fp_to_s
tring
(
1e7
));
EXPECT_EQ
(
convert_with_stringstream
(
1e7
),
FpToS
tring
(
1e7
));
EXPECT_EQ
(
convert_with_stringstream
(
1e8
),
fp_to_s
tring
(
1e8
));
EXPECT_EQ
(
convert_with_stringstream
(
1e8
),
FpToS
tring
(
1e8
));
EXPECT_EQ
(
convert_with_stringstream
(
1e9
),
fp_to_s
tring
(
1e9
));
EXPECT_EQ
(
convert_with_stringstream
(
1e9
),
FpToS
tring
(
1e9
));
// Print by default values below 1e6 without scientific notation
// Print by default values below 1e6 without scientific notation
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1.
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1.
));
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1e0
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1e0
));
EXPECT_EQ
(
"10"
,
fp_to_s
tring
(
1e1
));
EXPECT_EQ
(
"10"
,
FpToS
tring
(
1e1
));
EXPECT_EQ
(
"100"
,
fp_to_s
tring
(
1e2
));
EXPECT_EQ
(
"100"
,
FpToS
tring
(
1e2
));
EXPECT_EQ
(
"1000"
,
fp_to_s
tring
(
1e3
));
EXPECT_EQ
(
"1000"
,
FpToS
tring
(
1e3
));
EXPECT_EQ
(
"10000"
,
fp_to_s
tring
(
1e4
));
EXPECT_EQ
(
"10000"
,
FpToS
tring
(
1e4
));
EXPECT_EQ
(
"100000"
,
fp_to_s
tring
(
1e5
));
EXPECT_EQ
(
"100000"
,
FpToS
tring
(
1e5
));
EXPECT_EQ
(
"1e+06"
,
fp_to_s
tring
(
1e6
));
EXPECT_EQ
(
"1e+06"
,
FpToS
tring
(
1e6
));
EXPECT_EQ
(
"1e+07"
,
fp_to_s
tring
(
1e7
));
EXPECT_EQ
(
"1e+07"
,
FpToS
tring
(
1e7
));
EXPECT_EQ
(
"1e+08"
,
fp_to_s
tring
(
1e8
));
EXPECT_EQ
(
"1e+08"
,
FpToS
tring
(
1e8
));
EXPECT_EQ
(
"1e+09"
,
fp_to_s
tring
(
1e9
));
EXPECT_EQ
(
"1e+09"
,
FpToS
tring
(
1e9
));
// prints the same way as std::stringstream
// prints the same way as std::stringstream
EXPECT_EQ
(
convert_with_stringstream
(
1.
),
fp_to_s
tring
(
1.
));
EXPECT_EQ
(
convert_with_stringstream
(
1.
),
FpToS
tring
(
1.
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-0
),
fp_to_s
tring
(
1e-0
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-0
),
FpToS
tring
(
1e-0
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-1
),
fp_to_s
tring
(
1e-1
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-1
),
FpToS
tring
(
1e-1
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-2
),
fp_to_s
tring
(
1e-2
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-2
),
FpToS
tring
(
1e-2
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-3
),
fp_to_s
tring
(
1e-3
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-3
),
FpToS
tring
(
1e-3
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-4
),
fp_to_s
tring
(
1e-4
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-4
),
FpToS
tring
(
1e-4
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-5
),
fp_to_s
tring
(
1e-5
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-5
),
FpToS
tring
(
1e-5
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-6
),
fp_to_s
tring
(
1e-6
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-6
),
FpToS
tring
(
1e-6
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-7
),
fp_to_s
tring
(
1e-7
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-7
),
FpToS
tring
(
1e-7
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-8
),
fp_to_s
tring
(
1e-8
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-8
),
FpToS
tring
(
1e-8
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-9
),
fp_to_s
tring
(
1e-9
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-9
),
FpToS
tring
(
1e-9
));
// Print by default values above 1e-5 without scientific notation
// Print by default values above 1e-5 without scientific notation
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1.
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1.
));
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1e-0
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1e-0
));
EXPECT_EQ
(
"0.1"
,
fp_to_s
tring
(
1e-1
));
EXPECT_EQ
(
"0.1"
,
FpToS
tring
(
1e-1
));
EXPECT_EQ
(
"0.01"
,
fp_to_s
tring
(
1e-2
));
EXPECT_EQ
(
"0.01"
,
FpToS
tring
(
1e-2
));
EXPECT_EQ
(
"0.001"
,
fp_to_s
tring
(
1e-3
));
EXPECT_EQ
(
"0.001"
,
FpToS
tring
(
1e-3
));
EXPECT_EQ
(
"0.0001"
,
fp_to_s
tring
(
1e-4
));
EXPECT_EQ
(
"0.0001"
,
FpToS
tring
(
1e-4
));
EXPECT_EQ
(
"1e-05"
,
fp_to_s
tring
(
1e-5
));
EXPECT_EQ
(
"1e-05"
,
FpToS
tring
(
1e-5
));
EXPECT_EQ
(
"1e-06"
,
fp_to_s
tring
(
1e-6
));
EXPECT_EQ
(
"1e-06"
,
FpToS
tring
(
1e-6
));
EXPECT_EQ
(
"1e-07"
,
fp_to_s
tring
(
1e-7
));
EXPECT_EQ
(
"1e-07"
,
FpToS
tring
(
1e-7
));
EXPECT_EQ
(
"1e-08"
,
fp_to_s
tring
(
1e-8
));
EXPECT_EQ
(
"1e-08"
,
FpToS
tring
(
1e-8
));
EXPECT_EQ
(
"1e-09"
,
fp_to_s
tring
(
1e-9
));
EXPECT_EQ
(
"1e-09"
,
FpToS
tring
(
1e-9
));
// changing precision has the same effect as std::stringstream
// changing precision has the same effect as std::stringstream
EXPECT_EQ
(
convert_with_stringstream
(
123.
,
1
),
fp_to_s
tring
(
123.
,
1
));
EXPECT_EQ
(
convert_with_stringstream
(
123.
,
1
),
FpToS
tring
(
123.
,
1
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567.
,
7
),
fp_to_s
tring
(
1234567.
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567.
,
7
),
FpToS
tring
(
1234567.
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
12345.67
,
7
),
fp_to_s
tring
(
12345.67
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
12345.67
,
7
),
FpToS
tring
(
12345.67
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
,
7
),
fp_to_s
tring
(
1234567e-9
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
,
7
),
FpToS
tring
(
1234567e-9
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
,
1
),
fp_to_s
tring
(
1234567e-9
,
1
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
,
1
),
FpToS
tring
(
1234567e-9
,
1
));
// known example that is difficult to round
// known example that is difficult to round
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
0.9999
,
2
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
0.9999
,
2
));
EXPECT_EQ
(
"-1"
,
fp_to_s
tring
(
-
0.9999
,
2
));
EXPECT_EQ
(
"-1"
,
FpToS
tring
(
-
0.9999
,
2
));
// some more random tests
// some more random tests
EXPECT_EQ
(
"1.25"
,
fp_to_s
tring
(
1.25
));
EXPECT_EQ
(
"1.25"
,
FpToS
tring
(
1.25
));
EXPECT_EQ
(
"34.34"
,
fp_to_s
tring
(
34.34
));
EXPECT_EQ
(
"34.34"
,
FpToS
tring
(
34.34
));
EXPECT_EQ
(
"1e+20"
,
fp_to_s
tring
(
1e+20
));
EXPECT_EQ
(
"1e+20"
,
FpToS
tring
(
1e+20
));
EXPECT_EQ
(
"1.1e+20"
,
fp_to_s
tring
(
1.1e+20
));
EXPECT_EQ
(
"1.1e+20"
,
FpToS
tring
(
1.1e+20
));
EXPECT_EQ
(
"1e-20"
,
fp_to_s
tring
(
1e-20
));
EXPECT_EQ
(
"1e-20"
,
FpToS
tring
(
1e-20
));
EXPECT_EQ
(
"1.1e-20"
,
fp_to_s
tring
(
1.1e-20
));
EXPECT_EQ
(
"1.1e-20"
,
FpToS
tring
(
1.1e-20
));
EXPECT_EQ
(
"1e-20"
,
fp_to_s
tring
(
0.1e-19
));
EXPECT_EQ
(
"1e-20"
,
FpToS
tring
(
0.1e-19
));
EXPECT_EQ
(
"1.1e-20"
,
fp_to_s
tring
(
0.11e-19
));
EXPECT_EQ
(
"1.1e-20"
,
FpToS
tring
(
0.11e-19
));
EXPECT_EQ
(
"19"
,
fp_to_s
tring
(
18.9
,
2
));
EXPECT_EQ
(
"19"
,
FpToS
tring
(
18.9
,
2
));
EXPECT_EQ
(
"20"
,
fp_to_s
tring
(
19.9
,
2
));
EXPECT_EQ
(
"20"
,
FpToS
tring
(
19.9
,
2
));
EXPECT_EQ
(
"2e+01"
,
fp_to_s
tring
(
19.9
,
1
));
EXPECT_EQ
(
"2e+01"
,
FpToS
tring
(
19.9
,
1
));
EXPECT_EQ
(
"1.2e-05"
,
fp_to_s
tring
(
1.234e-5
,
2
));
EXPECT_EQ
(
"1.2e-05"
,
FpToS
tring
(
1.234e-5
,
2
));
EXPECT_EQ
(
"1.3e-05"
,
fp_to_s
tring
(
1.299e-5
,
2
));
EXPECT_EQ
(
"1.3e-05"
,
FpToS
tring
(
1.299e-5
,
2
));
EXPECT_EQ
(
"-1"
,
fp_to_s
tring
(
-
1.
));
EXPECT_EQ
(
"-1"
,
FpToS
tring
(
-
1.
));
EXPECT_EQ
(
"-1.25"
,
fp_to_s
tring
(
-
1.25
));
EXPECT_EQ
(
"-1.25"
,
FpToS
tring
(
-
1.25
));
EXPECT_EQ
(
"-34.34"
,
fp_to_s
tring
(
-
34.34
));
EXPECT_EQ
(
"-34.34"
,
FpToS
tring
(
-
34.34
));
EXPECT_EQ
(
"-1e+20"
,
fp_to_s
tring
(
-
1e+20
));
EXPECT_EQ
(
"-1e+20"
,
FpToS
tring
(
-
1e+20
));
EXPECT_EQ
(
"-1.1e+20"
,
fp_to_s
tring
(
-
1.1e+20
));
EXPECT_EQ
(
"-1.1e+20"
,
FpToS
tring
(
-
1.1e+20
));
EXPECT_EQ
(
"-1e-20"
,
fp_to_s
tring
(
-
1e-20
));
EXPECT_EQ
(
"-1e-20"
,
FpToS
tring
(
-
1e-20
));
EXPECT_EQ
(
"-1.1e-20"
,
fp_to_s
tring
(
-
1.1e-20
));
EXPECT_EQ
(
"-1.1e-20"
,
FpToS
tring
(
-
1.1e-20
));
EXPECT_EQ
(
"-1e-20"
,
fp_to_s
tring
(
-
0.1e-19
));
EXPECT_EQ
(
"-1e-20"
,
FpToS
tring
(
-
0.1e-19
));
EXPECT_EQ
(
"-1.1e-20"
,
fp_to_s
tring
(
-
0.11e-19
));
EXPECT_EQ
(
"-1.1e-20"
,
FpToS
tring
(
-
0.11e-19
));
EXPECT_EQ
(
"-19"
,
fp_to_s
tring
(
-
18.9
,
2
));
EXPECT_EQ
(
"-19"
,
FpToS
tring
(
-
18.9
,
2
));
EXPECT_EQ
(
"-20"
,
fp_to_s
tring
(
-
19.9
,
2
));
EXPECT_EQ
(
"-20"
,
FpToS
tring
(
-
19.9
,
2
));
EXPECT_EQ
(
"-2e+01"
,
fp_to_s
tring
(
-
19.9
,
1
));
EXPECT_EQ
(
"-2e+01"
,
FpToS
tring
(
-
19.9
,
1
));
EXPECT_EQ
(
"-1.2e-05"
,
fp_to_s
tring
(
-
1.234e-5
,
2
));
EXPECT_EQ
(
"-1.2e-05"
,
FpToS
tring
(
-
1.234e-5
,
2
));
EXPECT_EQ
(
"-1.3e-05"
,
fp_to_s
tring
(
-
1.299e-5
,
2
));
EXPECT_EQ
(
"-1.3e-05"
,
FpToS
tring
(
-
1.299e-5
,
2
));
}
}
TEST
(
FpToStringTest
,
conversion_float
)
{
TEST
(
FpToStringTest
,
conversion_float
)
{
// Issue motivating
fp_to_s
tring function,
// Issue motivating
FpToS
tring function,
// https://github.com/jbeder/yaml-cpp/issues/1289
// https://github.com/jbeder/yaml-cpp/issues/1289
// Original problem at hand:
// Original problem at hand:
EXPECT_EQ
(
"34.34"
,
fp_to_s
tring
(
34.34
f
));
EXPECT_EQ
(
"34.34"
,
FpToS
tring
(
34.34
f
));
EXPECT_EQ
(
"56.56"
,
fp_to_s
tring
(
56.56
f
));
EXPECT_EQ
(
"56.56"
,
FpToS
tring
(
56.56
f
));
EXPECT_EQ
(
"12.12"
,
fp_to_s
tring
(
12.12
f
));
EXPECT_EQ
(
"12.12"
,
FpToS
tring
(
12.12
f
));
EXPECT_EQ
(
"78.78"
,
fp_to_s
tring
(
78.78
f
));
EXPECT_EQ
(
"78.78"
,
FpToS
tring
(
78.78
f
));
// prints the same way as std::stringstream
// prints the same way as std::stringstream
EXPECT_EQ
(
convert_with_stringstream
(
1.
f
),
fp_to_s
tring
(
1.
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1.
f
),
FpToS
tring
(
1.
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e0
f
),
fp_to_s
tring
(
1e0
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e0
f
),
FpToS
tring
(
1e0
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e1
f
),
fp_to_s
tring
(
1e1
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e1
f
),
FpToS
tring
(
1e1
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e2
f
),
fp_to_s
tring
(
1e2
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e2
f
),
FpToS
tring
(
1e2
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e3
f
),
fp_to_s
tring
(
1e3
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e3
f
),
FpToS
tring
(
1e3
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e4
f
),
fp_to_s
tring
(
1e4
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e4
f
),
FpToS
tring
(
1e4
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e5
f
),
fp_to_s
tring
(
1e5
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e5
f
),
FpToS
tring
(
1e5
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e6
f
),
fp_to_s
tring
(
1e6
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e6
f
),
FpToS
tring
(
1e6
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e7
f
),
fp_to_s
tring
(
1e7
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e7
f
),
FpToS
tring
(
1e7
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e8
f
),
fp_to_s
tring
(
1e8
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e8
f
),
FpToS
tring
(
1e8
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e9
f
),
fp_to_s
tring
(
1e9
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e9
f
),
FpToS
tring
(
1e9
f
));
// Print by default values below 1e6 without scientific notation
// Print by default values below 1e6 without scientific notation
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1.
f
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1.
f
));
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1e0
f
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1e0
f
));
EXPECT_EQ
(
"10"
,
fp_to_s
tring
(
1e1
f
));
EXPECT_EQ
(
"10"
,
FpToS
tring
(
1e1
f
));
EXPECT_EQ
(
"100"
,
fp_to_s
tring
(
1e2
f
));
EXPECT_EQ
(
"100"
,
FpToS
tring
(
1e2
f
));
EXPECT_EQ
(
"1000"
,
fp_to_s
tring
(
1e3
f
));
EXPECT_EQ
(
"1000"
,
FpToS
tring
(
1e3
f
));
EXPECT_EQ
(
"10000"
,
fp_to_s
tring
(
1e4
f
));
EXPECT_EQ
(
"10000"
,
FpToS
tring
(
1e4
f
));
EXPECT_EQ
(
"100000"
,
fp_to_s
tring
(
1e5
f
));
EXPECT_EQ
(
"100000"
,
FpToS
tring
(
1e5
f
));
EXPECT_EQ
(
"1e+06"
,
fp_to_s
tring
(
1e6
f
));
EXPECT_EQ
(
"1e+06"
,
FpToS
tring
(
1e6
f
));
EXPECT_EQ
(
"1e+07"
,
fp_to_s
tring
(
1e7
f
));
EXPECT_EQ
(
"1e+07"
,
FpToS
tring
(
1e7
f
));
EXPECT_EQ
(
"1e+08"
,
fp_to_s
tring
(
1e8
f
));
EXPECT_EQ
(
"1e+08"
,
FpToS
tring
(
1e8
f
));
EXPECT_EQ
(
"1e+09"
,
fp_to_s
tring
(
1e9
f
));
EXPECT_EQ
(
"1e+09"
,
FpToS
tring
(
1e9
f
));
// prints the same way as std::stringstream
// prints the same way as std::stringstream
EXPECT_EQ
(
convert_with_stringstream
(
1.
f
),
fp_to_s
tring
(
1.
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1.
f
),
FpToS
tring
(
1.
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-0
f
),
fp_to_s
tring
(
1e-0
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-0
f
),
FpToS
tring
(
1e-0
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-1
f
),
fp_to_s
tring
(
1e-1
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-1
f
),
FpToS
tring
(
1e-1
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-2
f
),
fp_to_s
tring
(
1e-2
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-2
f
),
FpToS
tring
(
1e-2
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-3
f
),
fp_to_s
tring
(
1e-3
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-3
f
),
FpToS
tring
(
1e-3
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-4
f
),
fp_to_s
tring
(
1e-4
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-4
f
),
FpToS
tring
(
1e-4
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-5
f
),
fp_to_s
tring
(
1e-5
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-5
f
),
FpToS
tring
(
1e-5
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-6
f
),
fp_to_s
tring
(
1e-6
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-6
f
),
FpToS
tring
(
1e-6
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-7
f
),
fp_to_s
tring
(
1e-7
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-7
f
),
FpToS
tring
(
1e-7
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-8
f
),
fp_to_s
tring
(
1e-8
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-8
f
),
FpToS
tring
(
1e-8
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-9
f
),
fp_to_s
tring
(
1e-9
f
));
EXPECT_EQ
(
convert_with_stringstream
(
1e-9
f
),
FpToS
tring
(
1e-9
f
));
// Print by default values above 1e-5 without scientific notation
// Print by default values above 1e-5 without scientific notation
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1.
f
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1.
f
));
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
1e-0
f
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
1e-0
f
));
EXPECT_EQ
(
"0.1"
,
fp_to_s
tring
(
1e-1
f
));
EXPECT_EQ
(
"0.1"
,
FpToS
tring
(
1e-1
f
));
EXPECT_EQ
(
"0.01"
,
fp_to_s
tring
(
1e-2
f
));
EXPECT_EQ
(
"0.01"
,
FpToS
tring
(
1e-2
f
));
EXPECT_EQ
(
"0.001"
,
fp_to_s
tring
(
1e-3
f
));
EXPECT_EQ
(
"0.001"
,
FpToS
tring
(
1e-3
f
));
EXPECT_EQ
(
"0.0001"
,
fp_to_s
tring
(
1e-4
f
));
EXPECT_EQ
(
"0.0001"
,
FpToS
tring
(
1e-4
f
));
EXPECT_EQ
(
"1e-05"
,
fp_to_s
tring
(
1e-5
f
));
EXPECT_EQ
(
"1e-05"
,
FpToS
tring
(
1e-5
f
));
EXPECT_EQ
(
"1e-06"
,
fp_to_s
tring
(
1e-6
f
));
EXPECT_EQ
(
"1e-06"
,
FpToS
tring
(
1e-6
f
));
EXPECT_EQ
(
"1e-07"
,
fp_to_s
tring
(
1e-7
f
));
EXPECT_EQ
(
"1e-07"
,
FpToS
tring
(
1e-7
f
));
EXPECT_EQ
(
"1e-08"
,
fp_to_s
tring
(
1e-8
f
));
EXPECT_EQ
(
"1e-08"
,
FpToS
tring
(
1e-8
f
));
EXPECT_EQ
(
"1e-09"
,
fp_to_s
tring
(
1e-9
f
));
EXPECT_EQ
(
"1e-09"
,
FpToS
tring
(
1e-9
f
));
// changing precision has the same effect as std::stringstream
// changing precision has the same effect as std::stringstream
EXPECT_EQ
(
convert_with_stringstream
(
123.
f
,
1
),
fp_to_s
tring
(
123.
f
,
1
));
EXPECT_EQ
(
convert_with_stringstream
(
123.
f
,
1
),
FpToS
tring
(
123.
f
,
1
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567.
f
,
7
),
fp_to_s
tring
(
1234567.
f
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567.
f
,
7
),
FpToS
tring
(
1234567.
f
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
12345.67
f
,
7
),
fp_to_s
tring
(
12345.67
f
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
12345.67
f
,
7
),
FpToS
tring
(
12345.67
f
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
f
,
7
),
fp_to_s
tring
(
1234567e-9
f
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
f
,
7
),
FpToS
tring
(
1234567e-9
f
,
7
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
f
,
1
),
fp_to_s
tring
(
1234567e-9
f
,
1
));
EXPECT_EQ
(
convert_with_stringstream
(
1234567e-9
f
,
1
),
FpToS
tring
(
1234567e-9
f
,
1
));
// known example that is difficult to round
// known example that is difficult to round
EXPECT_EQ
(
"1"
,
fp_to_s
tring
(
0.9999
f
,
2
));
EXPECT_EQ
(
"1"
,
FpToS
tring
(
0.9999
f
,
2
));
EXPECT_EQ
(
"-1"
,
fp_to_s
tring
(
-
0.9999
f
,
2
));
EXPECT_EQ
(
"-1"
,
FpToS
tring
(
-
0.9999
f
,
2
));
// some more random tests
// some more random tests
EXPECT_EQ
(
"1.25"
,
fp_to_s
tring
(
1.25
f
));
EXPECT_EQ
(
"1.25"
,
FpToS
tring
(
1.25
f
));
EXPECT_EQ
(
"34.34"
,
fp_to_s
tring
(
34.34
f
));
EXPECT_EQ
(
"34.34"
,
FpToS
tring
(
34.34
f
));
EXPECT_EQ
(
"1e+20"
,
fp_to_s
tring
(
1e+20
f
));
EXPECT_EQ
(
"1e+20"
,
FpToS
tring
(
1e+20
f
));
EXPECT_EQ
(
"1.1e+20"
,
fp_to_s
tring
(
1.1e+20
f
));
EXPECT_EQ
(
"1.1e+20"
,
FpToS
tring
(
1.1e+20
f
));
EXPECT_EQ
(
"1e-20"
,
fp_to_s
tring
(
1e-20
f
));
EXPECT_EQ
(
"1e-20"
,
FpToS
tring
(
1e-20
f
));
EXPECT_EQ
(
"1.1e-20"
,
fp_to_s
tring
(
1.1e-20
f
));
EXPECT_EQ
(
"1.1e-20"
,
FpToS
tring
(
1.1e-20
f
));
EXPECT_EQ
(
"1e-20"
,
fp_to_s
tring
(
0.1e-19
f
));
EXPECT_EQ
(
"1e-20"
,
FpToS
tring
(
0.1e-19
f
));
EXPECT_EQ
(
"1.1e-20"
,
fp_to_s
tring
(
0.11e-19
f
));
EXPECT_EQ
(
"1.1e-20"
,
FpToS
tring
(
0.11e-19
f
));
EXPECT_EQ
(
"19"
,
fp_to_s
tring
(
18.9
f
,
2
));
EXPECT_EQ
(
"19"
,
FpToS
tring
(
18.9
f
,
2
));
EXPECT_EQ
(
"20"
,
fp_to_s
tring
(
19.9
f
,
2
));
EXPECT_EQ
(
"20"
,
FpToS
tring
(
19.9
f
,
2
));
EXPECT_EQ
(
"2e+01"
,
fp_to_s
tring
(
19.9
f
,
1
));
EXPECT_EQ
(
"2e+01"
,
FpToS
tring
(
19.9
f
,
1
));
EXPECT_EQ
(
"1.2e-05"
,
fp_to_s
tring
(
1.234e-5
f
,
2
));
EXPECT_EQ
(
"1.2e-05"
,
FpToS
tring
(
1.234e-5
f
,
2
));
EXPECT_EQ
(
"1.3e-05"
,
fp_to_s
tring
(
1.299e-5
f
,
2
));
EXPECT_EQ
(
"1.3e-05"
,
FpToS
tring
(
1.299e-5
f
,
2
));
EXPECT_EQ
(
"-1"
,
fp_to_s
tring
(
-
1.
f
));
EXPECT_EQ
(
"-1"
,
FpToS
tring
(
-
1.
f
));
EXPECT_EQ
(
"-1.25"
,
fp_to_s
tring
(
-
1.25
f
));
EXPECT_EQ
(
"-1.25"
,
FpToS
tring
(
-
1.25
f
));
EXPECT_EQ
(
"-34.34"
,
fp_to_s
tring
(
-
34.34
f
));
EXPECT_EQ
(
"-34.34"
,
FpToS
tring
(
-
34.34
f
));
EXPECT_EQ
(
"-1e+20"
,
fp_to_s
tring
(
-
1e+20
f
));
EXPECT_EQ
(
"-1e+20"
,
FpToS
tring
(
-
1e+20
f
));
EXPECT_EQ
(
"-1.1e+20"
,
fp_to_s
tring
(
-
1.1e+20
f
));
EXPECT_EQ
(
"-1.1e+20"
,
FpToS
tring
(
-
1.1e+20
f
));
EXPECT_EQ
(
"-1e-20"
,
fp_to_s
tring
(
-
1e-20
f
));
EXPECT_EQ
(
"-1e-20"
,
FpToS
tring
(
-
1e-20
f
));
EXPECT_EQ
(
"-1.1e-20"
,
fp_to_s
tring
(
-
1.1e-20
f
));
EXPECT_EQ
(
"-1.1e-20"
,
FpToS
tring
(
-
1.1e-20
f
));
EXPECT_EQ
(
"-1e-20"
,
fp_to_s
tring
(
-
0.1e-19
f
));
EXPECT_EQ
(
"-1e-20"
,
FpToS
tring
(
-
0.1e-19
f
));
EXPECT_EQ
(
"-1.1e-20"
,
fp_to_s
tring
(
-
0.11e-19
f
));
EXPECT_EQ
(
"-1.1e-20"
,
FpToS
tring
(
-
0.11e-19
f
));
EXPECT_EQ
(
"-19"
,
fp_to_s
tring
(
-
18.9
f
,
2
));
EXPECT_EQ
(
"-19"
,
FpToS
tring
(
-
18.9
f
,
2
));
EXPECT_EQ
(
"-20"
,
fp_to_s
tring
(
-
19.9
f
,
2
));
EXPECT_EQ
(
"-20"
,
FpToS
tring
(
-
19.9
f
,
2
));
EXPECT_EQ
(
"-2e+01"
,
fp_to_s
tring
(
-
19.9
f
,
1
));
EXPECT_EQ
(
"-2e+01"
,
FpToS
tring
(
-
19.9
f
,
1
));
EXPECT_EQ
(
"-1.2e-05"
,
fp_to_s
tring
(
-
1.234e-5
f
,
2
));
EXPECT_EQ
(
"-1.2e-05"
,
FpToS
tring
(
-
1.234e-5
f
,
2
));
EXPECT_EQ
(
"-1.3e-05"
,
fp_to_s
tring
(
-
1.299e-5
f
,
2
));
EXPECT_EQ
(
"-1.3e-05"
,
FpToS
tring
(
-
1.299e-5
f
,
2
));
}
}
}
// namespace
}
// namespace
...
...
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