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
088cec3a
Commit
088cec3a
authored
Aug 24, 2012
by
Mark Dickinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
parent
85ede8da
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
1 deletion
+23
-1
Lib/decimal.py
Lib/decimal.py
+7
-1
Lib/test/test_decimal.py
Lib/test/test_decimal.py
+14
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/decimal.py
View file @
088cec3a
...
@@ -1581,7 +1581,13 @@ class Decimal(object):
...
@@ -1581,7 +1581,13 @@ class Decimal(object):
def
__float__
(
self
):
def
__float__
(
self
):
"""Float representation."""
"""Float representation."""
return
float
(
str
(
self
))
if
self
.
_isnan
():
if
self
.
is_snan
():
raise
ValueError
(
"Cannot convert signaling NaN to float"
)
s
=
"-nan"
if
self
.
_sign
else
"nan"
else
:
s
=
str
(
self
)
return
float
(
s
)
def
__int__
(
self
):
def
__int__
(
self
):
"""Converts self to an int, truncating if necessary."""
"""Converts self to an int, truncating if necessary."""
...
...
Lib/test/test_decimal.py
View file @
088cec3a
...
@@ -1448,6 +1448,20 @@ class DecimalUsabilityTest(unittest.TestCase):
...
@@ -1448,6 +1448,20 @@ class DecimalUsabilityTest(unittest.TestCase):
self
.
assertEqual
(
float
(
d1
),
66
)
self
.
assertEqual
(
float
(
d1
),
66
)
self
.
assertEqual
(
float
(
d2
),
15.32
)
self
.
assertEqual
(
float
(
d2
),
15.32
)
def
test_nan_to_float
(
self
):
# Test conversions of decimal NANs to float.
# See http://bugs.python.org/issue15544
for
s
in
(
'nan'
,
'nan1234'
,
'-nan'
,
'-nan2468'
):
f
=
float
(
Decimal
(
s
))
self
.
assertTrue
(
math
.
isnan
(
f
))
sign
=
math
.
copysign
(
1.0
,
f
)
self
.
assertEqual
(
sign
,
-
1.0
if
s
.
startswith
(
'-'
)
else
1.0
)
def
test_snan_to_float
(
self
):
for
s
in
(
'snan'
,
'-snan'
,
'snan1357'
,
'-snan1234'
):
d
=
Decimal
(
s
)
self
.
assertRaises
(
ValueError
,
float
,
d
)
def
test_eval_round_trip
(
self
):
def
test_eval_round_trip
(
self
):
#with zero
#with zero
...
...
Misc/NEWS
View file @
088cec3a
...
@@ -95,6 +95,8 @@ Core and Builtins
...
@@ -95,6 +95,8 @@ Core and Builtins
Library
Library
-------
-------
- Issue #15544: Fix Decimal.__float__ to work with payload-carrying NaNs.
- Issue #15199: Fix JavaScript'
s
default
MIME
type
to
application
/
javascript
.
- Issue #15199: Fix JavaScript'
s
default
MIME
type
to
application
/
javascript
.
Patch
by
Bohuslav
Kabrda
.
Patch
by
Bohuslav
Kabrda
.
...
...
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