Given:
let a = 4.2
let b = -1.3
let c = 6.4
I want to know the simplest, Swiftiest way to clamp these values to a given range, say 0...5, such that:
a -> 4.2
b -> 0
c -> 5
I know I can do the following:
let clamped = min(max(a, 0), 5)
Or something like:
let clamped = (a < 0) ? 0 : ((a > 5) ? 5 : a)
But I was wondering if there were any other ways to do this in Swift—in particular, I want to know (and document on SO, since there doesn't appear to be a question about clamping numbers in Swift) whether there is anything in the Swift standard library intended specifically for this purpose.
There may not be, and if so, that's also an answer I'll happily accept.