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
7321ec43
Commit
7321ec43
authored
Jul 26, 2001
by
Tim Peters
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SF bug #444510: int() should guarantee truncation.
It's guaranteed now, assuming the platform modf() works correctly.
parent
7cf92fa1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
10 deletions
+27
-10
Doc/lib/libfuncs.tex
Doc/lib/libfuncs.tex
+1
-3
Lib/test/test_b1.py
Lib/test/test_b1.py
+13
-0
Objects/floatobject.c
Objects/floatobject.c
+13
-7
No files found.
Doc/lib/libfuncs.tex
View file @
7321ec43
...
...
@@ -338,9 +338,7 @@ module from which it is called).
\exception
{
TypeError
}
is raised.
Otherwise, the argument may be a plain or
long integer or a floating point number. Conversion of floating
point numbers to integers is defined by the C semantics; normally
the conversion truncates towards zero.
\footnote
{
This is ugly --- the
language definition should require truncation towards zero.
}
point numbers to integers truncates (towards zero).
\end{funcdesc}
\begin{funcdesc}
{
intern
}{
string
}
...
...
Lib/test/test_b1.py
View file @
7321ec43
...
...
@@ -366,6 +366,19 @@ except ValueError:
pass
else
:
raise
TestFailed
,
"int(%s)"
%
`s[1:]`
+
" should raise ValueError"
try
:
int
(
1e100
)
except
OverflowError
:
pass
else
:
raise
TestFailed
(
"int(1e100) expected OverflowError"
)
try
:
int
(
-
1e100
)
except
OverflowError
:
pass
else
:
raise
TestFailed
(
"int(-1e100) expected OverflowError"
)
# SF bug 434186: 0x80000000/2 != 0x80000000>>1.
# Worked by accident in Windows release build, but failed in debug build.
...
...
Objects/floatobject.c
View file @
7321ec43
...
...
@@ -606,13 +606,19 @@ static PyObject *
float_int
(
PyObject
*
v
)
{
double
x
=
PyFloat_AsDouble
(
v
);
if
(
x
<
0
?
(
x
=
ceil
(
x
))
<
(
double
)
LONG_MIN
:
(
x
=
floor
(
x
))
>
(
double
)
LONG_MAX
)
{
PyErr_SetString
(
PyExc_OverflowError
,
"float too large to convert"
);
return
NULL
;
}
return
PyInt_FromLong
((
long
)
x
);
double
wholepart
;
/* integral portion of x, rounded toward 0 */
long
aslong
;
/* (long)wholepart */
(
void
)
modf
(
x
,
&
wholepart
);
/* doubles may have more bits than longs, or vice versa; and casting
to long may yield gibberish in either case. What really matters
is whether converting back to double again reproduces what we
started with. */
aslong
=
(
long
)
wholepart
;
if
((
double
)
aslong
==
wholepart
)
return
PyInt_FromLong
(
aslong
);
PyErr_SetString
(
PyExc_OverflowError
,
"float too large to convert"
);
return
NULL
;
}
static
PyObject
*
...
...
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