diff options
| author | Xavier Morel <xmo@odoo.com> | 2017-05-17 16:50:52 +0200 |
|---|---|---|
| committer | Xavier Morel <xmo@odoo.com> | 2017-07-18 11:49:28 +0200 |
| commit | ace4ee39e81a6da9e94236b5746f9ee70f8c9b95 (patch) | |
| tree | 086ce01febb306811755745f337af3014fcb9be1 /odoo | |
| parent | dc2575b5db2aaa2c56457cbd0848a02406260ff6 (diff) | |
[FIX] P3: round() compatibility shim
In Python 3 round:
* rounds half to even (banker's rounding) rather than up
* returns an int if not given a precision (or given a precision of
None)
* discards the negative sign when rounding to 0
Add a compatibility shim which implements P2 behaviour and at least
allows passing float_utils's tests.
Implementing float_round on top of Decimal was attempted but did not
work: although Decimal.quantize takes a non-integral
Decimal (e.g. ``d.quantize(D('0.001'))``) it only supports integral
powers of 10 and does not support quantizing to e.g. 0.05: while that
will not fail it will just use the exponent for quantization and 0.05
is thus equivalent to 0.01, which is not what we want if e.g. need to
round a value to 5 cents.
This is hinted at but maybe not clearly spelled out by the official
documentation:
> Return a value equal to the first operand after rounding
> and **having the exponent of** the second operand.
Also convert a bunch of hand-rolled currency rounding to just using
the round() method on res.currency.
Diffstat (limited to 'odoo')
| -rw-r--r-- | odoo/tools/float_utils.py | 2 | ||||
| -rw-r--r-- | odoo/tools/pycompat.py | 17 |
2 files changed, 18 insertions, 1 deletions
diff --git a/odoo/tools/float_utils.py b/odoo/tools/float_utils.py index 9c37da07fe9..d71576adc92 100644 --- a/odoo/tools/float_utils.py +++ b/odoo/tools/float_utils.py @@ -58,7 +58,7 @@ def float_round(value, precision_digits=None, precision_rounding=None, rounding_ epsilon = 2**(epsilon_magnitude-53) if rounding_method == 'HALF-UP': normalized_value += math.copysign(epsilon, normalized_value) - rounded_value = round(normalized_value) # round to integer + rounded_value = pycompat.round(normalized_value) # round to integer # TIE-BREAKING: UP (for ceiling operations) # When rounding the value up, we instead subtract the epsilon value diff --git a/odoo/tools/pycompat.py b/odoo/tools/pycompat.py index 7ce4f45773a..19b821863b2 100644 --- a/odoo/tools/pycompat.py +++ b/odoo/tools/pycompat.py @@ -8,6 +8,7 @@ PY2 = sys.version_info[0] == 2 if PY2: # pylint: disable=long-builtin,dict-iter-method integer_types = (int, long) + round = round keys = lambda d: iter(d.iterkeys()) values = lambda d: iter(d.itervalues()) @@ -23,8 +24,24 @@ if PY2: exec ('def reraise(tp, value, tb=None):\n raise tp, value, tb') else: + import builtins, math # pylint: disable=bad-functions integer_types = (int,) + def round(f): + # P3's builtin round differs from P2 in the following manner: + # * it rounds half to even rather than up (away from 0) + # * round(-0.) loses the sign (it returns -0 rather than 0) + # * round(x) returns an int rather than a float + # + # this compatibility shim implements Python 2's round in terms of + # Python 3's so that important rounding error under P3 can be + # trivially fixed, assuming the P2 behaviour to be debugged and + # correct. + roundf = builtins.round(f) + if builtins.round(f + 1) - roundf != 1: + return f + math.copysign(0.5, f) + # copysign ensures round(-0.) -> -0 *and* result is a float + return math.copysign(roundf, f) keys = lambda d: iter(d.keys()) values = lambda d: iter(d.values()) |
