9
9
.. moduleauthor :: Raymond Hettinger <python at rcn.com>
10
10
.. moduleauthor :: Aahz <aahz at pobox.com>
11
11
.. moduleauthor :: Tim Peters <tim.one at comcast.net>
12
+ .. moduleauthor :: Stefan Krah <skrah at bytereef.org>
12
13
.. sectionauthor :: Raymond D. Hettinger <python at rcn.com>
13
14
14
15
.. import modules for testing inline doctests with the Sphinx doctest builder
20
21
# make sure each group gets a fresh context
21
22
setcontext(Context())
22
23
23
- The :mod: `decimal ` module provides support for decimal floating point
24
- arithmetic. It offers several advantages over the :class: `float ` datatype:
24
+ The :mod: `decimal ` module provides support for fast correctly-rounded
25
+ decimal floating point arithmetic. It offers several advantages over the
26
+ :class: `float ` datatype:
25
27
26
28
* Decimal "is based on a floating-point model which was designed with people
27
29
in mind, and necessarily has a paramount guiding principle -- computers must
@@ -92,7 +94,7 @@ computation. Depending on the needs of the application, signals may be ignored,
92
94
considered as informational, or treated as exceptions. The signals in the
93
95
decimal module are: :const: `Clamped `, :const: `InvalidOperation `,
94
96
:const: `DivisionByZero `, :const: `Inexact `, :const: `Rounded `, :const: `Subnormal `,
95
- :const: `Overflow `, and :const: `Underflow `.
97
+ :const: `Overflow `, :const: ` Underflow ` and :const: `FloatOperation `.
96
98
97
99
For each signal there is a flag and a trap enabler. When a signal is
98
100
encountered, its flag is set to one, then, if the trap enabler is
@@ -122,7 +124,7 @@ precision, rounding, or enabled traps::
122
124
123
125
>>> from decimal import *
124
126
>>> getcontext()
125
- Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999 , Emax=999999999 ,
127
+ Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999 , Emax=999999 ,
126
128
capitals=1, clamp=0, flags=[], traps=[Overflow, DivisionByZero,
127
129
InvalidOperation])
128
130
@@ -132,7 +134,7 @@ Decimal instances can be constructed from integers, strings, floats, or tuples.
132
134
Construction from an integer or a float performs an exact conversion of the
133
135
value of that integer or float. Decimal numbers include special values such as
134
136
:const: `NaN ` which stands for "Not a number", positive and negative
135
- :const: `Infinity `, and :const: `-0 `.
137
+ :const: `Infinity `, and :const: `-0 `::
136
138
137
139
>>> getcontext().prec = 28
138
140
>>> Decimal(10)
@@ -152,6 +154,25 @@ value of that integer or float. Decimal numbers include special values such as
152
154
>>> Decimal('-Infinity')
153
155
Decimal('-Infinity')
154
156
157
+ If the :exc: `FloatOperation ` signal is trapped, accidental mixing of
158
+ decimals and floats in constructors or ordering comparisons raises
159
+ an exception::
160
+
161
+ >>> c = getcontext()
162
+ >>> c.traps[FloatOperation] = True
163
+ >>> Decimal(3.14)
164
+ Traceback (most recent call last):
165
+ File "<stdin>", line 1, in <module>
166
+ decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
167
+ >>> Decimal('3.5') < 3.7
168
+ Traceback (most recent call last):
169
+ File "<stdin>", line 1, in <module>
170
+ decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
171
+ >>> Decimal('3.5') == 3.5
172
+ True
173
+
174
+ .. versionadded :: 3.3
175
+
155
176
The significance of a new Decimal is determined solely by the number of digits
156
177
input. Context precision and rounding only come into play during arithmetic
157
178
operations.
@@ -169,6 +190,16 @@ operations.
169
190
>>> Decimal(' 3.1415926535' ) + Decimal(' 2.7182818285' )
170
191
Decimal('5.85988')
171
192
193
+ If the internal limits of the C version are exceeded, constructing
194
+ a decimal raises :class: `InvalidOperation `::
195
+
196
+ >>> Decimal("1e9999999999999999999")
197
+ Traceback (most recent call last):
198
+ File "<stdin>", line 1, in <module>
199
+ decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
200
+
201
+ .. versionchanged :: 3.3
202
+
172
203
Decimals interact well with much of the rest of Python. Here is a small decimal
173
204
floating point flying circus:
174
205
@@ -244,7 +275,7 @@ enabled:
244
275
Decimal('0.142857142857142857142857142857142857142857142857142857142857')
245
276
246
277
>>> ExtendedContext
247
- Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999 , Emax=999999999 ,
278
+ Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999 , Emax=999999 ,
248
279
capitals=1, clamp=0, flags=[], traps=[])
249
280
>>> setcontext(ExtendedContext)
250
281
>>> Decimal(1 ) / Decimal(7 )
@@ -269,7 +300,7 @@ using the :meth:`clear_flags` method. ::
269
300
>>> Decimal(355) / Decimal(113)
270
301
Decimal('3.14159292')
271
302
>>> getcontext()
272
- Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999 , Emax=999999999 ,
303
+ Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999 , Emax=999999 ,
273
304
capitals=1, clamp=0, flags=[Inexact, Rounded], traps=[])
274
305
275
306
The *flags * entry shows that the rational approximation to :const: `Pi ` was
@@ -358,6 +389,10 @@ Decimal objects
358
389
The argument to the constructor is now permitted to be a :class: `float `
359
390
instance.
360
391
392
+ .. versionchanged :: 3.3
393
+ :class: `float ` arguments raise an exception if the :exc: `FloatOperation `
394
+ trap is set. By default the trap is off.
395
+
361
396
Decimal floating point objects share many properties with the other built-in
362
397
numeric types such as :class: `float ` and :class: `int `. All of the usual math
363
398
operations and special methods apply. Likewise, decimal objects can be
@@ -880,39 +915,33 @@ described below. In addition, the module provides three pre-made contexts:
880
915
In single threaded environments, it is preferable to not use this context at
881
916
all. Instead, simply create contexts explicitly as described below.
882
917
883
- The default values are precision=28, rounding=ROUND_HALF_EVEN, and enabled traps
884
- for Overflow, InvalidOperation, and DivisionByZero.
918
+ The default values are :attr: `prec `\ =\ :const: `28 `,
919
+ :attr: `rounding `\ =\ :const: `ROUND_HALF_EVEN `,
920
+ and enabled traps for :class: `Overflow `, :class: `InvalidOperation `, and
921
+ :class: `DivisionByZero `.
885
922
886
923
In addition to the three supplied contexts, new contexts can be created with the
887
924
:class: `Context ` constructor.
888
925
889
926
890
- .. class :: Context(prec=None, rounding=None, traps =None, flags =None, Emin =None, Emax =None, capitals =None, clamp =None)
927
+ .. class :: Context(prec=None, rounding=None, Emin =None, Emax =None, capitals =None, clamp =None, flags =None, traps =None)
891
928
892
929
Creates a new context. If a field is not specified or is :const: `None `, the
893
930
default values are copied from the :const: `DefaultContext `. If the *flags *
894
931
field is not specified or is :const: `None `, all flags are cleared.
895
932
896
- The *prec * field is a positive integer that sets the precision for arithmetic
897
- operations in the context.
898
-
899
- The *rounding * option is one of:
933
+ *prec * is an integer in the range [:const: `1 `, :const: `MAX_PREC `] that sets
934
+ the precision for arithmetic operations in the context.
900
935
901
- * :const: `ROUND_CEILING ` (towards :const: `Infinity `),
902
- * :const: `ROUND_DOWN ` (towards zero),
903
- * :const: `ROUND_FLOOR ` (towards :const: `-Infinity `),
904
- * :const: `ROUND_HALF_DOWN ` (to nearest with ties going towards zero),
905
- * :const: `ROUND_HALF_EVEN ` (to nearest with ties going to nearest even integer),
906
- * :const: `ROUND_HALF_UP ` (to nearest with ties going away from zero), or
907
- * :const: `ROUND_UP ` (away from zero).
908
- * :const: `ROUND_05UP ` (away from zero if last digit after rounding towards zero
909
- would have been 0 or 5; otherwise towards zero)
936
+ The *rounding * option is one of the constants listed in the section
937
+ `Rounding Modes `_.
910
938
911
939
The *traps * and *flags * fields list any signals to be set. Generally, new
912
940
contexts should only set traps and leave the flags clear.
913
941
914
942
The *Emin * and *Emax * fields are integers specifying the outer limits allowable
915
- for exponents.
943
+ for exponents. *Emin * must be in the range [:const: `MIN_EMIN `, :const: `0 `],
944
+ *Emax * in the range [:const: `0 `, :const: `MAX_EMAX `].
916
945
917
946
The *capitals * field is either :const: `0 ` or :const: `1 ` (the default). If set to
918
947
:const: `1 `, exponents are printed with a capital :const: `E `; otherwise, a
@@ -951,6 +980,12 @@ In addition to the three supplied contexts, new contexts can be created with the
951
980
952
981
Resets all of the flags to :const: `0 `.
953
982
983
+ .. method :: clear_traps()
984
+
985
+ Resets all of the traps to :const: `0 `.
986
+
987
+ .. versionadded :: 3.3
988
+
954
989
.. method :: copy()
955
990
956
991
Return a duplicate of the context.
@@ -1250,8 +1285,13 @@ In addition to the three supplied contexts, new contexts can be created with the
1250
1285
With two arguments, compute ``x**y ``. If ``x `` is negative then ``y ``
1251
1286
must be integral. The result will be inexact unless ``y `` is integral and
1252
1287
the result is finite and can be expressed exactly in 'precision' digits.
1253
- The result should always be correctly rounded, using the rounding mode of
1254
- the current thread's context.
1288
+ The rounding mode of the context is used. Results are always correctly-rounded
1289
+ in the Python version.
1290
+
1291
+ .. versionchanged :: 3.3
1292
+ The C module computes :meth: `power ` in terms of the correctly-rounded
1293
+ :meth: `exp ` and :meth: `ln ` functions. The result is well-defined but
1294
+ only "almost always correctly-rounded".
1255
1295
1256
1296
With three arguments, compute ``(x**y) % modulo ``. For the three argument
1257
1297
form, the following restrictions on the arguments hold:
@@ -1339,6 +1379,66 @@ In addition to the three supplied contexts, new contexts can be created with the
1339
1379
1340
1380
.. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1341
1381
1382
+ .. _decimal-rounding-modes :
1383
+
1384
+ Constants
1385
+ ---------
1386
+
1387
+ The constants in this section are only relevant for the C module. They
1388
+ are also included in the pure Python version for compatibility.
1389
+
1390
+ +--------------------+---------------------+------------------------------+
1391
+ | | 32-bit | 64-bit |
1392
+ +====================+=====================+==============================+
1393
+ | .. data:: MAX_PREC | :const: `425000000 ` | :const: `999999999999999999 ` |
1394
+ +--------------------+---------------------+------------------------------+
1395
+ | .. data:: MAX_EMAX | :const: `425000000 ` | :const: `999999999999999999 ` |
1396
+ +--------------------+---------------------+------------------------------+
1397
+ | .. data:: MIN_EMIN | :const: `-425000000 ` | :const: `-999999999999999999 ` |
1398
+ +--------------------+---------------------+------------------------------+
1399
+
1400
+ .. data :: HAVE_THREADS
1401
+
1402
+ The default value is True. If Python is compiled without threads, the
1403
+ C version automatically disables the expensive thread local context
1404
+ machinery. In this case, the value is False.
1405
+
1406
+ Rounding modes
1407
+ --------------
1408
+
1409
+ .. data :: ROUND_CEILING
1410
+
1411
+ Round towards :const: `Infinity `.
1412
+
1413
+ .. data :: ROUND_DOWN
1414
+
1415
+ Round towards zero.
1416
+
1417
+ .. data :: ROUND_FLOOR
1418
+
1419
+ Round towards :const: `-Infinity `.
1420
+
1421
+ .. data :: ROUND_HALF_DOWN
1422
+
1423
+ Round to nearest with ties going towards zero.
1424
+
1425
+ .. data :: ROUND_HALF_EVEN
1426
+
1427
+ Round to nearest with ties going to nearest even integer.
1428
+
1429
+ .. data :: ROUND_HALF_UP
1430
+
1431
+ Round to nearest with ties going away from zero.
1432
+
1433
+ .. data :: ROUND_UP
1434
+
1435
+ Round away from zero.
1436
+
1437
+ .. data :: ROUND_05UP
1438
+
1439
+ Round away from zero if last digit after rounding towards zero would have
1440
+ been 0 or 5; otherwise round towards zero.
1441
+
1342
1442
1343
1443
.. _decimal-signals :
1344
1444
@@ -1403,7 +1503,6 @@ condition.
1403
1503
Infinity / Infinity
1404
1504
x % 0
1405
1505
Infinity % x
1406
- x._rescale( non-integer )
1407
1506
sqrt(-x) and x > 0
1408
1507
0 ** 0
1409
1508
x ** (non-integer)
@@ -1446,6 +1545,23 @@ condition.
1446
1545
Occurs when a subnormal result is pushed to zero by rounding. :class: `Inexact `
1447
1546
and :class: `Subnormal ` are also signaled.
1448
1547
1548
+
1549
+ .. class :: FloatOperation
1550
+
1551
+ Enable stricter semantics for mixing floats and Decimals.
1552
+
1553
+ If the signal is not trapped (default), mixing floats and Decimals is
1554
+ permitted in the :class: `~decimal.Decimal ` constructor,
1555
+ :meth: `~decimal.Context.create_decimal ` and all comparison operators.
1556
+ Both conversion and comparisons are exact. Any occurrence of a mixed
1557
+ operation is silently recorded by setting :exc: `FloatOperation ` in the
1558
+ context flags. Explicit conversions with :meth: `~decimal.Decimal.from_float `
1559
+ or :meth: `~decimal.Context.create_decimal_from_float ` do not set the flag.
1560
+
1561
+ Otherwise (the signal is trapped), only equality comparisons and explicit
1562
+ conversions are silent. All other mixed operations raise :exc: `FloatOperation `.
1563
+
1564
+
1449
1565
The following table summarizes the hierarchy of signals::
1450
1566
1451
1567
exceptions.ArithmeticError(exceptions.Exception)
@@ -1458,10 +1574,12 @@ The following table summarizes the hierarchy of signals::
1458
1574
InvalidOperation
1459
1575
Rounded
1460
1576
Subnormal
1577
+ FloatOperation
1461
1578
1462
1579
.. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463
1580
1464
1581
1582
+
1465
1583
.. _decimal-notes :
1466
1584
1467
1585
Floating Point Notes
@@ -1571,7 +1689,7 @@ normalized floating point representations, it is not immediately obvious that
1571
1689
the following calculation returns a value equal to zero:
1572
1690
1573
1691
>>> 1 / Decimal(' Infinity' )
1574
- Decimal('0E-1000000026 ')
1692
+ Decimal('0E-1000026 ')
1575
1693
1576
1694
.. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1577
1695
@@ -1583,7 +1701,7 @@ Working with threads
1583
1701
1584
1702
The :func: `getcontext ` function accesses a different :class: `Context ` object for
1585
1703
each thread. Having separate thread contexts means that threads may make
1586
- changes (such as ``getcontext.prec=10 ``) without interfering with other threads.
1704
+ changes (such as ``getcontext() .prec=10 ``) without interfering with other threads.
1587
1705
1588
1706
Likewise, the :func: `setcontext ` function automatically assigns its target to
1589
1707
the current thread.
0 commit comments