Skip to content

Commit 92edbc3

Browse files
committed
-
1 parent 95c17db commit 92edbc3

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

source_py3/python_toolbox/math_tools/misc.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
import numbers
55
import math
6+
import random
7+
8+
from python_toolbox import nifty_collections
69

710

811
infinity = float('inf')
@@ -153,12 +156,19 @@ def is_integer(x):
153156
return inted_x == x
154157

155158

156-
def cute_round(x, step=1, up=False):
159+
class RoundMode(nifty_collections.CuteEnum):
160+
CLOSEST_OR_DOWN = 0
161+
CLOSEST_OR_UP = 1
162+
ALWAYS_DOWN = 2
163+
ALWAYS_UP = 3
164+
PROBABILISTIC = 4
165+
166+
def cute_round(x, round_mode=RoundMode.CLOSEST_OR_DOWN, *, step=1):
157167
'''
158168
Round with a chosen step.
159169
160170
Examples:
161-
171+
blocktododoc
162172
>>> cute_round(7.456)
163173
7
164174
>>> cute_round(7.456, up=True)
@@ -178,6 +188,17 @@ def cute_round(x, step=1, up=False):
178188
179189
'''
180190
div, mod = divmod(x, step)
181-
return (div + bool(mod and up)) * step
191+
if round_mode == RoundMode.CLOSEST_OR_DOWN:
192+
round_up = (mod > 0.5 * step)
193+
elif round_mode == RoundMode.CLOSEST_OR_UP:
194+
round_up = (mod >= 0.5 * step)
195+
elif round_mode == RoundMode.ALWAYS_DOWN:
196+
round_up = False
197+
elif round_mode == RoundMode.ALWAYS_UP:
198+
round_up = True
199+
else:
200+
assert round_mode == RoundMode.PROBABILISTIC
201+
round_up = random.random() < mod / step
202+
return (div + round_up) * step
182203

183204

source_py3/python_toolbox/math_tools/statistics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def get_mean(iterable):
2929
sum_ += value
3030
return sum_ / (i + 1)
3131

32-
32+
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
# Copyright 2009-2015 Ram Rachum.
22
# This program is distributed under the MIT license.
33

4-
from python_toolbox.math_tools import cute_round
4+
import inspect
5+
6+
from python_toolbox.math_tools import cute_round, RoundMode
57

68
def almost_equals(x, y):
79
return (abs(1-(x / y)) < (10 ** -10))
810

911

10-
def test_cute_round():
11-
assert almost_equals(cute_round(7.456), 7)
12-
assert almost_equals(cute_round(7.456, up=True), 8)
13-
assert almost_equals(cute_round(7.456, step=0.1), 7.4)
14-
assert almost_equals(cute_round(7.456, step=0.1, up=True), 7.5)
15-
assert almost_equals(cute_round(7.456, step=0.2), 7.4)
16-
assert almost_equals(cute_round(7.456, step=0.2, up=True), 7.6)
17-
assert almost_equals(cute_round(7.456, step=0.01), 7.45)
18-
assert almost_equals(cute_round(7.456, step=0.01, up=True), 7.46)
12+
class CuteRoundTestCase:
13+
def test_closest_or_down(self):
14+
full_arg_spec = inspect.getfullargspec(cute_round)
15+
assert RoundMode.CLOSEST_OR_DOWN in full_arg_spec.defaults
16+
17+
assert almost_equals(cute_round(7.456), 7)
18+
assert almost_equals(cute_round(7.654), 8)
19+
assert almost_equals(cute_round(7.5), 7)
20+
assert almost_equals(cute_round(7.456, step=0.1), 7.5)
21+
assert almost_equals(cute_round(7.456, step=0.2), 7.4)
22+
assert almost_equals(cute_round(7.456, step=0.01), 7.46)

0 commit comments

Comments
 (0)