Why doesn't Python have a sign function?

前端 未结 12 714
旧巷少年郎
旧巷少年郎 2020-12-02 04:54

I can\'t understand why Python doesn\'t have a sign function. It has an abs builtin (which I consider sign\'s sister), but no si

12条回答
  •  感动是毒
    2020-12-02 05:20

    Yes a correct sign() function should be at least in the math module - as it is in numpy. Because one frequently needs it for math oriented code.

    But math.copysign() is also useful independently.

    cmp() and obj.__cmp__() ... have generally high importance independently. Not just for math oriented code. Consider comparing/sorting tuples, date objects, ...

    The dev arguments at http://bugs.python.org/issue1640 regarding the omission of math.sign() are odd, because:

    • There is no separate -NaN
    • sign(nan) == nan without worry (like exp(nan) )
    • sign(-0.0) == sign(0.0) == 0 without worry
    • sign(-inf) == -1 without worry

    -- as it is in numpy

提交回复
热议问题