# Approximating a clamp function using only addition, multiplication, division and subtraction

I'm trying to construct a function which satisfies the following:

\begin{align} f(x) = 0 & \qquad x \leq 0\\ f(x) = x & \qquad 0 \lt x \lt 1\\ f(x) = 1 & \qquad x \geq 1\\ \end{align}

Sadly I only have the very basic operators at my disposal: + - / *. I also have grouping operators ( ).

I've managed to come close using the Butterworth function:

$$f(x) = \frac{x}{1+\frac{2x-1}{1}+\frac{2x-1}{1}+\frac{2x-1}{1}+…}$$

This (approximately) satisfies my first two conditions, but not the third one. I'm sure there's a way to modify this function to satisfy all three, but I don't have the skills.

EDIT:

I'd like to be able to handle the range of -600 < x < 120. For accuracy, I don't have a definite target. Perhaps to start with if f(x) could be <0.01 when x < -0.01 and f(x) could be >0.99 when x > 1.01? I know that higher accuracy will result in a longer expression, so if I understand how to build the expression I can experiment to find an appropriate balance between brevity & accuracy.

If it makes your answer easier to read, feel free to include exponentiation by a constant value. I don't have access to any exponential operations, but I can easily convert them into repeated multiplication myself as long as the exponent is constant.

• The intuitive properties you would want would be encapsulated by $h(x)=x-xf(x)-(x-1)g(x)$ where $\lim_{x \to -\infty} f(x)=1,\lim_{x \to \infty} f(x)=0,\lim_{x \to -\infty} g(x)=0,\lim_{x \to \infty} g(x)=1$. The problem is that you can't build $f,g$ with those properties out of rational functions: rational functions that have a horizontal asymptote have the same horizontal asymptote on both ends. That's going to restrict you to a bounded interval, and we can't really give an answer for a bounded interval without knowing what that interval is.
– Ian
Sep 16, 2019 at 14:05
• (Cont.) If you had just exponential functions in addition, then we could do something.
– Ian
Sep 16, 2019 at 14:06
• It's implicit in your question that your programming environment/language does not allow you to use an if else construction, packaged inside a function call. If that's so, please edit the question to let us know. It would also help to know the range of input values and how good an approximation you need. Sep 16, 2019 at 14:22
• Please edit the question to provide an approximate range of input values and how good an approximation to the output you need. (Do that with an edit, not in a comment.) Sep 16, 2019 at 14:35
• On a range as wide as that you are going to be stuck with some rather large rational function expressions, but it can technically be done, look into Pade approximation. Most likely the effect of the middle region on an approximant of this sort is going to be negligible; on a range that large your function looks basically like an actual step function.
– Ian
Sep 16, 2019 at 14:41

Your function can be approximated through two shifted ramp functions, as \eqalign{ & f(x) = x\,H(x) - \left( {x - 1} \right)\,H(x - 1) \approx \cr & \approx {x \over 2}\left( {1 + {x \over {\sqrt {x^{\,2} + \varepsilon ^{\,2} } }}} \right) - {{\left( {x - 1} \right)} \over 2}\left( {1 + {{\left( {x - 1} \right)} \over {\sqrt {{\left( {x - 1} \right)}^{\,2} + \varepsilon ^{\,2} } }}} \right) \cr} where
$$H(x)$$ denotes the Heaviside step function;
$$\varepsilon <<1$$ is a small value.

Concerning the square root, you might calculate itrecursively through the famous Babylonian Method

This is an example of what you get with $$\varepsilon = 0.1$$.

It is clear that operating around the symmetric function $$x-1/2 \to y-1/2$$ you can reduce the computations almost in half.

• I think within the context of the question you would really need to pick a given number of iterations to do for a given bounded interval. Also your $H(x-1)$ was not substituted into correctly.
– Ian
Sep 18, 2019 at 1:20
• @Ian: thanks for signalling the typo. Concerning the the number of iterations, the Babylonian method has quadratic convergence, and OP is telling that knowing the approach he can experiment himself the best tradeoff precision/n. iterations. Sep 19, 2019 at 8:52
• I ended up approaching my problem a different way, but this is an awesome answer. Oct 2, 2019 at 7:58

I would comment if I only were privileged.

Have in mind that the function you want to construct is only piecewise analytic. Maybe you can obtain this function as the limit of some sequence of functions, which is presumably the exercise here, given the question tags.

I suspect that there won't be any closed series representing the given function.

• If you were allowed to use the Heaviside function $H(x)$, which is not analytic at the origin, you could represent your function exactly by $$f(x) = H(x)H(1-x) x + H(x-1).$$ You would then obtain a mentioned analytic approximation of $f(x)$ by inserting into the above expression any analytic approximation of $H(x)$, f.i. the logistic function $$H_a(x) = \frac{1}{1+\exp(-ax)}.$$ You see that $$H(x) = \lim_{a\rightarrow\infty}H_a(x).$$ Hence, $$f_a(x)=H_a(x)H_a(1-x) x+H_a(x-1)$$ gives you an analytic approximation of $f(x)$ fulfilling $$f(x)=\lim_{a \rightarrow \infty} f_a(x).$$ Sep 18, 2019 at 8:57