Skip to content

Commit 8ac963c

Browse files
committed
-
1 parent 791fd18 commit 8ac963c

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

source_py3/python_toolbox/math_tools/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def cute_round(x, round_mode=RoundMode.CLOSEST_OR_DOWN, *, step=1):
168168
Round with a chosen step.
169169
170170
Examples:
171-
blocktododoc
171+
blocktododoc explain the different options, especially probabilistic
172172
>>> cute_round(7.456)
173173
7
174174
>>> cute_round(7.456, up=True)

source_py3/test_python_toolbox/test_math_tools/test_cute_round.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import inspect
55

6+
from python_toolbox import nifty_collections
7+
68
from python_toolbox.math_tools import cute_round, RoundMode
79

810
def almost_equals(x, y):
@@ -20,3 +22,86 @@ def test_closest_or_down(self):
2022
assert almost_equals(cute_round(7.456, step=0.1), 7.5)
2123
assert almost_equals(cute_round(7.456, step=0.2), 7.4)
2224
assert almost_equals(cute_round(7.456, step=0.01), 7.46)
25+
26+
def test_closest_or_up(self):
27+
assert almost_equals(
28+
cute_round(7.456, RoundMode.CLOSEST_OR_UP), 7
29+
)
30+
assert almost_equals(
31+
cute_round(7.654, RoundMode.CLOSEST_OR_UP), 8
32+
)
33+
assert almost_equals(
34+
cute_round(7.5, RoundMode.CLOSEST_OR_UP), 8
35+
)
36+
assert almost_equals(
37+
cute_round(7.456, RoundMode.CLOSEST_OR_UP, step=0.1), 7.5
38+
)
39+
assert almost_equals(
40+
cute_round(7.456, RoundMode.CLOSEST_OR_UP, step=0.2), 7.4
41+
)
42+
assert almost_equals(
43+
cute_round(7.456, RoundMode.CLOSEST_OR_UP, step=0.01), 7.46
44+
)
45+
46+
def test_always_up(self):
47+
assert almost_equals(
48+
cute_round(7.456, RoundMode.ALWAYS_UP), 8
49+
)
50+
assert almost_equals(
51+
cute_round(7.654, RoundMode.ALWAYS_UP), 8
52+
)
53+
assert almost_equals(
54+
cute_round(7.5, RoundMode.ALWAYS_UP), 8
55+
)
56+
assert almost_equals(
57+
cute_round(7.456, RoundMode.ALWAYS_UP, step=0.1), 7.5
58+
)
59+
assert almost_equals(
60+
cute_round(7.456, RoundMode.ALWAYS_UP, step=0.2), 7.6
61+
)
62+
assert almost_equals(
63+
cute_round(7.456, RoundMode.ALWAYS_UP, step=0.01), 7.46
64+
)
65+
66+
def test_always_down(self):
67+
assert almost_equals(
68+
cute_round(7.456, RoundMode.ALWAYS_DOWN), 7
69+
)
70+
assert almost_equals(
71+
cute_round(7.654, RoundMode.ALWAYS_DOWN), 7
72+
)
73+
assert almost_equals(
74+
cute_round(7.5, RoundMode.ALWAYS_DOWN), 7
75+
)
76+
assert almost_equals(
77+
cute_round(7.456, RoundMode.ALWAYS_DOWN, step=0.1), 7.4
78+
)
79+
assert almost_equals(
80+
cute_round(7.456, RoundMode.ALWAYS_DOWN, step=0.2), 7.4
81+
)
82+
assert almost_equals(
83+
cute_round(7.456, RoundMode.ALWAYS_DOWN, step=0.01), 7.45
84+
)
85+
86+
def test_probabilistic(self):
87+
def get_bag(*args, **kwargs):
88+
kwargs.update({'round_mode': RoundMode.PROBABILISTIC,})
89+
return nifty_collections.Bag(
90+
cute_round(*args, **kwargs) for i in range(1000)
91+
)
92+
93+
bag = get_bag(5, step=5)
94+
assert bag[5] == 1000
95+
96+
bag = get_bag(6, step=5)
97+
assert 300 <= bag[5] <= 908
98+
assert 2 <= bag[10] <= 600
99+
100+
bag = get_bag(7.5, step=5)
101+
assert 100 <= bag[5] <= 900
102+
assert 100 <= bag[10] <= 900
103+
104+
bag = get_bag(10, step=5)
105+
assert bag[10] == 1000
106+
107+

0 commit comments

Comments
 (0)