[Python-ideas] Why no sign function?

MRAB python at mrabarnett.plus.com
Tue Apr 27 00:09:40 CEST 2010


Mark Dickinson wrote:
> On Mon, Apr 26, 2010 at 10:12 PM, cool-RR <cool-rr at cool-rr.com> wrote:
>> This was probably discussed a lot. Why is there no simple `sign` function in
>> Python, like in the math module or something? I mean one that tells you the
>> sign of a number.
> 
> What are your use-cases?  Can you give some examples of how you'd
> expect to use it?
> 
> Given math.copysign and direct comparisons like "if x > 0", I don't
> think I've ever wanted a sign function.
> 
> If a sign function were implemented, I'd probably want something like
> IEEE 754's signbit function, returning 1 for negative values and -0.0,
> and 0 for positive values and 0.0.  Ideally, something giving a
> boolean result:  hasSignBit.  (Insert better name here.)
> 
Sometimes it's called the signum function. In exists in BASIC (SGN), for
example, although I don't know what it would return for NaN (the
versions I've used didn't have NaN).



More information about the Python-ideas mailing list