# 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

I apologize for the late response. I recently came across this post and as it so happens, I have been experimenting with such functions recently and figured I would add my two-cents.

For my current research, I needed a smooth(ish) approximation to the Heaviside step function defined on a compact interval. I really wanted to use a similar approach to the one discussed in chapter 13 of the book "An Introduction to Manifolds" by Loring W. Tu. This approach uses the function $$f(x)=\left\{\begin{array}{cc}e^{-\frac{1}{x}},&x>0,\\0,&x\le0\end{array}\right.$$ to construct a $$C^\infty$$ bump function. When I tried implementing this numerically I ran into some problems due to the very fast nature of how this function approaches $$0$$ (overflow errors, division by zero, etc. ).

This led me to an approach that is extremely similar to that used in the book but instead of using the function $$e^{-\frac{1}{x}}$$, I needed to find a function with the same basic ''shape'' and behavior as the exponential but which was simpler to compute and used only addition/subtraction, multiplication/division (and was perhaps not $$C^\infty$$ but just $$C^1$$ or $$C^2$$). I thought for a while and finally decided to use the function

$$f(x)=\left\{\begin{array}{cc}1-\frac{1}{1+x^p},&x>0,\\0,&x\le0\end{array}\right.$$ where $$p$$ is a positive integer greater than or equal to 2.

To construct the Heaviside step function approximation from this you can simply follow a slightly altered version of the procedure in the book by Tu

Define $$g(x)=\frac{f(x)}{f(x)+f(1-x)}$$ and let $$\epsilon$$ be the desired width of the smoothed interval around the corner point (in the plots below I use $$\epsilon=.1$$)

The approximation to the Heaviside-step function is then given by $$H_\epsilon(x)=g\left(\frac{x-\frac{\epsilon}{2}}{\epsilon}\right)$$

Here is a comparison what Tu's Heaviside step function (red) and my Heaviside step function (purple) approximation look like for $$p=2$$ They look very similar and get sharper as you increase the value of $$p$$. As an added benefit, the function $$f$$ gains more degrees of smoothness as $$p$$ is increased.

When I apply this approximation to your desired function $$x(H(x)-H(x-1))+H(x-1)$$ for $$\epsilon=.1$$ and $$p=4$$ the result is visually indistinguishable from the desired result. Actual function (blue), My approximation (red) I hope this helps you out! Please let me know if you have any questions regarding my approach 🙂

• This looks very impressive! I ended up avoiding the problem. The reason I wanted the function was to polyfill the clamp() CSS function, which at the time was not available in any browsers. I scrapped the idea due to complexity and performance concerns. In the end I cheated by using JavaScript instead of pure CSS. Jun 10, 2022 at 13:25