Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

share|improve this question
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. –  Kevin Carlson Aug 4 '12 at 1:05
    
@KevinCarlson, This type of function $f_b(a)$ has a name? I'd like to read about. I'm not familiarized with mathematical terminology. –  Marcos da Silva Sampaio Aug 4 '12 at 1:10

2 Answers 2

up vote 3 down vote accepted

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!

share|improve this answer
    
Thanks, @Shaktal. What I need to know is that the cmp function is a signum function. –  Marcos da Silva Sampaio Aug 4 '12 at 1:16
    
@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. –  Henning Makholm Aug 4 '12 at 1:21
    
Or, in terms of the Iverson bracket, $$\mathrm{cmp}(a,b)=[a > b]-[a < b]$$ –  J. M. Aug 4 '12 at 3:30

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).

share|improve this answer
    
No worries, Robert! –  Kevin Carlson Aug 4 '12 at 2:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.