Skip to content

Commit 014bd7f

Browse files
committed
Py2 round issues
1 parent 74e9486 commit 014bd7f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

plotille.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@
3535
unichr = chr
3636
unicode = str
3737
izip = zip
38+
roundeven = round
3839
else:
3940
from itertools import izip
4041

42+
def roundeven(x):
43+
x_r = round(x)
44+
if abs(x_r - x) == 0.5:
45+
return 2.0 * round(x / 2)
46+
return x_r
47+
4148

4249
def hist(X, bins=40, width=80, log_scale=False, linesep=os.linesep): # noqa: N803
4350
'''Create histogram over `X` from left to right
@@ -330,10 +337,10 @@ def ymax(self):
330337
return self._ymax
331338

332339
def _transform_x(self, x):
333-
return int(round((x - self.xmin) / self._x_delta_pt))
340+
return int(roundeven((x - self.xmin) / self._x_delta_pt))
334341

335342
def _transform_y(self, y):
336-
return int(round((y - self.ymin) / self._y_delta_pt))
343+
return int(roundeven((y - self.ymin) / self._y_delta_pt))
337344

338345
def _set(self, x_idx, y_idx, set_=True):
339346
'''Put a dot into the canvas at (x_idx, y_idx) [canvas coordinate system]
@@ -419,8 +426,8 @@ def line(self, x0, y0, x1, y1, set_=True):
419426
y_diff = y1_idx - y0_idx
420427
steps = max(abs(x_diff), abs(y_diff))
421428
for i in range(1, steps):
422-
xb = x0_idx + int(round(x_diff / steps * i))
423-
yb = y0_idx + int(round(y_diff / steps * i))
429+
xb = x0_idx + int(roundeven(x_diff / steps * i))
430+
yb = y0_idx + int(roundeven(y_diff / steps * i))
424431
self._set(xb, yb, set_)
425432

426433
def rect(self, xmin, ymin, xmax, ymax, set_=True):

0 commit comments

Comments
 (0)