decimal.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """Decimal fixed-point and floating-point arithmetic.
  2. This is an implementation of decimal floating-point arithmetic based on
  3. the General Decimal Arithmetic Specification:
  4. http://speleotrove.com/decimal/decarith.html
  5. and IEEE standard 854-1987:
  6. http://en.wikipedia.org/wiki/IEEE_854-1987
  7. Decimal floating point has finite precision with arbitrarily large bounds.
  8. The purpose of this module is to support arithmetic using familiar
  9. "schoolhouse" rules and to avoid some of the tricky representation
  10. issues associated with binary floating point. The package is especially
  11. useful for financial applications or for contexts where users have
  12. expectations that are at odds with binary floating point (for instance,
  13. in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
  14. of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected
  15. Decimal('0.00')).
  16. Here are some examples of using the decimal module:
  17. >>> from decimal import *
  18. >>> setcontext(ExtendedContext)
  19. >>> Decimal(0)
  20. Decimal('0')
  21. >>> Decimal('1')
  22. Decimal('1')
  23. >>> Decimal('-.0123')
  24. Decimal('-0.0123')
  25. >>> Decimal(123456)
  26. Decimal('123456')
  27. >>> Decimal('123.45e12345678')
  28. Decimal('1.2345E+12345680')
  29. >>> Decimal('1.33') + Decimal('1.27')
  30. Decimal('2.60')
  31. >>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41')
  32. Decimal('-2.20')
  33. >>> dig = Decimal(1)
  34. >>> print(dig / Decimal(3))
  35. 0.333333333
  36. >>> getcontext().prec = 18
  37. >>> print(dig / Decimal(3))
  38. 0.333333333333333333
  39. >>> print(dig.sqrt())
  40. 1
  41. >>> print(Decimal(3).sqrt())
  42. 1.73205080756887729
  43. >>> print(Decimal(3) ** 123)
  44. 4.85192780976896427E+58
  45. >>> inf = Decimal(1) / Decimal(0)
  46. >>> print(inf)
  47. Infinity
  48. >>> neginf = Decimal(-1) / Decimal(0)
  49. >>> print(neginf)
  50. -Infinity
  51. >>> print(neginf + inf)
  52. NaN
  53. >>> print(neginf * inf)
  54. -Infinity
  55. >>> print(dig / 0)
  56. Infinity
  57. >>> getcontext().traps[DivisionByZero] = 1
  58. >>> print(dig / 0)
  59. Traceback (most recent call last):
  60. ...
  61. ...
  62. ...
  63. decimal.DivisionByZero: x / 0
  64. >>> c = Context()
  65. >>> c.traps[InvalidOperation] = 0
  66. >>> print(c.flags[InvalidOperation])
  67. 0
  68. >>> c.divide(Decimal(0), Decimal(0))
  69. Decimal('NaN')
  70. >>> c.traps[InvalidOperation] = 1
  71. >>> print(c.flags[InvalidOperation])
  72. 1
  73. >>> c.flags[InvalidOperation] = 0
  74. >>> print(c.flags[InvalidOperation])
  75. 0
  76. >>> print(c.divide(Decimal(0), Decimal(0)))
  77. Traceback (most recent call last):
  78. ...
  79. ...
  80. ...
  81. decimal.InvalidOperation: 0 / 0
  82. >>> print(c.flags[InvalidOperation])
  83. 1
  84. >>> c.flags[InvalidOperation] = 0
  85. >>> c.traps[InvalidOperation] = 0
  86. >>> print(c.divide(Decimal(0), Decimal(0)))
  87. NaN
  88. >>> print(c.flags[InvalidOperation])
  89. 1
  90. >>>
  91. """
  92. try:
  93. from _decimal import *
  94. from _decimal import __version__
  95. from _decimal import __libmpdec_version__
  96. except ImportError:
  97. from _pydecimal import *
  98. from _pydecimal import __version__
  99. from _pydecimal import __libmpdec_version__