1
$\begingroup$

While programming I came up with a the following equation / workflow

clampValue = (value - minValueofRange) / (maxValueofRange - minValueofRange);
clippedValue = min(1, max(0, clampValue));
finalValue = clippedValue * scale;

Now the clipping ultimately causes an if and else in programming. Can we convert it completely to a mathematical equation?

Edit:

max and min are functions in c++ defined like:

int max(int v1, int v2){
    if(v1 > v2) return v1;
    else return v2;
}

Similar for the min() function

$\endgroup$
7
  • $\begingroup$ How are those not mathematical equations? $\endgroup$
    – Ennar
    Commented Jul 26, 2017 at 10:51
  • $\begingroup$ As per my understanding this is not clippedValue = min(1, max(0, clampValue)); $\endgroup$ Commented Jul 26, 2017 at 10:52
  • 2
    $\begingroup$ I'm not sure I understand the code. If the variable max is greater than the variable min, and the variable value is always at least min, then clampValue will always be at least $0$. So when you apply the max function you should get that max(0, clampValue) is always equal to clampValue. And if value is never bigger than max, then clampValue is never bigger than $1$, so when you apply the min function you get that clippedValue is always clampValue. So I don't understand what clippedValue is doing. $\endgroup$
    – Mark S.
    Commented Jul 26, 2017 at 10:53
  • 1
    $\begingroup$ Value can be lower than the min and max Values. I'll update the code $\endgroup$ Commented Jul 26, 2017 at 10:55
  • 2
    $\begingroup$ Basit, I don't understand what you mean, I'm afraid. To what language do you think $\max$ and $\min$ belong to? These are mathematical constructs. $\endgroup$
    – Ennar
    Commented Jul 26, 2017 at 10:55

5 Answers 5

4
$\begingroup$

Using a piecewise function as in T. Linnell's answer or simply writing something like $\min(1,\max(0,c))$ would be clear and absolutely fine in mathematics.

If you just happen to be curious about how to represent $\min$ and $\max$ in a different way, $\max(a,b)=\dfrac{|a-b|+a+b}{2}$ and $\min(a,b)=\dfrac{a+b-|a-b|}{2}$.

$\endgroup$
2
$\begingroup$

If you want a functional expansion of the min/max function, use

$$ {\rm MinMax}(0,1,x) = \frac{ |x| - |x+|x|-2|}{4} + \frac{x+2}{4} $$

Or more generally

$$ {\rm MinMax}(x_{min},x_{max},x) = \frac{ |x-x_{min}| - | x+x_{min} + |x-x_{min}|-2 x_{max} |}{4} + \frac{x+2 x_{max}+x_{min}}{4} $$

$\endgroup$
2
$\begingroup$

I assume that you are after a branchless expression (otherwise you can stick to the $\min/\max$ definition).

I don't think there is anything in the floating-point mathematical functions that can help you. There are indeed built-in functions such as $\min$, $\max$ and $\text{abs}$ that you can use, but you don't know if they are implemented in a branchless way.

If you are looking for "by all means" optimization, you can hack into the floating-point representation and play with the sign bit, which is the most significant one.

For instance, in C, using a union with { int i; float f; }, the expression ~(i >> 31) & i implements the clamping of the negatives, $\max(0, f)$.

$\endgroup$
1
$\begingroup$

Mathematically, you're after a piecewise function:

$$f(x;a,b) = \begin{cases}0 & x < a\\ \frac{x-a}{b-a} &a\le x \le b\\ 1 &x > b\end{cases}$$

You can then multiply by whatever scalar factor you desire. I don't think there's a simpler way to do it in programming code than how you have. Ultimately, you need two comparisons to achieve the desired behaviour, which your code effectively has in the max and min functions.

$\endgroup$
1
  • 1
    $\begingroup$ Something like this would probably be the most common way of writing this in mathematics. $\endgroup$
    – Mark S.
    Commented Jul 26, 2017 at 11:02
0
$\begingroup$

In sum you can display clipped value as:

$$ clamp = \frac {value - min(A)}{max(A) - min(A)} $$

$$ max = \frac {\lvert clamp \rvert + clamp}{2} $$

$$ min = \frac {\lvert 1 - max \rvert + 1 + max}{2} $$

$$ clip = min; \ final =clip*scale $$

$$ final = \frac {\lvert 1 - \frac {\lvert \frac {value - min(A)}{max(A) - min(A)} \rvert + \frac {value - min(A)}{max(A) - min(A)}}{2} \rvert + 1 + \frac {\lvert \frac {value - min(A)}{max(A) - min(A)} \rvert + \frac {value - min(A)}{max(A) - min(A)}}{2}}{2} * scale $$

or:

$$ scale * \frac{1}{4} * \Biggl(\frac{x}{a - b}- \sqrt{\biggl(\frac{b - x}{a - b} - \frac {\sqrt{ (b - x) ^2}}{\sqrt{(a - b)^2}} + 2\biggr)^2} + \frac {\sqrt{(b - x)^2}}{\sqrt{(a - b)^2}} - \frac {b}{a - b} + 2 \Biggr) $$

$with\ a = max(A), \ b = min(A)\ and\ A=Range$

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .