0
$\begingroup$

If I have the equation: $$y= \frac{(d-x)}{a}-1$$ and I want to limit the possible values of $y$ (between 0 and 1 in this example), would I have to use something like: $$y= max(0,min(\frac{(d-x)}{a}-1,0))$$ or if I could just add a restriction on the range of values $y$ can be by saying: $$\{y\in \Bbb{R} | max(0,min(y,0))\}$$ I would prefer to write the restriction after as it is neater and easier to read but I don't know if it makes perfect sense. When programming this I use a Clamp function to restrict the return value but I am unsure of the correct syntax when writing out the same operation mathematically.

I read here that:

Range restrictions usually occur due to the nature of the function or the relation… they are not usually imposed by the author.

I'm not 100% sure if this means that I am going about this in the wrong way or not. Which of these methods is correct and if neither, could you help me find something more appropriate?

  • 0
    Are $d, x$ and $a$ all variables?2017-02-27
  • 0
    Yes. They define the sizes of two concentric circles and a target's distance from the center of those circles. With $d$ being the inner circles radius, $a$ being the (outer circle's radius - inner circles radius) and $x$ being the target's distance from the center of the circle.2017-02-27
  • 0
    It has been programmed so as $x$ is the only variable that changes at run-time (as the target moves around inside the circles), if that is any help. $d$ and $a$ are both compile-time constants to define the sizes of the circles.2017-02-27

1 Answers 1

0

$$y = \max\left(0, \min(1,\frac{d-x}{a}-1)\right)$$

If $\frac{d-x}{a}-1\leq 0$, $$y = \max\left(0, \min(1,\frac{d-x}{a}-1)\right)=\max\left(0, \frac{d-x}{a}-1\right)=0$$

If $0 \leq \frac{d-x}{a}-1\leq 1$, $$y = \max\left(0, \min(1,\frac{d-x}{a}-1)\right)=\max\left(0, \frac{d-x}{a}-1\right)=\frac{d-x}{a}-1$$

If $ \frac{d-x}{a}-1\geq 1$, $$y = \max\left(0, \min(1,\frac{d-x}{a}-1)\right)=\max\left(0, 1\right)=1$$