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
2d575662
Commit
2d575662
authored
Aug 31, 2007
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use pow() instead of repeated multiplication by 10 in round(x, n).
parent
f3f6381a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
15 deletions
+5
-15
Objects/floatobject.c
Objects/floatobject.c
+5
-15
No files found.
Objects/floatobject.c
View file @
2d575662
...
@@ -771,10 +771,9 @@ float_round(PyObject *v, PyObject *args)
...
@@ -771,10 +771,9 @@ float_round(PyObject *v, PyObject *args)
{
{
#define UNDEF_NDIGITS (-0x7fffffff)
/* Unlikely ndigits value */
#define UNDEF_NDIGITS (-0x7fffffff)
/* Unlikely ndigits value */
double
x
;
double
x
;
double
f
;
double
f
=
1
.
0
;
double
flr
,
cil
;
double
flr
,
cil
;
double
rounded
;
double
rounded
;
int
i
;
int
ndigits
=
UNDEF_NDIGITS
;
int
ndigits
=
UNDEF_NDIGITS
;
if
(
!
PyArg_ParseTuple
(
args
,
"|i"
,
&
ndigits
))
if
(
!
PyArg_ParseTuple
(
args
,
"|i"
,
&
ndigits
))
...
@@ -783,14 +782,8 @@ float_round(PyObject *v, PyObject *args)
...
@@ -783,14 +782,8 @@ float_round(PyObject *v, PyObject *args)
x
=
PyFloat_AsDouble
(
v
);
x
=
PyFloat_AsDouble
(
v
);
if
(
ndigits
!=
UNDEF_NDIGITS
)
{
if
(
ndigits
!=
UNDEF_NDIGITS
)
{
f
=
1
.
0
;
f
=
pow
(
10
.
0
,
ndigits
);
i
=
abs
(
ndigits
);
x
*=
f
;
while
(
--
i
>=
0
)
f
=
f
*
10
.
0
;
if
(
ndigits
<
0
)
x
/=
f
;
else
x
*=
f
;
}
}
flr
=
floor
(
x
);
flr
=
floor
(
x
);
...
@@ -798,16 +791,13 @@ float_round(PyObject *v, PyObject *args)
...
@@ -798,16 +791,13 @@ float_round(PyObject *v, PyObject *args)
if
(
x
-
flr
>
0
.
5
)
if
(
x
-
flr
>
0
.
5
)
rounded
=
cil
;
rounded
=
cil
;
else
if
(
x
-
flr
==
0
.
5
)
else
if
(
x
-
flr
==
0
.
5
)
rounded
=
fmod
(
flr
,
2
)
==
0
?
flr
:
cil
;
rounded
=
fmod
(
flr
,
2
)
==
0
?
flr
:
cil
;
else
else
rounded
=
flr
;
rounded
=
flr
;
if
(
ndigits
!=
UNDEF_NDIGITS
)
{
if
(
ndigits
!=
UNDEF_NDIGITS
)
{
if
(
ndigits
<
0
)
rounded
/=
f
;
rounded
*=
f
;
else
rounded
/=
f
;
return
PyFloat_FromDouble
(
rounded
);
return
PyFloat_FromDouble
(
rounded
);
}
}
...
...
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