[Python-ideas] Why no sign function?

Robert Kern robert.kern at gmail.com
Mon Apr 26 23:32:41 CEST 2010


On 4/26/10 4:21 PM, cool-RR wrote:

> I return 1 for positive, 0 for zero, -1 for negative. Yes, I know I can
> improvise something like it, by using `x > 0` or `cmp(x, 0)`, but I like
> code that reads like what it does.

That is what small utility functions are for.

Basically, there are number of different conventions a sign() function could 
choose. In floating point, there are signed zeros and NaNs. Similarly, there is 
no sign() function in the standard C math library, which the math module tries 
to wrap thinly. Since there is an important ambiguity, the standard libraries 
leave it to you to write your own small utility function which implements the 
convention you desire.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-ideas mailing list