Code example for clamp
This commit is contained in:
parent
2a470c4217
commit
fde53bffd7
1 changed files with 5 additions and 1 deletions
|
|
@ -1508,6 +1508,10 @@ proc max*(x, y: float): float {.magic: "MaxF64", noSideEffect.} =
|
||||||
|
|
||||||
proc clamp*[T](x, a, b: T): T =
|
proc clamp*[T](x, a, b: T): T =
|
||||||
## limits the value ``x`` within the interval [a, b]
|
## limits the value ``x`` within the interval [a, b]
|
||||||
|
##
|
||||||
|
## .. code-block:: Nimrod
|
||||||
|
## assert((1.4).clamp(0.0, 1.0) == 1.0)
|
||||||
|
## assert((0.5).clamp(0.0, 1.0) == 0.5)
|
||||||
if x < a: return a
|
if x < a: return a
|
||||||
if x > b: return b
|
if x > b: return b
|
||||||
return x
|
return x
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue