Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
57072eb7
Commit
57072eb7
authored
Dec 23, 1999
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement the other easy thing: repr() of a float now uses %.17g,
while str() uses %.12g as before.
parent
9093834a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
6 deletions
+43
-6
Objects/floatobject.c
Objects/floatobject.c
+43
-6
No files found.
Objects/floatobject.c
View file @
57072eb7
...
...
@@ -243,9 +243,10 @@ PyFloat_AsDouble(op)
/* Methods */
void
PyFloat_AsString
(
buf
,
v
)
PyFloat_AsString
Ex
(
buf
,
v
,
precision
)
char
*
buf
;
PyFloatObject
*
v
;
int
precision
;
{
register
char
*
cp
;
/* Subroutine for float_repr and float_print.
...
...
@@ -253,7 +254,7 @@ PyFloat_AsString(buf, v)
i.e., they should contain a decimal point or an exponent.
However, %g may print the number as an integer;
in such cases, we append ".0" to the string. */
sprintf
(
buf
,
"%.
12g"
,
v
->
ob_fval
);
sprintf
(
buf
,
"%.
*g"
,
precision
,
v
->
ob_fval
);
cp
=
buf
;
if
(
*
cp
==
'-'
)
cp
++
;
...
...
@@ -270,6 +271,31 @@ PyFloat_AsString(buf, v)
}
}
/* Precisions used by repr() and str(), respectively.
The repr() precision (17 significant decimal digits) is the minimal number
that is guaranteed to have enough precision so that if the number is read
back in the exact same binary value is recreated. This is true for IEEE
floating point by design, and also happens to work for all other modern
hardware.
The str() precision is chosen so that in most cases, the rounding noise
created by various operations is suppressed, while giving plenty of
precision for practical use.
*/
#define PREC_REPR 17
#define PREC_STR 12
void
PyFloat_AsString
(
buf
,
v
)
char
*
buf
;
PyFloatObject
*
v
;
{
PyFloat_AsStringEx
(
buf
,
v
,
PREC_STR
);
}
/* ARGSUSED */
static
int
float_print
(
v
,
fp
,
flags
)
...
...
@@ -278,7 +304,7 @@ float_print(v, fp, flags)
int
flags
;
/* Not used but required by interface */
{
char
buf
[
100
];
PyFloat_AsString
(
buf
,
v
);
PyFloat_AsString
Ex
(
buf
,
v
,
flags
&
Py_PRINT_RAW
?
PREC_STR
:
PREC_REPR
);
fputs
(
buf
,
fp
);
return
0
;
}
...
...
@@ -288,7 +314,16 @@ float_repr(v)
PyFloatObject
*
v
;
{
char
buf
[
100
];
PyFloat_AsString
(
buf
,
v
);
PyFloat_AsStringEx
(
buf
,
v
,
PREC_REPR
);
return
PyString_FromString
(
buf
);
}
static
PyObject
*
float_str
(
v
)
PyFloatObject
*
v
;
{
char
buf
[
100
];
PyFloat_AsStringEx
(
buf
,
v
,
PREC_STR
);
return
PyString_FromString
(
buf
);
}
...
...
@@ -673,11 +708,13 @@ PyTypeObject PyFloat_Type = {
0
,
/*tp_getattr*/
0
,
/*tp_setattr*/
(
cmpfunc
)
float_compare
,
/*tp_compare*/
(
reprfunc
)
float_repr
,
/*tp_repr*/
(
reprfunc
)
float_repr
,
/*tp_repr*/
&
float_as_number
,
/*tp_as_number*/
0
,
/*tp_as_sequence*/
0
,
/*tp_as_mapping*/
(
hashfunc
)
float_hash
,
/*tp_hash*/
(
hashfunc
)
float_hash
,
/*tp_hash*/
0
,
/*tp_call*/
(
reprfunc
)
float_str
,
/*tp_str*/
};
void
...
...
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