To change the rounding algorithm used by the FixedPoint class, set the value of the class method FixedPoint.round
; e.g.,
>>> >>> from fixedpoint import FixedPoint, addHalfAndChop >>> >>> # Demonstrate the default rounding ... >>> print FixedPoint("1.555") 1.56 >>> print FixedPoint("1.565") 1.56 >>> print FixedPoint("1.575") 1.58 >>> # Use a "round up" algorithm ... FixedPoint.round = addHalfAndChop >>> >>> print FixedPoint("1.555") 1.56 >>> print FixedPoint("1.565") 1.57 >>> print FixedPoint("1.575") 1.58 >>> print FixedPoint("-1.555") -1.56 >>> print FixedPoint("-1.565") -1.57 >>> print FixedPoint("-1.575") -1.58 >>>
Developers may customize the FixedPoint rounding algorithm by defining a newRoundingAlgorithm
function as follows:
long
values representing normalized values of the dividend, divisor, quotient and remainder. The function returns the rounded value for the quotient.
The normalizing process insures that the sign of the result is carried in the quotient, that the remainder is greater than or equal to 0 and that the divisor is greater than 0. This normalization process results in a reduction in "sign-bit headaches." The only real "decision" that the rounding algorithm variant needs to make is whether or not to add 1 to the quotient.
For example:
>>> from fixedpoint import FixedPoint >>> def truncate(dividend, divisor, quotient, remainder): ... """Don't round: truncate""" ... return quotient ... >>> FixedPoint.round = truncate >>> print FixedPoint("1.555") 1.55 >>> print FixedPoint("1.565") 1.56 >>> print FixedPoint("1.575") 1.57 >>>
Note that changing the rounding algorithm changes the algorithm for ALL new instances of FixedPoint objects including those created as the result of operations on FixedPoint objects. It is a good practice to set the rounding algorithm right after importing FixedPoint, and to avoid changing it in the body of the program.
See About this document... for information on suggesting changes.