Two numbers equality formula

Given two real numbers $a$ and $b$, is there any formula which would give $1$ if $a=b$ and $0$ if not ? (I am not talking about conditional expressions). Thanks.

Edit : Everything is allowed.

• Depends on the allowed functions: $1-\mathrm{sign}|a-b|$ is one solution. – gammatester Oct 27 '15 at 13:29
• It's rather obvious that no continuous function $f(a,b)$ will do (thus a function that uses only polynomials or absolute value can't work), so there must be some hidden conditional in the definition of $f$ or a limiting process. Hence, you should clarify what is allowed and what is not. – Jean-Claude Arbaut Oct 27 '15 at 13:39
• @Weber Trigonometric functions alone can't work, as they are either continuous (sine, cosine) or not defined everyhere (tangent). In the latter case, you will end up with a function that is not defined everywhere either, thus can't work on all cases. In the former, it's continuous, whereas your function is not. Thus, either you hide the conditional (sign) either you "push the discontinuity to infinity" with a limit. – Jean-Claude Arbaut Oct 27 '15 at 13:54
• @Weber: Why do you think explicit conditionals are hurting your performance? An explicit conditional is just as much of a "math formula" as any answer you've received, many of which would be implemented with conditionals anyway. And what language are you working in? In a lot of languages, a == b is already 1 if they're equal and 0 otherwise. – user2357112 Oct 27 '15 at 20:06
• @Weber: Are you trying to get an expression that you can simplify? It's not clear what you mean by having a big chunk of your algorithm "transformed into a math function", or why that would provide any performance benefit. – user2357112 Oct 27 '15 at 20:15

