Return number if positive and zero if negative

1.6k Views Asked by At

I have the following mathematical operations to use: Add, Divide, Minimum, Minus, Modulo, Multiply and Round.

With these I need to get a number, run it through a combination of these and return 0 if the number is negative or equal to 0 and the number itself if the number is greater than 0.

Is that possible?

EDIT: Minus is Subtract

3

There are 3 best solutions below

0
On BEST ANSWER

You really want $\max\{x,0\}$, which can be realized as $\boxed{-\min\{-x,0\}}$.

In your precise language, $\operatorname{Minus}(\operatorname{Min}(\operatorname{Minus}(x),0))$.

(I assume that your "Minus" is unary minus, not "Subtract". If it really is "Subtract", you can produce unary minus by Subtract($0,x$).)

0
On

Take $x-\min(0,x)$.

If $x>0$, then $\min(0,x)=0$, so $x-\min(0,x)=x$.

If $x\le0$, then $\min(0,x)=x$, so $x-\min(0,x)=0$.

3
On

Here is a slightly different way.

What you really want is $\max(0,x)$, but you don't have the max function available. Fortunately however you do have the min function, and you can use the fact that $\max(a,b) = -\min(-a,-b)$.

So you can use $-\min(0,-x)$.