From e6512edb98e8b80c6a793986aea75afda406ad1a Mon Sep 17 00:00:00 2001 From: Sascha Peilicke Date: Thu, 8 Aug 2013 18:06:10 +0200 Subject: [PATCH] Add utility away_from_zero_round and use it for LESS round() Also _ophsl should use it when running unter Py3k (instead of convergent_round). --- lesscpy/lessc/color.py | 2 +- lesscpy/lessc/utility.py | 13 +++++++++++++ lesscpy/plib/call.py | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lesscpy/lessc/color.py b/lesscpy/lessc/color.py index 1a392ed..cde21b3 100644 --- a/lesscpy/lessc/color.py +++ b/lesscpy/lessc/color.py @@ -364,5 +364,5 @@ class Color(): hls = list(self._hextohls(color)) hls[idx] = self._clamp(getattr(hls[idx], op)(diff / 100.0)) rgb = colorsys.hls_to_rgb(*hls) - color = (utility.convergent_round(c * 255) for c in rgb) + color = (utility.away_from_zero_round(c * 255) for c in rgb) return self._rgbatohex(color) diff --git a/lesscpy/lessc/utility.py b/lesscpy/lessc/utility.py index 73f5361..02b1d07 100644 --- a/lesscpy/lessc/utility.py +++ b/lesscpy/lessc/utility.py @@ -13,6 +13,7 @@ from __future__ import print_function import collections import math import re +import sys def flatten(lst): @@ -246,6 +247,18 @@ def split_unit(value): return r.groups() if r else ('', '') +def away_from_zero_round(value, ndigits=0): + """Round half-way away from zero. + + Python2's round() method. + """ + if sys.version_info[0] >= 3: + p = 10 ** ndigits + return float(math.floor((value * p) + math.copysign(0.5, value))) / p + else: + return round(value, ndigits) + + def convergent_round(value, ndigits=0): """Convergent rounding. diff --git a/lesscpy/plib/call.py b/lesscpy/plib/call.py index d2fe8c7..2b8bbcb 100644 --- a/lesscpy/plib/call.py +++ b/lesscpy/plib/call.py @@ -196,7 +196,7 @@ class Call(Node): str """ n, u = utility.analyze_number(value) - return utility.with_unit(int(round(float(n))), u) + return utility.with_unit(int(utility.away_from_zero_round(float(n))), u) def ceil(self, value, *args): """ Ceil number