Lines Matching +full:sign +full:- +full:compare
1 :mod:`decimal` --- Decimal fixed point and floating point arithmetic
31 --------------
37 * Decimal "is based on a floating-point model which was designed with people
38 in mind, and necessarily has a paramount guiding principle -- computers must
40 people learn at school." -- excerpt from the decimal arithmetic specification.
48 + 0.1 + 0.1 - 0.3`` is exactly equal to zero. In binary floating point, the result
49 is ``5.5511151231257827e-017``. While near to zero, the differences
74 standards. While the built-in float type exposes only a modest portion of its
81 unrounded decimal arithmetic (sometimes called fixed-point arithmetic)
82 and rounded floating-point arithmetic." -- excerpt from the decimal
88 A decimal number is immutable. It has a sign, coefficient digits, and an
91 ``Infinity``, ``-Infinity``, and ``NaN``. The standard also
92 differentiates ``-0`` from ``+0``.
122 .. _decimal-tutorial:
124 Quick-start Tutorial
125 --------------------
133 Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999,
143 ``Infinity``, and ``-0``::
152 >>> Decimal((0, (3, 1, 4), -2))
160 >>> Decimal('-Infinity')
161 Decimal('-Infinity')
284 Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999,
295 File "<pyshell#143>", line 1, in -toplevel-
309 Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999,
314 result is inexact (some of the discarded digits were non-zero).
327 File "<pyshell#112>", line 1, in -toplevel-
340 .. _decimal-decimal:
343 ---------------
355 sign ::= '+' | '-'
359 decimal-part ::= digits '.' [digits] | ['.'] digits
360 exponent-part ::= indicator [sign] digits
363 numeric-value ::= decimal-part [exponent-part] | infinity
364 numeric-string ::= [sign] numeric-value | [sign] nan
368 alphabets (for example, Arabic-Indic and Devanāgarī digits) along
371 If *value* is a :class:`tuple`, it should have three components, a sign
373 digits, and an integer exponent. For example, ``Decimal((0, (1, 4, 1, 4), -3))``
403 Underscores are allowed for grouping, as with integral and floating-point
406 Decimal floating point objects share many properties with the other built-in
415 applied to Decimal objects, the sign of the result is the sign of the
416 *dividend* rather than the sign of the divisor::
418 >>> (-7) % 4
420 >>> Decimal(-7) % Decimal(4)
421 Decimal('-3')
427 >>> -7 // 4
428 -2
429 >>> Decimal(-7) // Decimal(4)
430 Decimal('-1')
433 ``divide-integer`` operations (respectively) as described in the
440 use Python's comparison operators to compare a :class:`Decimal`
445 Mixed-type comparisons between :class:`Decimal` instances and other
465 >>> Decimal('-3.14').as_integer_ratio()
466 (-157, 50)
476 ``DecimalTuple(sign, digits, exponent)``.
485 .. method:: compare(other, context=None)
487 Compare the values of two Decimal instances. :meth:`compare` returns a
492 a < b ==> Decimal('-1')
498 This operation is identical to the :meth:`compare` method, except that all
504 Compare two operands using their abstract representation rather than their
505 numerical value. Similar to the :meth:`compare` method, but the result
508 representations compare unequal in this ordering:
511 Decimal('-1')
515 representation, ``Decimal('-1')`` if the first operand is lower in the
526 Compare two operands using their abstract representation rather than their
527 value as in :meth:`compare_total`, but ignoring the sign of each operand.
553 Return a copy of the first operand with the sign set to be the same as the
554 sign of the second operand. For example:
556 >>> Decimal('2.3').copy_sign(Decimal('-1.5'))
557 Decimal('-2.3')
582 ``0x1.999999999999ap-4``. That equivalent value in decimal is
596 >>> Decimal.from_float(float('-inf'))
597 Decimal('-Infinity')
603 Fused multiply-add. Return self*other+third with no rounding of the
642 Return :const:`True` if the argument has a negative sign and
674 ``Decimal('-Infinity')`` is returned and the :const:`DivisionByZero` flag
682 digit-wise ``and`` of the two operands.
687 result is the digit-wise inversion of the operand.
693 digit-wise ``or`` of the two operands.
699 digit-wise exclusive or of the two operands.
741 numerically equal, return a copy of the first operand with the sign set to
742 be the same as the sign of the second operand.
758 * ``"-Infinity"``, indicating that the operand is negative infinity.
759 * ``"-Normal"``, indicating that the operand is a negative normal number.
760 * ``"-Subnormal"``, indicating that the operand is negative and subnormal.
761 * ``"-Zero"``, indicating that the operand is a negative zero.
781 the right-hand operand.
804 ``self % other`` in that the sign of the remainder is chosen so as to
806 ``self - n * other`` where ``n`` is the integer nearest to the exact
810 If the result is zero then its sign will be the sign of *self*.
813 Decimal('-2')
817 Decimal('-5')
823 the range -precision through precision. The absolute value of the second
827 length precision if necessary. The sign and exponent of the first operand
849 the range -precision through precision. The absolute value of the second
852 right. Digits shifted into the coefficient are zeros. The sign and
898 exponent and sign are both zero, and whose digits are all either
904 .. _decimal-context:
907 ---------------
932 to a copy of *ctx* on entry to the with-statement and restore the previous context
933 when exiting the with-statement. If no context is specified, a copy of the
963 described below. In addition, the module provides three pre-made contexts:
996 This context is most useful in multi-threaded environments. Changing one of the
997 fields before threads are started has the effect of setting system-wide
1039 range ``Emin - prec + 1 <= e <= Emax - prec + 1``. If *clamp* is
1052 fixed-width decimal interchange formats specified in IEEE 754.
1103 This method implements the to-number operation of the IBM specification.
1129 Returns a value equal to ``Emin - prec + 1`` which is the minimum exponent
1135 Returns a value equal to ``Emax - prec + 1``.
1160 .. method:: compare(x, y)
1177 Compares two operands using their abstract representation, ignoring sign.
1182 Returns a copy of *x* with the sign set to 0.
1187 Returns a copy of *x* with the sign inverted.
1192 Copies the sign from *y* to *x*.
1312 Compares the values numerically with their sign ignored.
1322 Compares the values numerically with their sign ignored.
1382 :meth:`exp` and :meth:`ln` functions. The result is well-defined but
1388 - all three arguments must be integral
1389 - ``y`` must be nonnegative
1390 - at least one of ``x`` or ``y`` must be nonzero
1391 - ``modulo`` must be nonzero and have at most 'precision' digits
1415 The sign of the result, if non-zero, is the same as that of the original
1421 Returns ``x - y * n``, where *n* is the integer nearest the exact value
1422 of ``x / y`` (if the result is 0 then its sign will be the sign of *x*).
1447 Square root of a non-negative number to context precision.
1475 .. _decimal-rounding-modes:
1478 ---------
1483 +---------------------+---------------------+-------------------------------+
1484 | | 32-bit | 64-bit |
1487 +---------------------+---------------------+-------------------------------+
1489 +---------------------+---------------------+-------------------------------+
1490 | .. data:: MIN_EMIN | ``-425000000`` | ``-999999999999999999`` |
1491 +---------------------+---------------------+-------------------------------+
1492 | .. data:: MIN_ETINY | ``-849999999`` | ``-1999999999999999997`` |
1493 +---------------------+---------------------+-------------------------------+
1505 the --without-decimal-contextvar option <--without-decimal-contextvar>`,
1506 the C version uses a thread-local rather than a coroutine-local context and the value
1513 --------------
1525 Round towards ``-Infinity``.
1549 .. _decimal-signals:
1552 -------
1584 Signals the division of a non-infinite number by zero.
1588 ``-Infinity`` with the sign determined by the inputs to the calculation.
1595 Signals when non-zero digits were discarded during rounding. The rounded result
1607 Infinity - Infinity
1612 sqrt(-x) and x > 0
1614 x ** (non-integer)
1689 .. _decimal-notes:
1692 --------------------
1695 Mitigating round-off error with increased precision
1700 can still incur round-off error when non-zero digits exceed the fixed precision.
1702 The effects of round-off error can be amplified by the addition or subtraction
1714 >>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')
1720 >>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')
1732 >>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')
1738 >>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')
1749 including ``NaN``, ``sNaN``, ``-Infinity``, ``Infinity``,
1750 and two zeros, ``+0`` and ``-0``.
1766 series of computations that occasionally have missing inputs --- it allows the
1777 :const:`True`. An attempt to compare two Decimals using any of the ``<``,
1783 section 5.7). To ensure strict standards-compliance, use the :meth:`~Decimal.compare`
1786 The signed zeros can result from calculations that underflow. They keep the sign
1789 treated as equal and their sign is informational.
1798 Decimal('0E-1000026')
1803 .. _decimal-threads:
1806 --------------------
1841 .. _decimal-recipes:
1844 -------
1850 pos='', neg='-', trailneg=''):
1854 curr: optional currency symbol before the sign (may be blank)
1858 pos: optional sign for positive numbers: '+', space or blank
1859 neg: optional sign for negative numbers: '-', '(', space or blank
1860 trailneg:optional trailing minus indicator: '-', ')', space or blank
1862 >>> d = Decimal('-1234567.8901')
1864 '-$1,234,567.89'
1865 >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
1866 '1.234.568-'
1871 >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')
1875 q = Decimal(10) ** -places # 2 places --> '0.01'
1876 sign, digits, exp = value.quantize(q).as_tuple()
1880 if sign:
1896 build(neg if sign else pos)
1915 getcontext().prec -= 2
1939 getcontext().prec -= 2
1957 i, lasts, s, fact, num, sign = 0, 0, 1, 1, 1, 1
1961 fact *= i * (i-1)
1963 sign *= -1
1964 s += num / fact * sign
1965 getcontext().prec -= 2
1983 i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1
1987 fact *= i * (i-1)
1989 sign *= -1
1990 s += num / fact * sign
1991 getcontext().prec -= 2
1998 .. _decimal-faq:
2001 -----------
2012 Q. In a fixed-point application with two decimal places, some inputs have many
2019 >>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01')
2039 non-integer multiplication, will change the number of decimal places and need to
2040 be followed-up with a :meth:`~Decimal.quantize` step:
2042 >>> a = Decimal('102.72') # Initial fixed-point values
2044 >>> a + b # Addition preserves fixed-point
2046 >>> a - b
2050 >>> (a * b).quantize(TWOPLACES) # Must quantize non-integer multiplication
2055 In developing fixed-point applications, it is convenient to define functions
2063 >>> mul(a, b) # Automatically preserve fixed-point
2081 to get a non-exponential representation?
2086 original's two-place significance.
2113 re-run calculations using greater precision and with various rounding modes.
2115 ill-conditioned inputs, or a numerically unstable algorithm.
2157 for medium-sized numbers and the `Number Theoretic Transform
2158 <https://en.wikipedia.org/wiki/Discrete_Fourier_transform_(general)#Number-theoretic_transform>`_
2174 For inexact results, :attr:`MAX_PREC` is far too large on 64-bit platforms and
2188 >>> # Maximum number of digits for a single operand using 500MB in 8-byte words
2189 >>> # with 19 digits per word (4-byte and 9 digits for the 32-bit build):
2216 This approach now works for all exact results except for non-integer powers.