Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
66d7746b
Commit
66d7746b
authored
Nov 19, 2019
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use grisu for exponent notation
parent
e9bff788
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
23 deletions
+28
-23
include/fmt/format.h
include/fmt/format.h
+27
-23
test/format-test.cc
test/format-test.cc
+1
-0
No files found.
include/fmt/format.h
View file @
66d7746b
...
@@ -1123,6 +1123,9 @@ template <typename Char> class grisu_writer {
...
@@ -1123,6 +1123,9 @@ template <typename Char> class grisu_writer {
*
it
++
=
static_cast
<
Char
>
(
*
digits_
);
*
it
++
=
static_cast
<
Char
>
(
*
digits_
);
if
(
num_digits_
>
1
)
*
it
++
=
decimal_point_
;
if
(
num_digits_
>
1
)
*
it
++
=
decimal_point_
;
it
=
copy_str
<
Char
>
(
digits_
+
1
,
digits_
+
num_digits_
,
it
);
it
=
copy_str
<
Char
>
(
digits_
+
1
,
digits_
+
num_digits_
,
it
);
int
num_zeros
=
params_
.
num_digits
-
num_digits_
;
if
(
num_zeros
>
0
&&
params_
.
trailing_zeros
)
it
=
std
::
fill_n
(
it
,
num_zeros
,
static_cast
<
Char
>
(
'0'
));
*
it
++
=
static_cast
<
Char
>
(
params_
.
upper
?
'E'
:
'e'
);
*
it
++
=
static_cast
<
Char
>
(
params_
.
upper
?
'E'
:
'e'
);
return
write_exponent
<
Char
>
(
full_exp
-
1
,
it
);
return
write_exponent
<
Char
>
(
full_exp
-
1
,
it
);
}
}
...
@@ -2808,8 +2811,7 @@ template <typename T, bool USE_GRISU>
...
@@ -2808,8 +2811,7 @@ template <typename T, bool USE_GRISU>
void
internal
::
basic_writer
<
Range
>::
write_fp
(
T
value
,
void
internal
::
basic_writer
<
Range
>::
write_fp
(
T
value
,
const
format_specs
&
specs
)
{
const
format_specs
&
specs
)
{
auto
sign
=
specs
.
sign
;
auto
sign
=
specs
.
sign
;
// Use signbit instead of value < 0 since the latter is always false for NaN.
if
(
std
::
signbit
(
value
))
{
// value < 0 is false for NaN so use signbit.
if
(
std
::
signbit
(
value
))
{
sign
=
sign
::
minus
;
sign
=
sign
::
minus
;
value
=
-
value
;
value
=
-
value
;
}
else
if
(
sign
==
sign
::
minus
)
{
}
else
if
(
sign
==
sign
::
minus
)
{
...
@@ -2828,15 +2830,15 @@ void internal::basic_writer<Range>::write_fp(T value,
...
@@ -2828,15 +2830,15 @@ void internal::basic_writer<Range>::write_fp(T value,
memory_buffer
buffer
;
memory_buffer
buffer
;
int
exp
=
0
;
int
exp
=
0
;
int
precision
=
specs
.
precision
>=
0
||
!
specs
.
type
?
specs
.
precision
:
6
;
int
precision
=
specs
.
precision
>=
0
||
!
specs
.
type
?
specs
.
precision
:
6
;
int
num_digits
=
fspec
.
format
==
float_format
::
exp
?
precision
+
1
:
precision
;
unsigned
options
=
0
;
unsigned
options
=
0
;
if
(
fspec
.
format
==
float_format
::
fixed
)
options
|=
grisu_options
::
fixed
;
if
(
fspec
.
format
==
float_format
::
fixed
)
options
|=
grisu_options
::
fixed
;
if
(
const_check
(
sizeof
(
value
)
==
sizeof
(
float
)))
if
(
const_check
(
sizeof
(
value
)
==
sizeof
(
float
)))
options
|=
grisu_options
::
binary32
;
options
|=
grisu_options
::
binary32
;
bool
use_grisu
=
bool
use_grisu
=
USE_GRISU
&&
(
specs
.
type
!=
'a'
&&
specs
.
type
!=
'A'
)
&&
USE_GRISU
&&
grisu_format
(
static_cast
<
double
>
(
value
),
buffer
,
num_digits
,
(
specs
.
type
!=
'a'
&&
specs
.
type
!=
'A'
&&
specs
.
type
!=
'e'
&&
options
,
exp
);
specs
.
type
!=
'E'
)
&&
grisu_format
(
static_cast
<
double
>
(
value
),
buffer
,
precision
,
options
,
exp
);
char
*
decimal_point_pos
=
nullptr
;
char
*
decimal_point_pos
=
nullptr
;
if
(
!
use_grisu
)
decimal_point_pos
=
sprintf_format
(
value
,
buffer
,
specs
);
if
(
!
use_grisu
)
decimal_point_pos
=
sprintf_format
(
value
,
buffer
,
specs
);
...
@@ -2863,11 +2865,13 @@ void internal::basic_writer<Range>::write_fp(T value,
...
@@ -2863,11 +2865,13 @@ void internal::basic_writer<Range>::write_fp(T value,
auto
params
=
gen_digits_params
();
auto
params
=
gen_digits_params
();
params
.
sign
=
sign
;
params
.
sign
=
sign
;
params
.
format
=
fspec
.
format
;
params
.
format
=
fspec
.
format
;
params
.
num_digits
=
precision
;
params
.
num_digits
=
num_digits
;
params
.
trailing_zeros
=
params
.
trailing_zeros
=
(
precision
!=
0
&&
(
precision
!=
0
&&
(
fspec
.
format
==
float_format
::
fixed
||
!
specs
.
type
))
||
(
!
specs
.
type
||
fspec
.
format
==
float_format
::
fixed
||
fspec
.
format
==
float_format
::
exp
))
||
specs
.
alt
;
specs
.
alt
;
params
.
upper
=
fspec
.
upper
;
int
num_digits
=
static_cast
<
int
>
(
buffer
.
size
());
int
num_digits
=
static_cast
<
int
>
(
buffer
.
size
());
write_padded
(
as
,
grisu_writer
<
char_type
>
(
buffer
.
data
(),
num_digits
,
exp
,
write_padded
(
as
,
grisu_writer
<
char_type
>
(
buffer
.
data
(),
num_digits
,
exp
,
params
,
decimal_point
));
params
,
decimal_point
));
...
@@ -3602,20 +3606,20 @@ FMT_CONSTEXPR internal::udl_arg<wchar_t> operator"" _a(const wchar_t* s,
...
@@ -3602,20 +3606,20 @@ FMT_CONSTEXPR internal::udl_arg<wchar_t> operator"" _a(const wchar_t* s,
#endif // FMT_USE_USER_DEFINED_LITERALS
#endif // FMT_USE_USER_DEFINED_LITERALS
FMT_END_NAMESPACE
FMT_END_NAMESPACE
#define FMT_STRING_IMPL(s, ...)
\
#define FMT_STRING_IMPL(s, ...) \
[] {
\
[] { \
struct str : fmt::compile_string {
\
struct str : fmt::compile_string { \
using char_type = typename std::remove_cv<std::remove_pointer<
\
using char_type = typename std::remove_cv<std::remove_pointer< \
typename std::decay<decltype(s)>::type>::type>::type;
\
typename std::decay<decltype(s)>::type>::type>::type; \
__VA_ARGS__ FMT_CONSTEXPR \
__VA_ARGS__ FMT_CONSTEXPR
\
operator fmt::basic_string_view<char_type>() const {
\
operator fmt::basic_string_view<char_type>() const {
\
return {s, sizeof(s) / sizeof(char_type) - 1};
\
return {s, sizeof(s) / sizeof(char_type) - 1}; \
}
\
} \
} result;
\
} result; \
/* Suppress Qt Creator warning about unused operator. */
\
/* Suppress Qt Creator warning about unused operator. */
\
(void)static_cast<fmt::basic_string_view<typename str::char_type>>(
\
(void)static_cast<fmt::basic_string_view<typename str::char_type>>( \
result);
\
result); \
return result;
\
return result; \
}()
}()
/**
/**
...
...
test/format-test.cc
View file @
66d7746b
...
@@ -1206,6 +1206,7 @@ TEST(FormatterTest, Precision) {
...
@@ -1206,6 +1206,7 @@ TEST(FormatterTest, Precision) {
"precision not allowed for this argument type"
);
"precision not allowed for this argument type"
);
EXPECT_EQ
(
"1.2"
,
format
(
"{0:.2}"
,
1.2345
));
EXPECT_EQ
(
"1.2"
,
format
(
"{0:.2}"
,
1.2345
));
EXPECT_EQ
(
"1.2"
,
format
(
"{0:.2}"
,
1.2345
l
));
EXPECT_EQ
(
"1.2"
,
format
(
"{0:.2}"
,
1.2345
l
));
EXPECT_EQ
(
"1.2e+56"
,
format
(
"{:.2}"
,
1.234e56
));
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
reinterpret_cast
<
void
*>
(
0xcafe
)),
EXPECT_THROW_MSG
(
format
(
"{0:.2}"
,
reinterpret_cast
<
void
*>
(
0xcafe
)),
format_error
,
format_error
,
...
...
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