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
4762a8af
Commit
4762a8af
authored
Dec 29, 2012
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Format infinity.
parent
529045b6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
6 deletions
+30
-6
README.rst
README.rst
+2
-1
format.cc
format.cc
+16
-1
format_test.cc
format_test.cc
+12
-4
No files found.
README.rst
View file @
4762a8af
...
...
@@ -25,7 +25,8 @@ Features
reported using exceptions.
* Ease of use: small self-contained code base, no external dependencies,
permissive BSD `license`_.
* `Portability`_ and support for older compilers.
* `Portability`_ with consistent output across platforms and support
for older compilers.
Examples
--------
...
...
format.cc
View file @
4762a8af
...
...
@@ -141,7 +141,7 @@ int signbit(double value) {
if
(
value
<
0
)
return
1
;
if
(
value
==
value
)
return
0
;
int
dec
=
0
,
sign
=
0
;
ecvt
(
value
,
0
,
&
dec
,
&
sign
);
_
ecvt
(
value
,
0
,
&
dec
,
&
sign
);
return
sign
;
}
#endif
...
...
@@ -337,6 +337,21 @@ void Formatter::FormatDouble(T value, const FormatSpec &spec, int precision) {
return
;
}
if
(
isinf
(
value
))
{
// Format infinity ourselves because sprintf's output is not consistent
// across platforms.
std
::
size_t
size
=
4
;
const
char
*
inf
=
upper
?
" INF"
:
" inf"
;
if
(
!
sign
)
{
--
size
;
++
inf
;
}
char
*
out
=
FormatString
(
inf
,
size
,
spec
);
if
(
sign
)
*
out
=
sign
;
return
;
}
size_t
offset
=
buffer_
.
size
();
unsigned
width
=
spec
.
width
;
if
(
sign
)
{
...
...
format_test.cc
View file @
4762a8af
...
...
@@ -774,10 +774,6 @@ TEST(FormatterTest, FormatDouble) {
sprintf
(
buffer
,
"%E"
,
392.65
);
EXPECT_EQ
(
buffer
,
str
(
Format
(
"{0:E}"
)
<<
392.65
));
EXPECT_EQ
(
"+0000392.6"
,
str
(
Format
(
"{0:+010.4g}"
)
<<
392.65
));
double
inf
=
std
::
numeric_limits
<
double
>::
infinity
();
EXPECT_EQ
(
"inf"
,
str
(
Format
(
"{}"
)
<<
inf
));
EXPECT_EQ
(
"-inf"
,
str
(
Format
(
"{}"
)
<<
-
inf
));
EXPECT_EQ
(
"INF"
,
str
(
Format
(
"{:F}"
)
<<
inf
));
}
TEST
(
FormatterTest
,
FormatNaN
)
{
...
...
@@ -792,6 +788,18 @@ TEST(FormatterTest, FormatNaN) {
EXPECT_EQ
(
" nan"
,
str
(
Format
(
"{:>7}"
)
<<
nan
));
}
TEST
(
FormatterTest
,
FormatInfinity
)
{
double
inf
=
std
::
numeric_limits
<
double
>::
infinity
();
EXPECT_EQ
(
"inf"
,
str
(
Format
(
"{}"
)
<<
inf
));
EXPECT_EQ
(
"+inf"
,
str
(
Format
(
"{:+}"
)
<<
inf
));
EXPECT_EQ
(
"-inf"
,
str
(
Format
(
"{}"
)
<<
-
inf
));
EXPECT_EQ
(
" inf"
,
str
(
Format
(
"{: }"
)
<<
inf
));
EXPECT_EQ
(
"INF"
,
str
(
Format
(
"{:F}"
)
<<
inf
));
EXPECT_EQ
(
"inf "
,
str
(
Format
(
"{:<7}"
)
<<
inf
));
EXPECT_EQ
(
" inf "
,
str
(
Format
(
"{:^7}"
)
<<
inf
));
EXPECT_EQ
(
" inf"
,
str
(
Format
(
"{:>7}"
)
<<
inf
));
}
TEST
(
FormatterTest
,
FormatLongDouble
)
{
EXPECT_EQ
(
"0"
,
str
(
Format
(
"{0:}"
)
<<
0.0
l
));
EXPECT_EQ
(
"0.000000"
,
str
(
Format
(
"{0:f}"
)
<<
0.0
l
));
...
...
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