diff options
author | Jason Beetham <beefers331@gmail.com> | 2021-03-04 18:27:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-04 17:27:02 -0800 |
commit | f28dc2c61ed2ee4452441c1a5508cb1e46a8f288 (patch) | |
tree | efe90c45133000032c753e272092d813a502074b | |
parent | 81889fb84ce9fb185bdbf4d4f657c80cf94b23fc (diff) | |
download | Nim-f28dc2c61ed2ee4452441c1a5508cb1e46a8f288.tar.gz |
Added assertion to clamp (#17248)
Co-authored-by: flywind <xzsflywind@gmail.com>
-rw-r--r-- | lib/pure/math.nim | 2 | ||||
-rw-r--r-- | lib/system/comparisons.nim | 7 |
2 files changed, 7 insertions, 2 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 95c57730f..4a13eefcb 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -1190,6 +1190,8 @@ func clamp*[T](val: T, bounds: Slice[T]): T {.since: (1, 5), inline.} = type A = enum a0, a1, a2, a3, a4, a5 assert a1.clamp(a2..a4) == a2 assert clamp((3, 0), (1, 0) .. (2, 9)) == (2, 9) + doAssertRaises(AssertionDefect): discard clamp(1, 3..2) # invalid bounds + assert bounds.a <= bounds.b, $(bounds.a, bounds.b) clamp(val, bounds.a, bounds.b) func lcm*[T](x: openArray[T]): T {.since: (1, 1).} = diff --git a/lib/system/comparisons.nim b/lib/system/comparisons.nim index 6c67ca493..2e5664a95 100644 --- a/lib/system/comparisons.nim +++ b/lib/system/comparisons.nim @@ -251,9 +251,12 @@ proc max*[T](x: openArray[T]): T = proc clamp*[T](x, a, b: T): T = ## Limits the value `x` within the interval [a, b]. - ## This proc is equivalent to but fatser than `max(a, min(b, x))`. + ## This proc is equivalent to but faster than `max(a, min(b, x))`. ## - ## **Note:** `a <= b` is assumed and will not be checked. + ## .. warning:: `a <= b` is assumed and will not be checked (currently). + ## + ## **See also:** + ## `math.clamp` for a version that takes a `Slice[T]` instead. runnableExamples: assert (1.4).clamp(0.0, 1.0) == 1.0 assert (0.5).clamp(0.0, 1.0) == 0.5 |