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
4631481d
Kaydet (Commit)
4631481d
authored
Şub 14, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add fixed-point examples to the decimal FAQ
üst
1b563244
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
3 deletions
+30
-3
decimal.rst
Doc/library/decimal.rst
+30
-3
No files found.
Doc/library/decimal.rst
Dosyayı görüntüle @
4631481d
...
@@ -1565,9 +1565,36 @@ the :const:`Inexact` trap is set, it is also useful for validation::
...
@@ -1565,9 +1565,36 @@ the :const:`Inexact` trap is set, it is also useful for validation::
Q. Once I have valid two place inputs, how do I maintain that invariant
Q. Once I have valid two place inputs, how do I maintain that invariant
throughout an application?
throughout an application?
A. Some operations like addition and subtraction automatically preserve fixed
A. Some operations like addition, subtraction, and multiplication by an integer
point. Others, like multiplication and division, change the number of decimal
will automatically preserve fixed point. Others operations, like division and
places and need to be followed-up with a :meth:`quantize` step.
non-integer multiplication, will change the number of decimal places and need to
be followed-up with a :meth:`quantize` step.
>>> a = Decimal('102.72') # Initial fixed-point values
>>> b = Decimal('3.17')
>>> a + b # Addition preserves fixed-point
Decimal('105.89')
>>> a - b
Decimal('99.55')
>>> a * 42 # So does integer multiplication
Decimal('4314.24')
>>> (a * b).quantize(TWOPLACES) # Must quantize non-integer multiplication
Decimal('325.62')
>>> (b / a).quantize(TWOPLACES) # And quantize divisions
Decimal('0.03')
In developing fixed-point applications, it is convenient to define functions
to handle the :meth:`quantize` step::
def mul(x, y, fp=TWOPLACES):
return (x * y).quantize(fp)
def div(x, y, fp=TWOPLACES):
return (x / y).quantize(fp)
>>> mul(a, b) # Automatically preserve fixed-point
Decimal('325.62')
>>> div(b, a)
Decimal('0.03')
Q. There are many ways to express the same value. The numbers :const:`200`,
Q. There are many ways to express the same value. The numbers :const:`200`,
:const:`200.000`, :const:`2E2`, and :const:`.02E+4` all have the same value at
:const:`200.000`, :const:`2E2`, and :const:`.02E+4` all have the same value at
...
...
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