Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
clamp.hpp
Go to the documentation of this file.
1/**
2 * @file clamp.hpp
3 * @brief Clamping
4 * @ingroup math
5 *
6 * One clamp for the library, for any ordered type. For floating point a NaN
7 * lands on the low bound, so a bad signal is a quiet one, not a loud one.
8 */
9
10#ifndef SBL_MATH_CLAMP_HPP_
11#define SBL_MATH_CLAMP_HPP_
12
13namespace sbl::math {
14
15/**
16 * @brief x held to [lo, hi]; a NaN goes to lo.
17 *
18 * @param x Value to clamp
19 * @param lo Low bound
20 * @param hi High bound, at or above lo
21 * @return x, lo or hi
22 */
23template<typename T>
24inline constexpr T clamp(T x, T lo, T hi) {
25 return (x > lo) ? (x < hi ? x : hi) : lo;
26}
27
28/**
29 * @brief x held to [0, 1]; a NaN goes to 0.
30 *
31 * @param x Value to clamp
32 * @return x, 0 or 1
33 */
34template<typename T>
35inline constexpr T clamp01(T x) {
36 return clamp(x, T(0), T(1));
37}
38
39} // namespace sbl::math
40
41#endif // SBL_MATH_CLAMP_HPP_
Domain-neutral math: constants, clamp, interpolation.
Definition clamp.hpp:13
constexpr T clamp(T x, T lo, T hi)
x held to [lo, hi]; a NaN goes to lo.
Definition clamp.hpp:24
constexpr T clamp01(T x)
x held to [0, 1]; a NaN goes to 0.
Definition clamp.hpp:35