Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
ed171abd
Kaydet (Commit)
ed171abd
authored
Nis 02, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 8257: Decimal constructor to accept float.
üst
dc36d7cf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
11 deletions
+45
-11
decimal.rst
Doc/library/decimal.rst
+13
-9
decimal.py
Lib/decimal.py
+6
-2
test_decimal.py
Lib/test/test_decimal.py
+26
-0
No files found.
Doc/library/decimal.rst
Dosyayı görüntüle @
ed171abd
...
...
@@ -314,7 +314,7 @@ Decimal objects
Construct a new :class:`Decimal` object based from *value*.
*value* can be an integer, string, tuple, or another :class:`Decimal`
*value* can be an integer, string, tuple,
:class:`float`,
or another :class:`Decimal`
object. If no *value* is given, returns ``Decimal('0')``. If *value* is a
string, it should conform to the decimal numeric string syntax after leading
and trailing whitespace characters are removed::
...
...
@@ -341,6 +341,11 @@ Decimal objects
digits, and an integer exponent. For example, ``Decimal((0, (1, 4, 1, 4), -3))``
returns ``Decimal('1.414')``.
If *value* is a :class:`float`, the binary floating point value is losslessly
converted to its exact decimal equivalent. This conversion can often require
upto 53 digits of precision. For example, ``Decimal(float('1.1'))`` converts
to ``Decimal('1.100000000000000088817841970012523233890533447265625')``.
The *context* precision does not affect how many digits are stored. That is
determined exclusively by the number of digits in *value*. For example,
``Decimal('3.00000')`` records all five zeros even if the context precision is
...
...
@@ -1948,17 +1953,16 @@ value unchanged::
'''
return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
Q. Is there a way to convert a regular float to a
Decimal
?
Q. Is there a way to convert a regular float to a
:class:`Decimal`
?
A. Yes, the classmethod :meth:`from_float` makes an exact conversion.
A. Yes, all binary floating point numbers can be exactly expressed as a
Decimal though an exact conversion may take more precision than intuition would
suggest:
The regular decimal constructor does not do this by default because there is
some question about whether it is advisable to mix binary and decimal floating
point. Also, its use requires some care to avoid the representation issues
associated with binary floating point:
.. doctest::
>>> Decimal.from_float(1.1
)
Decimal('1.10000000000000008881784197001252323389053344726562
5')
>>> Decimal(math.pi
)
Decimal('3.14159265358979311599796346854418516159057617187
5')
Q. Within a complex calculation, how can I make sure that I haven't gotten a
spurious result because of insufficient precision or rounding anomalies.
...
...
Lib/decimal.py
Dosyayı görüntüle @
ed171abd
...
...
@@ -648,8 +648,12 @@ class Decimal(object):
return
self
if
isinstance
(
value
,
float
):
raise
TypeError
(
"Cannot convert float in Decimal constructor. "
"Use from_float class method."
)
value
=
Decimal
.
from_float
(
value
)
self
.
_exp
=
value
.
_exp
self
.
_sign
=
value
.
_sign
self
.
_int
=
value
.
_int
self
.
_is_special
=
value
.
_is_special
return
self
raise
TypeError
(
"Cannot convert
%
r to Decimal"
%
value
)
...
...
Lib/test/test_decimal.py
Dosyayı görüntüle @
ed171abd
...
...
@@ -54,6 +54,11 @@ def init():
)
setcontext
(
DefaultTestContext
)
# decorator for skipping tests on non-IEEE 754 platforms
requires_IEEE_754
=
unittest
.
skipUnless
(
float
.
__getformat__
(
"double"
)
.
startswith
(
"IEEE"
),
"test requires IEEE 754 doubles"
)
TESTDATADIR
=
'decimaltestdata'
if
__name__
==
'__main__'
:
file
=
sys
.
argv
[
0
]
...
...
@@ -509,6 +514,27 @@ class DecimalExplicitConstructionTest(unittest.TestCase):
self
.
assertEqual
(
str
(
e
),
'0'
)
self
.
assertNotEqual
(
id
(
d
),
id
(
e
))
@requires_IEEE_754
def
test_explicit_from_float
(
self
):
r
=
Decimal
(
0.1
)
self
.
assertEqual
(
type
(
r
),
Decimal
)
self
.
assertEqual
(
str
(
r
),
'0.1000000000000000055511151231257827021181583404541015625'
)
self
.
assertTrue
(
Decimal
(
float
(
'nan'
))
.
is_qnan
())
self
.
assertTrue
(
Decimal
(
float
(
'inf'
))
.
is_infinite
())
self
.
assertTrue
(
Decimal
(
float
(
'-inf'
))
.
is_infinite
())
self
.
assertEqual
(
str
(
Decimal
(
float
(
'nan'
))),
str
(
Decimal
(
'NaN'
)))
self
.
assertEqual
(
str
(
Decimal
(
float
(
'inf'
))),
str
(
Decimal
(
'Infinity'
)))
self
.
assertEqual
(
str
(
Decimal
(
float
(
'-inf'
))),
str
(
Decimal
(
'-Infinity'
)))
self
.
assertEqual
(
str
(
Decimal
(
float
(
'-0.0'
))),
str
(
Decimal
(
'-0'
)))
for
i
in
range
(
200
):
x
=
random
.
expovariate
(
0.01
)
*
(
random
.
random
()
*
2.0
-
1.0
)
self
.
assertEqual
(
x
,
float
(
Decimal
(
x
)))
# roundtrip
def
test_explicit_context_create_decimal
(
self
):
nc
=
copy
.
copy
(
getcontext
())
...
...
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