(It's taken me long enough to write this ... I bet some of these appear in other answers.)

• Using the Iverson bracket: $[a=b]$
• Using the Kronecker delta (not typically used for reals, except when defining discrete distributions): $\delta_{a b}$
• Using contour integration (sneakily): $\liminf_{n \rightarrow \infty} \frac{1}{2\pi}\int_{0}^{2\pi} \mathrm{e}^{\mathrm{i}(a-b)\theta/n} \,\mathrm{d}\theta$
• Using the indicator function: $\mathbf{1}_{\{a\}}(b)$
• Using a standard not uniformly continuous (on $[0,1]$) sequence of functions: $\lim_{n \rightarrow \infty} (\min\{a/b,b/a\})^n$
• Using the Heaviside step function (in disguise, using the Fourier result that the value at the jump is the mean of the limits from each side): $\left. 4(\frac{\mathrm{d}}{\mathrm{d}x} \max\{x,0\})(1-\frac{\mathrm{d}}{\mathrm{d}x} \max\{x,0\}) \right|_{x = a-b}$
• Using the ceiling function: $1 - \lceil \frac{|a-b|}{|a-b|+1} \rceil$

Edit: Because, of course, one has to get the parity of the last one backwards. Fixed.

Edit: Added limit to contour integral because, sadly, not all pairs of reals have integer differences. And more:

• We can look at the mean of a certain cosine function: $\lim_{A \rightarrow \infty} \frac{1}{2A} \int_{-A}^A \cos((a-b)x) \,\mathrm{d}x$
• We can use the sine integral function: $1-\lim_{N \rightarrow \infty}\frac{2}{N\pi} \sum_{n=0}^N \mathrm{Si}(n \, |a-b|)$
• Iteration: $f^{[1]}(x)=\sqrt{x}$ and $f^{[n]}(x) = f^{[n-1]} \circ f^{[1]}(x)$. Compute $1-\lim_{n \rightarrow \infty} f^{[n]}(|a-b|)$.
• +1 for mentioning the Iverson bracket. Those little braces have made many an expression more efficient, more readable and generally beautiful. – JArkinstall Oct 27 '15 at 17:01
• Well done, but some of your suggestions are “conditional expressions.” – Michael Hoppe Oct 27 '15 at 19:04
• It seems to me that the contour integral one only works for integers. – Meni Rosenfeld Oct 27 '15 at 20:59
• These are all wonderful... it should be noted for OP that in terms of computation, all of these are waaaaay harder for a computer to perform than a single "==" check – ASKASK Oct 28 '15 at 0:39
• @MeniRosenfeld : D'oh. Umm... All reals are integers, right? :-) – Eric Towers Oct 28 '15 at 7:28

The classic one: $0^{|a-b|}$ ...

Edit: In case you consider $|.|$ as conditional choose $$0^\sqrt{(a-b)^2}$$ instead.

• Could this one be expressed in another form so the zero is not there any more ? – Weber Oct 27 '15 at 15:24
• @Weber : $\: (1\hspace{-0.05 in}-\hspace{-0.05 in}1)^{\hspace{.02 in}|\hspace{.02 in}a-b|} \;\;\;\;$ – user57159 Oct 27 '15 at 16:06
• Why not just $0^{(a-b)^2}$? The square root is unnecessary. @Weber – Akiva Weinberger Oct 27 '15 at 19:14
• Wait $0^0$ is 1? I thought it's undefined... – Tomáš Zato Oct 27 '15 at 19:44
• – Ypnypn Oct 27 '15 at 20:15

This one would do, if you allow limits:

$$\lim_{n\to\infty} \exp (-n|a-b|)$$

You could also compute

$$2-\mathrm{Card} \;\{a,b\}$$

Or

$$1-\int_{a-b}^{e(a-b)} \frac{\mathrm{d}x}{x}$$

but I don't think it's very clean to rely on $\int_0^0 f(x) \mathrm dx=0$ when $f$ is not defined at $x=0$.

Another limit: taking $\frac{2}{\pi}\arctan (b-a)$ you get a number in $]-1,1[$, hence you can use the limit $\lim_{n\to\infty} (1-x^2)^n$, which is $1$ if $x=0$ and $0$ if $x\in [-1,1]\backslash\{0\}$. All in all:

$$\lim_{n\to\infty} \left[1-\left(\frac{2}{\pi}\arctan (a-b)\right)^2\right]^n$$

You can use this function: $||sgn(b-a)|-1|$

How about this where $\lfloor x\rfloor$ is the floor function of $x$:

$\min\{\left\lfloor 2^x\right\rfloor, \left\lfloor 2^{-x}\right\rfloor\}$.

You can even get rid of the min function by recasting this as

$\frac{\left\lfloor 2^x\right\rfloor+ \left\lfloor 2^{-x}\right\rfloor-\mid \left\lfloor 2^x\right\rfloor- \left\lfloor 2^{-x}\right\rfloor\mid}{2}$

Edit: As ASKASK correctly pointed out (thanks), the question asked for an indicator of equality between $a$ and $b$ rather than an input of $x$ being $0$. To make the adjustment, $x$ in the post can be replaced by $a-b$.

• I think you should at least mention that we are plugging $x=a-b$ into this formula as OP seemed to request a formula in terms of $a$ and $b$ – ASKASK Oct 27 '15 at 14:56
• But otherwise, I really like this formula. To me it seems to be the cleanest. – ASKASK Oct 27 '15 at 14:57

$\left\lfloor\frac{1}{1+(a-b)^2}\right\rfloor$, where $\lfloor x\rfloor$ - floor function and $a$ and $b$ - real numbers.

$$\left|\{a\}\cap\{b\}\right|$$

Where $|\cdot|$ denotes the order (number of elements) of a set.

One can come up with many similar examples. For example, $\left|\{(a,b)\}\cap\Delta\right|$, where $\Delta=\{(x,x)\;\colon\;x\in\mathbb R\}\subset\mathbb R\times\mathbb R$.

I would use this, but it maybe it is already too conditinal for you:

The indicator function is often used in such cases, so the function takes only two values, $0,1$ in such a way that $$1_M(x)=\begin{cases}0,x\text{ is not element of } M \\1,x\text{ is element of } M \end{cases}$$ where M is any set which we choose. So in your case we would take $\{a\}:=M\subset\mathbb{R}$ and get $$1_{\{a\}}(x)=\begin{cases}0,x\text{ is not element of } \{a\} \\1,x\text{ is element of } \{a\} \end{cases}$$ meaning $1_{\{a\}}(x)$ equals only $1$ if and only if $x=a$ or in you case $1_{\{a\}}(b)=1$ with $b=a$. Although technically it actually says that it is only an element of an $1$-element set.

• As I have said in my question, I am not talking about conditional expressions. – Weber Oct 27 '15 at 14:32
• @Weber I know you did...but then I wasn't sure exactly what you by this, for example $\operatorname{sgn}(\cdot)$ or floor/ceil function are off the limits as well? – user190080 Oct 27 '15 at 14:35
• All functions are allowed, even integer ones. – Weber Oct 27 '15 at 14:41
• @Weber I got this, I just meant that the above function I gave is as conditional as $\operatorname{sgn}(\cdot)$,ceil/floor functions are - so if you are satisfied with those, you might also take the indicator function into consideration – user190080 Oct 27 '15 at 14:44

$\operatorname{sinc}(a\pi-b\pi)$ works for when $a$ and $b$ are integers.

Just this: $$\{a=b\}$$

It evaluates to 1 if the condition inside the curly brackets is true and to 0 if it isn't.

• This is the Iverson bracket; square braces $[\cdot]$ are normally used, though. – Akiva Weinberger Oct 27 '15 at 19:16
• @AkivaWeinberger Hmm, I don't know, it may be different in different countries – Maks Maisak Oct 27 '15 at 19:17
• It's not nearly old enough for that to happen. Iverson died in 2004, and Knuth is still around. (Iverson introduced it in his programming language APL, but with regular parentheses; Knuth started using it outside of programming, and changed it to square brackets to avoid ambiguity with the grouping parentheses already used in logic.) – Akiva Weinberger Oct 27 '15 at 19:22
• @AkivaWeinberger It is possible that we're talking about different things here. I originally meant this expression to be the type of the one used in here near the middle of the page, "Cost Function" section – Maks Maisak Oct 27 '15 at 19:40