1
$\begingroup$

There's any math trick that can turn any negative number to 0? I'm not sure if this is possible without conditional logic

f(x>=0) = x  f(x<0) = 0 
  • 0
    The expression is $x \cdot 0$.2012-09-19

4 Answers 4

5

You can avoid absolute value (and therefore avoid "if") using $ f(x) = \frac{x + \sqrt{x^2}}{2}. $ I adopt the convention that $\sqrt{}$ is a function returning only the positive square root of its argument.

  • 0
    @Nick I've incorporated your remark into my solution. Thank you.2012-09-19
4

How about $f(x)=\frac{x+|x|}2$? The "if" is then hidden in the definition of absoulute value of course ...

3

It's surprisingly easy to write down the function $\max$ in terms of absolute values: $\max(x,y)=\frac{|x-y|+x+y}{2}$

So just take $\max(x,0)$.

2

Computer science, and some branches of mathematics, deal with a "truncated subtraction" operator, $\dot-$ which is exactly what you want. The value of $a\;\dot-\;b$ is defined to be the same as $a-b$ if $a$ is at least as big as $b$, and zero if it is smaller.

So you could say for example that $f(x) = x\;\dot-\;0$.

But this is just saying the same as what you said in your question, and making up a funny symbol for it. There is no reason why writing $x\;\dot-\;0$ is better than writing what you actually wrote. So just write what you wrote and don't worry about the funny symbol. No "math tricks" are needed. All that is needed is for you to say what you mean.

  • 0
    $c$ool, $n$ever heard that before...2012-09-19