summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Dony <odo@openerp.com>2011-12-19 11:57:18 +0100
committerOlivier Dony <odo@openerp.com>2011-12-19 11:57:18 +0100
commitdcd1c1daff6e27b26c76cb222fc8ca05b98aaae4 (patch)
treec8a00671aa52934cd69d10711b24b5e3349e1c3b
parent9ddd05fb3ae83a6517b667009d34ab1826582cb7 (diff)
[IMP] float_utils: better variable names, explain /epsilon/ usage
bzr revid: odo@openerp.com-20111219105718-0jbz01rirl0s64ca
-rw-r--r--openerp/tools/float_utils.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/openerp/tools/float_utils.py b/openerp/tools/float_utils.py
index dde6aa5962c..effd473e269 100644
--- a/openerp/tools/float_utils.py
+++ b/openerp/tools/float_utils.py
@@ -58,9 +58,11 @@ def float_round(value, precision_digits=None, precision_rounding=None):
def float_is_zero(value, precision_digits=None, precision_rounding=None):
"""Returns true if ``value`` is small enough to be treated as
- zero at the given precision.
+ zero at the given precision (smaller than the given *epsilon*).
Precision must be given by ``precision_digits`` or ``precision_rounding``,
- not both!
+ not both! Here the precision (``10**-precision_digits`` or
+ ``precision_rounding``) is used as the zero *epsilon*: values smaller
+ than that are considered to be zero.
Warning: ``float_is_zero(value1-value2)`` is not always equivalent to
``float_compare(value1,value2) == 0``, as the former will round after
@@ -74,25 +76,28 @@ def float_is_zero(value, precision_digits=None, precision_rounding=None):
:param float value: value to compare with currency's zero
:return: True if ``value`` is considered 0
"""
- rounding_factor = _float_check_precision(precision_digits=precision_digits,
+ epsilon = _float_check_precision(precision_digits=precision_digits,
precision_rounding=precision_rounding)
- return abs(float_round(value, precision_rounding=rounding_factor)) < rounding_factor
+ return abs(float_round(value, precision_rounding=epsilon)) < epsilon
def float_compare(value1, value2, precision_digits=None, precision_rounding=None):
"""Compare ``value1`` and ``value2`` after rounding them according to the
given precision. A value is considered lower/greater than another value
if their rounded value is different. This is not the same as having a
non-zero difference!
+ Precision must be given by ``precision_digits`` or ``precision_rounding``,
+ not both!
- For example 1.432 and 1.431 are equal at 2 digits precision,
+ Example: 1.432 and 1.431 are equal at 2 digits precision,
so this method would return 0
- However 0.006 and 0.002 are considered different (returns 1) because
- they respectively round to 0.01 and 0.0, even though
- 0.006-0.002 = 0.004 which would be considered zero at 2 digits precision.
-
+ However 0.006 and 0.002 are considered different (method returns 1) because
+ they respectively round to 0.01 and 0.0, even though 0.006-0.002 = 0.004
+ which would be considered zero at 2 digits precision.
- Precision must be given by ``precision_digits`` or ``precision_rounding``,
- not both!
+ Warning: ``float_is_zero(value1-value2)`` is not always equivalent to
+ ``float_compare(value1,value2) == 0``, as the former will round after
+ computing the difference, while the latter will round before, giving
+ different results for e.g. 0.006 and 0.002 at 2 digits precision.
:param int precision_digits: number of decimal digits to round to.
:param float precision_rounding: decimal number representing the minimum