mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
math: add fn clamp (#12205)
This commit is contained in:
parent
fd3a10ab43
commit
29f068997b
@ -87,6 +87,18 @@ pub fn minmax(a f64, b f64) (f64, f64) {
|
||||
return b, a
|
||||
}
|
||||
|
||||
// clamp returns x constrained between a and b
|
||||
[inline]
|
||||
pub fn clamp(x f64, a f64, b f64) f64 {
|
||||
if x < a {
|
||||
return a
|
||||
}
|
||||
if x > b {
|
||||
return b
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// sign returns the corresponding sign -1.0, 1.0 of the provided number.
|
||||
// if n is not a number, its sign is nan too.
|
||||
[inline]
|
||||
|
@ -435,6 +435,14 @@ fn test_min() {
|
||||
}
|
||||
}
|
||||
|
||||
fn test_clamp() {
|
||||
assert clamp(2, 5, 10) == 5
|
||||
assert clamp(7, 5, 10) == 7
|
||||
assert clamp(15, 5, 10) == 10
|
||||
assert clamp(5, 5, 10) == 5
|
||||
assert clamp(10, 5, 10) == 10
|
||||
}
|
||||
|
||||
fn test_signi() {
|
||||
assert signi(inf(-1)) == -1
|
||||
assert signi(-72234878292.4586129) == -1
|
||||
|
Loading…
Reference in New Issue
Block a user