• Home
  • Raw
  • Download

Lines Matching full:decimal

471 PEP 327: Decimal Data Type
477 unaware that floating-point numbers don't represent certain decimal fractions
478 accurately. The new :class:`Decimal` type can represent these fractions
482 Why is Decimal needed?
492 decimal notation.
494 * The exponent, which tells where the decimal point is located in the number
498 binary), and an exponent of 0 (the decimal point doesn't need to be shifted).
510 For example, 1.1 decimal is binary ``1.0001100110011 ...``; .1 = 1/16 + 1/32 +
512 that infinitely repeated decimal after 52 digits, so the representation is
521 decimal-string conversion is provided by the C library, and most C libraries try
528 decimal places, and if you round the number to two or three or even eight
529 decimal places, the error is never apparent. However, for applications where it
533 Hence, the :class:`Decimal` type was created.
536 The :class:`Decimal` type
539 A new module, :mod:`decimal`, was added to Python's standard library. It
540 contains two classes, :class:`Decimal` and :class:`Context`. :class:`Decimal`
544 :class:`Decimal` instances are immutable, like regular Python integers and FP
546 represents. :class:`Decimal` instances can be created from integers or
549 >>> import decimal
550 >>> decimal.Decimal(1972)
551 Decimal("1972")
552 >>> decimal.Decimal("1.1")
553 Decimal("1.1")
556 tuple of decimal digits, and the exponent::
558 >>> decimal.Decimal((1, (1, 4, 7, 5), -2))
559 Decimal("-14.75")
565 number representing 1.1 turn into the decimal number for exactly 1.1, or for 1.1
569 string to the :class:`Decimal` constructor::
572 >>> decimal.Decimal(str(f))
573 Decimal("1.1")
574 >>> decimal.Decimal('%.12f' % f)
575 Decimal("1.100000000000")
577 Once you have :class:`Decimal` instances, you can perform the usual mathematical
581 >>> a = decimal.Decimal('35.72')
582 >>> b = decimal.Decimal('1.73')
584 Decimal("37.45")
586 Decimal("33.99")
588 Decimal("61.7956")
590 Decimal("20.64739884393063583815028902")
592 Decimal("1275.9184")
596 decimal.InvalidOperation: x ** (non-integer)
598 You can combine :class:`Decimal` instances with integers, but not with floating-
602 Decimal("39.72")
606 TypeError: You can interact Decimal only with int, long or Decimal data types.
609 :class:`Decimal` numbers can be used with the :mod:`math` and :mod:`cmath`
613 and not a :class:`Decimal`. ::
616 >>> d = decimal.Decimal('123456789012.345')
622 :class:`Decimal` instances have a :meth:`sqrt` method that returns a
623 :class:`Decimal`, but if you need other things such as trigonometric functions
627 Decimal("351364.1828820134592177245001")
634 decimal operations:
636 * :attr:`prec` is the precision, the number of decimal places.
638 * :attr:`rounding` specifies the rounding mode. The :mod:`decimal` module has
652 >>> decimal.getcontext().prec
654 >>> decimal.Decimal(1) / decimal.Decimal(7)
655 Decimal("0.1428571428571428571428571429")
656 >>> decimal.getcontext().prec = 9
657 >>> decimal.Decimal(1) / decimal.Decimal(7)
658 Decimal("0.142857143")
664 >>> decimal.Decimal(1) / decimal.Decimal(0)
667 decimal.DivisionByZero: x / 0
668 >>> decimal.getcontext().traps[decimal.DivisionByZero] = False
669 >>> decimal.Decimal(1) / decimal.Decimal(0)
670 Decimal("Infinity")
676 For more information, see the documentation for the :mod:`decimal` module, which
682 :pep:`327` - Decimal Data Type
690 http://speleotrove.com/decimal/
691 A description of a decimal-based representation. This representation is being
692 proposed as a standard, and underlies the new Python decimal type. Much of this