Skip to content

Commit e535518

Browse files
Call constrain from map_range.
Add tests for out-of-range reversed order map_range.
1 parent 92036d1 commit e535518

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

adafruit_simplemath.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ def map_range(
5656
"""
5757

5858
mapped = unconstrained_map_range(x, in_min, in_max, out_min, out_max)
59-
60-
if out_min <= out_max:
61-
return max(min(mapped, out_max), out_min)
62-
return min(max(mapped, out_max), out_min)
59+
return constrain(mapped, out_min, out_max)
6360

6461

6562
def unconstrained_map_range(

tests/map_range_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ def test_map_range():
1313
assert map_range(1, 10, 0, 0, 5) == 4.5
1414
assert map_range(1, 0, 10, 10, 0) == 9.0
1515
assert map_range(10, 1, 10, 1, 20) == 20.0
16+
# Tests for out-of-range descending output order
17+
assert map_range(11, 1, 10, 20, 1) == 1.0
18+
assert map_range(-1, 1, 10, 20, 1) == 20.0

0 commit comments

Comments
 (0)