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
ce604838
Commit
ce604838
authored
Dec 28, 2012
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Format NaN.
parent
5d4ef338
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
8 deletions
+40
-8
format.cc
format.cc
+27
-3
format.h
format.h
+1
-1
format_test.cc
format_test.cc
+12
-4
No files found.
format.cc
View file @
ce604838
...
...
@@ -33,6 +33,7 @@
#include "format.h"
#include <math.h>
#include <stdint.h>
#include <cassert>
...
...
@@ -280,17 +281,21 @@ template <typename T>
void
Formatter
::
FormatDouble
(
T
value
,
const
FormatSpec
&
spec
,
int
precision
)
{
// Check type.
char
type
=
spec
.
type
;
bool
upper
=
false
;
switch
(
type
)
{
case
0
:
type
=
'g'
;
break
;
case
'e'
:
case
'
E'
:
case
'f'
:
case
'g'
:
case
'G
'
:
case
'e'
:
case
'
f'
:
case
'g
'
:
break
;
case
'F'
:
#ifdef _MSC_VER
// MSVC's printf doesn't support 'F'.
type
=
'f'
;
#endif
// Fall through.
case
'E'
:
case
'G'
:
upper
=
true
;
break
;
default:
ReportUnknownType
(
type
,
"double"
);
...
...
@@ -298,12 +303,30 @@ void Formatter::FormatDouble(T value, const FormatSpec &spec, int precision) {
}
char
sign
=
0
;
if
(
value
<
0
)
{
// Use signbit instead of value < 0 because the latter is always
// false for NaN.
if
(
signbit
(
value
))
{
sign
=
'-'
;
value
=
-
value
;
}
else
if
((
spec
.
flags
&
SIGN_FLAG
)
!=
0
)
{
sign
=
(
spec
.
flags
&
PLUS_FLAG
)
!=
0
?
'+'
:
' '
;
}
if
(
isnan
(
value
))
{
// Format NaN ourselves because sprintf's output is not consistent
// across platforms.
std
::
size_t
size
=
4
;
const
char
*
nan
=
upper
?
" NAN"
:
" nan"
;
if
(
!
sign
)
{
--
size
;
++
nan
;
}
char
*
out
=
FormatString
(
nan
,
size
,
spec
);
if
(
sign
)
*
out
=
sign
;
return
;
}
size_t
offset
=
buffer_
.
size
();
unsigned
width
=
spec
.
width
;
if
(
sign
)
{
...
...
@@ -382,7 +405,7 @@ void Formatter::FormatDouble(T value, const FormatSpec &spec, int precision) {
}
}
void
Formatter
::
FormatString
(
char
*
Formatter
::
FormatString
(
const
char
*
s
,
std
::
size_t
size
,
const
FormatSpec
&
spec
)
{
char
*
out
=
0
;
if
(
spec
.
width
>
size
)
{
...
...
@@ -399,6 +422,7 @@ void Formatter::FormatString(
out
=
GrowBuffer
(
size
);
}
std
::
copy
(
s
,
s
+
size
,
out
);
return
out
;
}
// Parses an unsigned integer advancing s to the end of the parsed input.
...
...
format.h
View file @
ce604838
...
...
@@ -322,7 +322,7 @@ class Formatter : public BasicFormatter {
template
<
typename
T
>
void
FormatDouble
(
T
value
,
const
FormatSpec
&
spec
,
int
precision
);
void
FormatString
(
const
char
*
s
,
std
::
size_t
size
,
const
FormatSpec
&
spec
);
char
*
FormatString
(
const
char
*
s
,
std
::
size_t
size
,
const
FormatSpec
&
spec
);
// Formats an argument of a custom type, such as a user-defined class.
template
<
typename
T
>
...
...
format_test.cc
View file @
ce604838
...
...
@@ -774,16 +774,24 @@ 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
nan
=
std
::
numeric_limits
<
double
>::
quiet_NaN
();
EXPECT_EQ
(
"nan"
,
str
(
Format
(
"{}"
)
<<
nan
));
EXPECT_EQ
(
"-nan"
,
str
(
Format
(
"{}"
)
<<
-
nan
));
EXPECT_EQ
(
"NAN"
,
str
(
Format
(
"{:F}"
)
<<
nan
));
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
)
{
double
nan
=
std
::
numeric_limits
<
double
>::
quiet_NaN
();
EXPECT_EQ
(
"nan"
,
str
(
Format
(
"{}"
)
<<
nan
));
EXPECT_EQ
(
"+nan"
,
str
(
Format
(
"{:+}"
)
<<
nan
));
EXPECT_EQ
(
"-nan"
,
str
(
Format
(
"{}"
)
<<
-
nan
));
EXPECT_EQ
(
" nan"
,
str
(
Format
(
"{: }"
)
<<
nan
));
EXPECT_EQ
(
"NAN"
,
str
(
Format
(
"{:F}"
)
<<
nan
));
EXPECT_EQ
(
"nan "
,
str
(
Format
(
"{:<7}"
)
<<
nan
));
EXPECT_EQ
(
" nan "
,
str
(
Format
(
"{:^7}"
)
<<
nan
));
EXPECT_EQ
(
" nan"
,
str
(
Format
(
"{:>7}"
)
<<
nan
));
}
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