mirror of https://github.com/python/cpython
Some additions (examples and a bit on the tutorial).
This commit is contained in:
parent
a7f49f733b
commit
e90bc3c81c
|
@ -128,6 +128,8 @@ representation error). Decimal numbers include special values such as
|
||||||
Decimal("3.14")
|
Decimal("3.14")
|
||||||
>>> Decimal(str(2.0 ** 0.5))
|
>>> Decimal(str(2.0 ** 0.5))
|
||||||
Decimal("1.41421356237")
|
Decimal("1.41421356237")
|
||||||
|
>>> Decimal(2) ** Decimal("0.5")
|
||||||
|
Decimal("1.414213562373095048801688724")
|
||||||
>>> Decimal("NaN")
|
>>> Decimal("NaN")
|
||||||
Decimal("NaN")
|
Decimal("NaN")
|
||||||
>>> Decimal("-Infinity")
|
>>> Decimal("-Infinity")
|
||||||
|
@ -177,6 +179,17 @@ floating point flying circus::
|
||||||
>>> c % a
|
>>> c % a
|
||||||
Decimal("0.77")
|
Decimal("0.77")
|
||||||
|
|
||||||
|
And some mathematic functions are also available to Decimal::
|
||||||
|
|
||||||
|
>>> Decimal(2).sqrt()
|
||||||
|
Decimal("1.414213562373095048801688724")
|
||||||
|
>>> Decimal(1).exp()
|
||||||
|
Decimal("2.718281828459045235360287471")
|
||||||
|
>>> Decimal("10").ln()
|
||||||
|
Decimal("2.302585092994045684017991455")
|
||||||
|
>>> Decimal("10").log10()
|
||||||
|
Decimal("1")
|
||||||
|
|
||||||
The :meth:`quantize` method rounds a number to a fixed exponent. This method is
|
The :meth:`quantize` method rounds a number to a fixed exponent. This method is
|
||||||
useful for monetary applications that often round results to a fixed number of
|
useful for monetary applications that often round results to a fixed number of
|
||||||
places::
|
places::
|
||||||
|
@ -419,6 +432,11 @@ also have a number of specialized methods:
|
||||||
given number. The result is correctly rounded using the
|
given number. The result is correctly rounded using the
|
||||||
:const:`ROUND_HALF_EVEN` rounding mode.
|
:const:`ROUND_HALF_EVEN` rounding mode.
|
||||||
|
|
||||||
|
>>> Decimal(1).exp()
|
||||||
|
Decimal("2.718281828459045235360287471")
|
||||||
|
>>> Decimal(321).exp()
|
||||||
|
Decimal("2.561702493119680037517373933E+139")
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: Decimal.fma(other, third[, context])
|
.. method:: Decimal.fma(other, third[, context])
|
||||||
|
@ -426,78 +444,82 @@ also have a number of specialized methods:
|
||||||
Fused multiply-add. Return self*other+third with no rounding of
|
Fused multiply-add. Return self*other+third with no rounding of
|
||||||
the intermediate product self*other.
|
the intermediate product self*other.
|
||||||
|
|
||||||
|
>>> Decimal(2).fma(3, 5)
|
||||||
|
Decimal("11")
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: Decimal.is_canonical()
|
.. method:: Decimal.is_canonical()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is canonical and
|
Return :const:`True` if the argument is canonical and
|
||||||
``Decimal(0)`` otherwise. Currently, a :class:`Decimal` instance
|
:const:`False` otherwise. Currently, a :class:`Decimal` instance
|
||||||
is always canonical, so this operation always returns
|
is always canonical, so this operation always returns
|
||||||
``Decimal(1)``.
|
:const:`True`.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_finite()
|
.. method:: is_finite()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is a finite number, and
|
Return :const:`True` if the argument is a finite number, and
|
||||||
``Decimal(0)`` if the argument is an infinity or a NaN.
|
:const:`False` if the argument is an infinity or a NaN.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_infinite()
|
.. method:: is_infinite()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is either positive or
|
Return :const:`True` if the argument is either positive or
|
||||||
negative infinity and ``Decimal(0)`` otherwise.
|
negative infinity and :const:`False` otherwise.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_nan()
|
.. method:: is_nan()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is a (quiet or signaling)
|
Return :const:`True` if the argument is a (quiet or signaling)
|
||||||
NaN and ``Decimal(0)`` otherwise.
|
NaN and :const:`False` otherwise.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_normal()
|
.. method:: is_normal()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is a *normal* finite number.
|
Return :const:`True` if the argument is a *normal* finite number.
|
||||||
Return ``Decimal(0)`` if the argument is zero, subnormal, infinite
|
Return :const:`False` if the argument is zero, subnormal, infinite
|
||||||
or a NaN.
|
or a NaN.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_qnan()
|
.. method:: is_qnan()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is a quiet NaN, and ``Decimal(0)`` otherwise.
|
Return :const:`True` if the argument is a quiet NaN, and
|
||||||
|
:const:`False` otherwise.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_signed()
|
.. method:: is_signed()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument has a negative sign and
|
Return :const:`True` if the argument has a negative sign and
|
||||||
``Decimal(0)`` otherwise. Note that zeros and NaNs can both carry
|
:const:`False` otherwise. Note that zeros and NaNs can both carry
|
||||||
signs.
|
signs.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_snan()
|
.. method:: is_snan()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is a signaling NaN and
|
Return :const:`True` if the argument is a signaling NaN and
|
||||||
``Decimal(0)`` otherwise.
|
:const:`False` otherwise.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_subnormal()
|
.. method:: is_subnormal()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is subnormal, and
|
Return :const:`True` if the argument is subnormal, and
|
||||||
``Decimal(0)`` otherwise.
|
:const:`False` otherwise.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
.. method:: is_zero()
|
.. method:: is_zero()
|
||||||
|
|
||||||
Return ``Decimal(1)`` if the argument is a (positive or negative)
|
Return :const:`True` if the argument is a (positive or negative)
|
||||||
zero and ``Decimal(0)`` otherwise.
|
zero and :const:`False` otherwise.
|
||||||
|
|
||||||
.. versionadded:: 2.6
|
.. versionadded:: 2.6
|
||||||
|
|
||||||
|
@ -640,6 +662,9 @@ also have a number of specialized methods:
|
||||||
Returns a value equal to the first operand after rounding and
|
Returns a value equal to the first operand after rounding and
|
||||||
having the exponent of the second operand.
|
having the exponent of the second operand.
|
||||||
|
|
||||||
|
>>> Decimal("1.41421356").quantize(Decimal("1.000"))
|
||||||
|
Decimal("1.414")
|
||||||
|
|
||||||
Unlike other operations, if the length of the coefficient after the
|
Unlike other operations, if the length of the coefficient after the
|
||||||
quantize operation would be greater than precision, then an
|
quantize operation would be greater than precision, then an
|
||||||
:const:`InvalidOperation` is signaled. This guarantees that, unless
|
:const:`InvalidOperation` is signaled. This guarantees that, unless
|
||||||
|
|
Loading…
Reference in New Issue