0

I have an X value that can go from 0.0000001 to infinite.
I want, if this value is less than 0.8, get 0.8
And if that value is equal to or more than 0.8, get 0

How would you do that using only min and max functions (and of course basic +-/x operations) ?

I ask this because I have to calculate this inside a custom piece of software that does not accept/interpret the IF statement, and for the purpose of the asked problem, supports MIN and MAX functions.

Oliver
  • 111
  • 4
  • 2
    this looks like a homework assignment, is it? – Thijs Steel Apr 11 '20 at 21:02
  • @ThijsSteel: no, just a headache since hours I'm searching how to do that – Oliver Apr 11 '20 at 21:09
  • How about combining the theta function and writing the absolute value in terms of min and max? – Maxim Umansky Apr 12 '20 at 00:09
  • 3
    If it's not a homework, it's easy to do it like this: if X < 0.8: return 0.8 else: return 0 and I don't see why you're searching for a supposed to be "more elegant way" to this with only min and max functions. – Mithridates the Great Apr 13 '20 at 03:10
  • 4
    Or, put more directly: what is your reason to use only min and max? People who ask questions like this one often have a problem that can be solved in a better way if they specify a little more about their true task at hand (see "XY problem"). – Federico Poloni Apr 13 '20 at 09:19
  • @AloneProgrammer: It's because I have to use it with a custom piece of software that does not accept/interpret the IF statement, and for the purpose of the asked problem, supports MIN and MAX functions – Oliver Apr 14 '20 at 08:31

2 Answers2

3

The trick is noticing that $|x| = \max(x, 0) - \min(x,0)$. With that, it's easy to get to the function you asked for, $$f(x) = \mbox{0.4}\left(1-\dfrac{x-0.8}{\max(x-0.8,0)-\min(x-0.8,0)}\right)$$

Amit Hochman
  • 1,081
  • 5
  • 6
  • Great and smart ! But, if I use anything lower than 0.8, it's ok, but if I use anything greater or equal than 0.8, this formula leads to a division error : f(x)=0.8/2(1-(calc/calc)) = 0.8/20 = 0.8/0. Do you see what's missing in the formula ? – Oliver Apr 12 '20 at 14:48
  • I edited the formula; the formatting was a bit unclear. The term in parenthesis is not in the denominator. For $x=0.8$ you do get 0/0, but you didn’t specify what the value should be in that case. – Amit Hochman Apr 12 '20 at 17:27
  • Right, I update the question. If x=0.8, the value should be 0 – Oliver Apr 12 '20 at 23:10
1

I have kind of an answer, but it might not be useful depending on what you want to use it for.

Let $t$ be some large parameter, $\text{min}(\text{max}(t*(0.8-x),0),0.8)$ will approximate the function you seek very well. It will only be incorrect for values that are very close to $0.8$, and we can make the approximation arbitrarily accurate by increasing t.

Thijs Steel
  • 1,693
  • 8
  • 16