summaryrefslogtreecommitdiff
path: root/openerp/tools/float_utils.py
diff options
context:
space:
mode:
authorMartin Trigaux <mat@openerp.com>2014-10-08 16:10:52 +0200
committerOlivier Dony <odo@openerp.com>2014-10-22 14:28:22 +0200
commit7705f883d23b71cdb1d30300ce69d9df665ae55e (patch)
tree7c867319ce47a3effca041e1dbf41eee0b0f6276 /openerp/tools/float_utils.py
parent354b82bee050ed29dd96adad613cac79ca778104 (diff)
[FIX] base: support float rounding with rounding_method=UP (ceiling)
Add rounding_method parameter on float_round method to offer HALF-UP (default, usual round) or UP (ceiling) rounding method. Use the second method instead of math.ceil() for product reservations. For UP, the python math.ceil() method uses "torwards infinity" rounding method while we want "away from zero". Therefore we use the absolute value of normalized_value to make sure than -1.8 is rounded to -2.0 and not -1. Fixes #1125 #2793 This is a cherry-pick of d4972ff which was reverted at 333852e due to remaining issue with negative values.
Diffstat (limited to 'openerp/tools/float_utils.py')
-rw-r--r--openerp/tools/float_utils.py33
1 files changed, 26 insertions, 7 deletions
diff --git a/openerp/tools/float_utils.py b/openerp/tools/float_utils.py
index 5c934114e3c..9f7b49974f4 100644
--- a/openerp/tools/float_utils.py
+++ b/openerp/tools/float_utils.py
@@ -29,10 +29,11 @@ def _float_check_precision(precision_digits=None, precision_rounding=None):
return 10 ** -precision_digits
return precision_rounding
-def float_round(value, precision_digits=None, precision_rounding=None):
- """Return ``value`` rounded to ``precision_digits``
- decimal digits, minimizing IEEE-754 floating point representation
- errors, and applying HALF-UP (away from zero) tie-breaking rule.
+def float_round(value, precision_digits=None, precision_rounding=None, rounding_method='HALF-UP'):
+ """Return ``value`` rounded to ``precision_digits`` decimal digits,
+ minimizing IEEE-754 floating point representation errors, and applying
+ the tie-breaking rule selected with ``rounding_method``, by default
+ HALF-UP (away from zero).
Precision must be given by ``precision_digits`` or ``precision_rounding``,
not both!
@@ -41,6 +42,9 @@ def float_round(value, precision_digits=None, precision_rounding=None):
:param float precision_rounding: decimal number representing the minimum
non-zero value at the desired precision (for example, 0.01 for a
2-digit precision).
+ :param rounding_method: the rounding method used: 'HALF-UP' or 'UP', the first
+ one rounding up to the closest number with the rule that number>=0.5 is
+ rounded up to 1, and the latest one always rounding up.
:return: rounded float
"""
rounding_factor = _float_check_precision(precision_digits=precision_digits,
@@ -52,7 +56,7 @@ def float_round(value, precision_digits=None, precision_rounding=None):
# we normalize the value before rounding it as an integer, and de-normalize
# after rounding: e.g. float_round(1.3, precision_rounding=.5) == 1.5
- # TIE-BREAKING: HALF-UP
+ # TIE-BREAKING: HALF-UP (for normal rounding)
# We want to apply HALF-UP tie-breaking rules, i.e. 0.5 rounds away from 0.
# Due to IEE754 float/double representation limits, the approximation of the
# real value may be slightly below the tie limit, resulting in an error of
@@ -66,8 +70,23 @@ def float_round(value, precision_digits=None, precision_rounding=None):
normalized_value = value / rounding_factor # normalize
epsilon_magnitude = math.log(abs(normalized_value), 2)
epsilon = 2**(epsilon_magnitude-53)
- normalized_value += cmp(normalized_value,0) * epsilon
- rounded_value = round(normalized_value) # round to integer
+ if rounding_method == 'HALF-UP':
+ normalized_value += cmp(normalized_value,0) * epsilon
+ rounded_value = round(normalized_value) # round to integer
+
+ # TIE-BREAKING: UP (for ceiling operations)
+ # When rounding the value up, we instead subtract the epsilon value
+ # as the the approximation of the real value may be slightly *above* the
+ # tie limit, this would result in incorrectly rounding up to the next number
+ # The math.ceil operation is applied on the absolute value in order to
+ # round "away from zero" and not "towards infinity", then the sign is
+ # restored.
+
+ elif rounding_method == 'UP':
+ sign = cmp(normalized_value, 0)
+ normalized_value -= sign*epsilon
+ rounded_value = math.ceil(abs(normalized_value))*sign # ceil to integer
+
result = rounded_value * rounding_factor # de-normalize
return result