0
$\begingroup$

The Python computer language has a built-in operation cmp(a,b) that returns $-1$, $0$ or $1$, if $a, $a=b$ or $a>b$, respectively. I'd like to know if there is a mathematical operation or property that supports this Python operation.

  • 2
    Sure, define a function $f_b(a)$ to have the same values as the Python procedure. It's discontinuous and piecewise defined, but that's how the Python operation behaves too.2012-08-04
  • 0
    @KevinCarlson, This type of function $f_b(a)$ has a name? I'd like to read about. I'm not familiarized with mathematical terminology.2012-08-04

2 Answers 2

3

You could do something manipulating the signum function. For instance, define your function: $\operatorname{cmp}:\mathbb{R}^{2}\to\{-1,0,1\}$, as follows:

$$\operatorname{cmp}(x,y)=\operatorname{sgn}\left(x-y\right)$$

And as pointed out by the wikipedia article, the signum function is defined as:

$$\operatorname{sgn}(x)=\begin{cases}1 & \text{ if } x\gt0 \\ 0 & \text{ if } x=0 \\ -1 & \text{ if } x\lt0\end{cases}$$

Hope this helps!

  • 0
    Thanks, @Shaktal. What I need to know is that the cmp function is a signum function.2012-08-04
  • 0
    @Marcos: Your function is not "a signum function". Shaktal showed you **the** signum function ("signum" is the _name_ of that particular function), which although related is not the same as your function -- for example, it takes one argument whereas yours takes two.2012-08-04
  • 0
    Or, in terms of the Iverson bracket, $$\mathrm{cmp}(a,b)=[a > b]-[a < b]$$2012-08-04
0

If you're willing to let $0/0=0$, then

$$\frac{a-b}{|a-b|}$$

works.

Kevin Carlson was just saying that you could simply define a function to have such a property and that would be sufficient. There's no reason to limit yourself to a single symbolic expression that covers the whole number line. Functions are more flexible than that. (I can delete this section if you want Kevin, I'd hate to steal your answer).

  • 0
    No worries, Robert!2012-08-04