1

not been on here in a while, but in this case I'm working on a blog article regarding an $\mathrm{isZero}$ function, which returns $1$ for true (the input is zero), and $0$ for false (the input is not zero). The function is based on taking advantage of the fact that:

$$\mathrm{isZero}(x) = 1 - \frac {x^2 + 1 - (|x + 1|) (|x - 1|)}{2}$$

I can programmatically test this for all values of $x$ (ADDENDUM: in the integer space), I've run all sorts of tests in Excel, JavaScript and other programming languages. I can prove it algebraically quite easily for $0$, just substitute $x = 0$ and everything reduces down perfectly to give me the $1$ that confirms the is-zero case.

However, (ADDENDUM: in the case of an integer x), where $x > 0$ or $x < 0$, when I pull graphs for $y = |x - 1|$ for $x > 0$ or $y = |x + 1|$ for $x < 0$, I find myself face to face with the inflection point where $x = 1$ for the former graph and where $x = -1$ for the latter graph, and I'm not sure how I could reduce down the $\mathrm{isZero}$ function equation based on whether I'm dealing with a positive non-zero value of $x$, or a negative non-zero value of $x$. I was wondering if anyone would know how I can prove in both cases that a non-zero value of $x$ yields a zero as a result of the above equation, thus satisfying the equation?

EDIT: I remembered that I was originally working with x being only in the integers space, thanks to 5xum. Apologies to all for having forgotten about this. I will update the main question accordingly with the necessary addenda.

Dylan Levine
  • 1,634
  • Please use MathJax to format your equations. – Gary Mar 28 '23 at 05:21
  • So $,\operatorname{isZero}\left(\frac{1}{2}\right) \ne 0,$? – dxiv Mar 28 '23 at 05:27
  • Is $x$ an integer value or float? – grand_chat Mar 28 '23 at 05:32
  • 1
    Why use an expression to test for zero? Why not simple code "if (var==0....)" – NoChance Mar 28 '23 at 05:43
  • Apologies to all, x is indeed an integer... I had forgotten that my original testing of this function was based solely on integers. – Eliseo D'Annunzio Mar 28 '23 at 10:58
  • 1
    @NoChance, this function comes from a programming language subset that has a limited set of functions, no if-then-else clauses, and statements are branchless and thus require a branchless (i.e. mathematical) approach to change specific values based on other variables. In this programming language, min() and max() are available as well as basic mathematical operators, so it's been a little tricky to work with to derive new functions based on what little ones I have available to me. – Eliseo D'Annunzio Mar 28 '23 at 11:01
  • I see, I thought that a zero test would be fundamental language capability, but I am no PL Design expert. Thanks for your explanation. – NoChance Mar 28 '23 at 16:10

1 Answers1

2

To really analyze the value of your expression, first off, you can simplify the expression a bit to only have one absolute value. Since $|a||b|=|ab|$ and $(x+1)(x-1)=x^2-1$, you can simplify your expression into

$$1-\frac{x^2+1-|x^2-1|}{2}$$

Now, you you need to split cases, but not into $x<0$ and $x>0$! You need to split the cases into regions where the values under the absolute sign are always positive or negative!

In your case, $x^2-1$ is positive if $|x|>1$, and negative if $|x|<1$. So let's split cases.

  1. If $|x|\geq 1$, then $$\begin{align} 1-\frac{x^2+1-|x^2-1|}{2} &= 1-\frac{x^2+1-(x^2-1)}{2}\\ &= 1-\frac{x^2+1-x^2+1}{2}\\ &= 1-\frac{2}{2}\\ &= 0 \end{align}$$ so your hypothesis is correct, the expression does evaluate to $0$.
  2. If $|x|< 1$, however, then $$\begin{align} 1-\frac{x^2+1-|x^2-1|}{2} &= 1-\frac{x^2+1-(-(x^2-1))}{2}\\ &= 1-\frac{x^2+1+x^2-1}{2}\\ &= 1-\frac{2x^2}{2}\\ &= 1 - x^2 \end{align}$$ so in this case, the expression is not equal to $0$.

In fact, your expression could most compactly be written as $\max\{0, 1-x^2\}$, or if you want the most explicit version, I would write it as

$$\begin{cases}1-x^2&\text{ if }-1\leq x\leq 1\\ 0&\text{ otherwise}\end{cases}.$$


Note, importantly, that so long as you evaluate the function on integer values, the function is indeed equal to $1$ for $x=0$, and it is also equal to $0$ for all other integers! This is because $0$ is the only integer that satisfies the equation $|x|<1$ and thus the only one that falls into the first case of the above analysis.

5xum
  • 123,496
  • 6
  • 128
  • 204
  • The second case should probably be $|x| \ge 1$. – Martin R Mar 28 '23 at 06:36
  • @MartinR Yes, of course, thanks for catching that one. – 5xum Mar 28 '23 at 08:44
  • @5xum, of course! I had been dealing with integers in my use cases all those years back when I started working on this problem, the focus had been only on integers, which probably explained why I was having a mental block about everything in between. Thank you for pointing it out, and especially with consolidating the |a||b| component of the equation. – Eliseo D'Annunzio Mar 28 '23 at 10:49
  • @Martin R Shouldn't it be the other way around? – LÜHECCHEgon Jun 11 '23 at 03:48
  • Note that it is even simpler if you write it as ${\rm isZero}(x) = \frac{{1 - x^2 + \left| {1 - x^2 } \right|}}{2}$. Also, in general, $\max (a,b) = \frac{{a + b + \left| {a - b} \right|}}{2}$. – Gary Jun 12 '23 at 05:41