99binary options clearing house18 comments
Best trades to get into australia
The decimal module provides support for decimal floating point arithmetic. It offers several advantages over the float datatype:. Decimal numbers can be represented exactly. In contrast, numbers like 1. End users typically would not expect 1. The exactness carries over into arithmetic. In decimal floating point, 0. In binary floating point, the result is 5. While near to zero, the differences prevent reliable equality testing and differences can accumulate.
For this reason, decimal is preferred in accounting applications which have strict equality invariants. The decimal module incorporates a notion of significant places so that 1. The trailing zero is kept to indicate significance. This is the customary presentation for monetary applications.
Unlike hardware based binary floating point, the decimal module has a user alterable precision defaulting to 28 places which can be as large as needed for a given problem:. Both binary and decimal floating point are implemented in terms of published standards. While the built-in float type exposes only a modest portion of its capabilities, the decimal module exposes all required parts of the standard.
When needed, the programmer has full control over rounding and signal handling. This includes an option to enforce exact arithmetic by using exceptions to block any inexact operations. The module design is centered around three concepts: A decimal number is immutable. It has a sign, coefficient digits, and an exponent. To preserve significance, the coefficient digits do not truncate trailing zeros.
Decimals also include special values such as Infinity , -Infinity , and NaN. The context for arithmetic is an environment specifying precision, rounding rules, limits on exponents, flags indicating the results of operations, and trap enablers which determine whether signals are treated as exceptions.
Signals are groups of exceptional conditions arising during the course of computation. Depending on the needs of the application, signals may be ignored, considered as informational, or treated as exceptions. The signals in the decimal module are: For each signal there is a flag and a trap enabler.
When a signal is encountered, its flag is set to one, then, if the trap enabler is set to one, an exception is raised. Flags are sticky, so the user needs to reset them before monitoring a calculation. The usual start to using decimals is importing the module, viewing the current context with getcontext and, if necessary, setting new values for precision, rounding, or enabled traps:.
Decimal instances can be constructed from integers, strings, floats, or tuples. Construction from an integer or a float performs an exact conversion of the value of that integer or float. The significance of a new Decimal is determined solely by the number of digits input. Context precision and rounding only come into play during arithmetic operations. Decimals interact well with much of the rest of Python. Here is a small decimal floating point flying circus:.
The 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 places:. As shown above, the getcontext function accesses the current context and allows the settings to be changed. This approach meets the needs of most applications. For more advanced work, it may be useful to create alternate contexts using the Context constructor. To make an alternate active, use the setcontext function.
In accordance with the standard, the decimal module provides two ready to use standard contexts, BasicContext and ExtendedContext. The former is especially useful for debugging because many of the traps are enabled:. Contexts also have signal flags for monitoring exceptional conditions encountered during computations. The flags entry shows that the rational approximation to Pi was rounded digits beyond the context precision were thrown away and that the result is inexact some of the discarded digits were non-zero.
Individual traps are set using the dictionary in the traps field of a context:. Most programs adjust the current context only once, at the beginning of the program.
And, in many applications, data is converted to Decimal with a single cast inside a loop. With context set and decimals created, the bulk of the program manipulates the data no differently than with other Python numeric types. Construct a new Decimal object based from value. If no value is given, returns Decimal '0'. If value is a string, it should conform to the decimal numeric string syntax after leading and trailing whitespace characters are removed:.
If value is a unicode string then other Unicode decimal digits are also permitted where digit appears above. If value is a tuple , it should have three components, a sign 0 for positive or 1 for negative , a tuple of digits, and an integer exponent.
For example, Decimal 0, 1, 4, 1, 4 , -3 returns Decimal '1. If value is a float , the binary floating point value is losslessly converted to its exact decimal equivalent. This conversion can often require 53 or more digits of precision. For example, Decimal float '1.
The context precision does not affect how many digits are stored. That is determined exclusively by the number of digits in value. For example, Decimal '3. The purpose of the context argument is determining what to do if value is a malformed string. If the context traps InvalidOperation , an exception is raised; otherwise, the constructor returns a new Decimal with the value of NaN. Once constructed, Decimal objects are immutable.
Changed in version 2. The argument to the constructor is now permitted to be a float instance. Decimal floating point objects share many properties with the other built-in numeric types such as float and int.
All of the usual math operations and special methods apply. Likewise, decimal objects can be copied, pickled, printed, used as dictionary keys, used as set elements, compared, sorted, and coerced to another type such as float or long. There are some small differences between arithmetic on Decimal objects and arithmetic on integers and floats. Decimal objects cannot generally be combined with floats in arithmetic operations: Without this exception, comparisons between Decimal and float instances would follow the general rules for comparing objects of different types described in the Expressions section of the reference manual, leading to confusing results.
A comparison between a float instance x and a Decimal instance y now returns a result based on the values of x and y. In addition to the standard numeric properties, decimal floating point objects also have a number of specialized methods:. Used for determining the position of the most significant digit with respect to the decimal point. Return a named tuple representation of the number: DecimalTuple sign, digits, exponent. Return the canonical encoding of the argument.
Currently, the encoding of a Decimal instance is always canonical, so this operation returns its argument unchanged. Compare the values of two Decimal instances.
This operation is identical to the compare method, except that all NaNs signal. That is, if neither operand is a signaling NaN then any quiet NaN operand is treated as though it were a signaling NaN.
Compare two operands using their abstract representation rather than their numerical value. Similar to the compare method, but the result gives a total ordering on Decimal instances. Two Decimal instances with the same numeric value but different representations compare unequal in this ordering:.
Quiet and signaling NaNs are also included in the total ordering. The result of this function is Decimal '0' if both operands have the same representation, Decimal '-1' if the first operand is lower in the total order than the second, and Decimal '1' if the first operand is higher in the total order than the second operand. See the specification for details of the total order.
Return the absolute value of the argument. This operation is unaffected by the context and is quiet: Return the negation of the argument. Return a copy of the first operand with the sign set to be the same as the sign of the second operand. That equivalent value in decimal is 0. Return True if the argument is canonical and False otherwise.
Currently, a Decimal instance is always canonical, so this operation always returns True. Return True if the argument is a finite number, and False if the argument is an infinity or a NaN. Return True if the argument is either positive or negative infinity and False otherwise.