Skip to content

Commit

Permalink
math: add clamp/3 to clamp numbers in a range
Browse files Browse the repository at this point in the history
  • Loading branch information
larpon committed Feb 10, 2024
1 parent 09d1311 commit dd38cb5
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mth/math.v
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub fn gcd[T](a T, b T) T {
})
}

// clamp clamps `num` to be between `min` and `max`.
@[inline]
pub fn clamp[T](num T, min_in_range T, max_in_range T) T {
return max[T](min_in_range, min[T](max_in_range, num))
}

// reduce reduces a fraction by finding the Greatest Common Divisor and dividing by it.
pub fn reduce_fraction[T](numerator T, denominator T) (T, T) {
g_c_d := gcd(numerator, denominator)
Expand Down

0 comments on commit dd38cb5

Please sign in to comment.